-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
executable file
·160 lines (135 loc) · 5.05 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.ArrayList;
/**
* Main class contains methods that tie all the other classes
* (Parser, ProductCategory, Product, Tax, Cart, Reciept) together.
*
* @author Vanessa Esli Tovar
*/
public class Main{
private Parser parser;
private Cart cart;
private Tax tax;
private ProductCategory productCategory;
private Reciept reciept;
/**
* Constructor.
*/
public Main(String filename, float basicTaxRate, float importedTaxRate){
parser = new Parser();
cart = new Cart();
tax = new Tax(basicTaxRate, importedTaxRate);
productCategory = new ProductCategory();
reciept = new Reciept();
init(filename);
}
/**
* Creates product categories, calls method to read the input, calls
* method that creates and adds products.
*
* @param filename name of the input file
*/
private void init(String filename){
// create product cateogries
// hard coded key-value for this project
productCategory.addProduct("book", "books");
productCategory.addProduct("books", "books");
productCategory.addProduct("chocolate", "food");
productCategory.addProduct("chocolates", "food");
productCategory.addProduct("pills", "medical");
// read input
parser.readInput(filename);
// addProducts
try {
this.addProducts(parser.getRawData());
} catch (Exception e) {
System.out.println("Check input integrity");
}
this.generateReceipt();
}
/**
* Creates and adds products using the rawData created in Parser class,
* and addProduct() method in the Cart class.
*
* @param rawData ArrayList<String> of data
*/
private void addProducts(ArrayList<String> rawData){
for(int i = 0; i < rawData.size(); i++) {
// split row at every space and store in array temp
String[] temp = rawData.get(i).split("\\s+");
String name = "";
int quantity = 0;
float price = 0;
boolean imported = false;
// quantity is always the first
quantity = Integer.parseInt(temp[0]);
price = Float.parseFloat(temp[temp.length-1]);
// don't need to check fist item (quantity) and last item (price)
for (int j=1; j<temp.length-2; j++){
if (temp[j].equals("imported")){
imported = true;
if (j == 1){ // leave imported at the begining
name = name + temp[j] + " ";
}
else{ // move it to the begining
name = "imported "+name;
}
}
else{
name = name + temp[j] + " ";
}
}
// removes the last character in the name (the extra space)
name = name.substring(0, name.length()-1);
boolean notExempt = !productCategory.getExemptCategory(name);
System.out.println("Product: "+name+" and its notExempt status is "+notExempt+", imported: "+imported);
float totalTax = tax.calcTax(quantity, price, notExempt, imported);
cart.addProduct(name, quantity, price, imported, totalTax);
}
}
/**
* Generates reciept using methods in the Cart class and Reciept class.
*/
private void generateReceipt(){
ArrayList<Product> productList = cart.getProductList();
ArrayList<Float> costsList = cart.getCostsList();
for (int i=0; i<productList.size(); i++){
String quantity = ""+productList.get(i).getQuantity();
String name = ""+productList.get(i).getName();
String cost = ""+Math.round(costsList.get(i) * 100.0) / 100.0;
//String cost = ""+costsList.get(i);
reciept.addLine(quantity+" "+name+": "+cost);
}
reciept.addLine("Total Taxes: "+Math.round(cart.getTotalTax() * 100.0) / 100.0);
reciept.addLine("Total: "+ Math.round((cart.getTotalTax()+cart.getTotalCost()) * 100.0) / 100.0);
// print reciept
}
/**
* Saves the reciept in a .txt file by calling methods in the Reciept class.
*
* @param outputFileName the desired name of the output file name (ex: "output1.txt")
*/
public void saveReciept(String outputFileName){
this.reciept.printReciept();
this.reciept.save(outputFileName);
}
/**
* Main method.
*/
public static void main(String[] args){
String filename = "";
System.out.println(args[0]);
if (args.length > 0){
filename = args[0];
}
else{
filename = "input1.txt";
}
float basicTaxRate = 0.1f;
float importedTaxRate = 0.05f;
System.out.println("Basic Tax: "+basicTaxRate);
System.out.println("Import Tax: "+importedTaxRate);
System.out.println("=====================");
Main m = new Main(filename, basicTaxRate, importedTaxRate);
m.saveReciept("output.txt"); // "output.txt" is the output file name which the user can change here
}
}