I got a little project that I had to complete for a job. I am sure you guys have seen this project on here. I am want two things, tip on how to get the correct answer and also a peer review on my solution so far.
public class OrderMethod {
static Map<String, BigDecimal> newOrder;
static final BigDecimal BASICSALES = new BigDecimal(.10);
static final BigDecimal IMPORTSALES = new BigDecimal(.05);
static final BigDecimal BASICANDIMPORTSALES = new BigDecimal(.15);
static final BigDecimal Rounding = new BigDecimal(0.05);
static BigDecimal total = new BigDecimal(0);
public void convertOrders() throws IOException {
ReadFile readfile = new ReadFile();
ArrayList<String> order = readfile.getFile();
newOrder = new LinkedHashMap<String, BigDecimal>();
for(String i : order) {
String[] splitOrder = i.split("at\\ ");
newOrder.put(splitOrder[0], new BigDecimal(splitOrder[1]));
}
}
public void calculate() {
newOrder.forEach((k, v) -> {
if(!((k.contains("chocolate")) || k.contains("book") || k.contains("pill")) && k.contains("import")){
v = v.multiply(BASICANDIMPORTSALES).add(v);
v = v.round(new MathContext(4));
System.out.println("Both " + k + " tax: $" + v);
newOrder.put(k, v);
}
else {
if(!(k.contains("chocolate")|| k.contains("book") || k.contains("pill"))) {
v = v.multiply(BASICSALES).add(v);
v = v.round(new MathContext(4));
System.out.println("Basic " + k + " tax: $" + v);
newOrder.put(k, v);
}
if(k.contains("import")) {
v = v.multiply(IMPORTSALES).add(v);
v = v.round(new MathContext(4));
System.out.println("Import " + k + " tax: $" + v);
newOrder.put(k, v);
}
}
total = total.add(v);
});
}
public void print() {
newOrder.forEach((k, v) -> {
System.out.println(k + ": $" + v);
});
System.out.println("Total: $" + total);
}
public static void main(String[] args) throws IOException {
OrderMethod om = new OrderMethod();
om.convertOrders();
om.calculate();
om.print();
}
}
So what I have done is that I have the program reading a text file containing inputs such as
Input 1:
1 book at 12.49
1 music CD at 14.99
1 chocolate bar at 0.85
Input 2:
1 imported box of chocolates at 10.00
1 imported bottle of perfume at 47.50
Input 3:
1 imported bottle of perfume at 27.99
1 bottle of perfume at 18.99
1 packet of headache pills at 9.75
1 box of imported chocolates at 11.25
These are the list of solutions: Output:
Output 1:
1 book : 12.49
1 music CD: 16.49
1 chocolate bar: 0.85
Sales Taxes: 1.50
Total: 29.83
Output 2:
1 imported box of chocolates: 10.50
1 imported bottle of perfume: 54.65
Sales Taxes: 7.65
Total: 65.15
Output 3:
1 imported bottle of perfume: 32.19
1 bottle of perfume: 20.89
1 packet of headache pills: 9.75
1 imported box of chocolates: 11.85
Sales Taxes: 6.70
Total: 74.68
I am having a little problem with my calculation. No matter what my answers to Input 2 and 3 seems to be a couple of decimal places off. I get $65.12 for Input 2 and $74.64 for Input 3. I want help on how to better round up my answers and also what do you think of my code so far.