HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Swedish Tax Calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
taxcalculatorswedish

Problem

I'm submitting this code in a couple of days as part of an an interview process. The company knows that I don't have any formal experience with Java. I'm hoping they are testing my ability to learn it on the fly.

This is my first non-trivial Java program and I'm looking for improvements and advice as if to whether I'm following "best practice" Java.

The comments below explain a bit about the program and example input/output is posted below as well.

The rounding was a bit weird, as it was always up to the next .05 cents to satisfy the requirements.

The code can be tested here.

```
/***
* RUN AT: http://ideone.com/ZEPZVl
* REVIEWED AT:
*
* ASSUMPTIONS: That the input is well-formed. A group is terminated by
an empty line of form "^\\s$". Headings are of the form
* "^Input (\\d):". Monetary values are of the form
* "(\\sat) ([0-9]+\\.[0-9]+)".
*
* MODIFYING: Strings that mark food, books and medical supplies can be set
* in base_array. String that mark imports can be set in import_array.
*
* NOTE: This code was developed at ideone.com. The final empty line
* was not detected and I was required to put an END. I assume the
* the front end of the web-app was stripping trailing white space.
*
***/

import java.io.InputStream;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Main {
public static void main (String[] args) {
try {
Taxer taxer = new Taxer();
taxer.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}

class Taxer {

private String[] import_array = {"import"},
base_array = {"book", "chocolate", "pills"};
private Scanner scanner = new Scanner(System.in);
private final Pattern p_input = Pattern.compile("^Input (\\d):"),

Solution

Just a few quick notes:

-
The catch block in the main method seems unnecessary. Throwing out exceptions has the same effect:

public static void main(final String[] args) {
    final Taxer taxer = new Taxer();
    taxer.run();
}


-
Try to minimize the scope of local variables. It's not necessary to declare them at the beginning of the method, declare them where they are first used. (Effective Java, Second Edition, Item 45: Minimize the scope of local variables)

-
Don't use floating point variables where you may need exact results:

  • Effective Java, 2nd Edition, Item 48: Avoid float and double if exact answers are required



  • Why not use Double or Float to represent currency?



-
Variable name prefixes are unnecessary and uncommon in the Java world. See Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions

Code Snippets

public static void main(final String[] args) {
    final Taxer taxer = new Taxer();
    taxer.run();
}

Context

StackExchange Code Review Q#18869, answer score: 3

Revisions (0)

No revisions yet.