-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCart.java
More file actions
125 lines (103 loc) · 3.1 KB
/
Copy pathCart.java
File metadata and controls
125 lines (103 loc) · 3.1 KB
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
import java.io.*;
import java.util.StringTokenizer;
public abstract class Cart
{
protected int purchaseQuantity;
private int productId;
protected String name;
protected double totalProfit;
protected static double profit;
public Cart(int purchaseQua, int prodId, String n)
{
purchaseQuantity = purchaseQua;
productId = prodId;
name = n;
}
public Cart()
{
purchaseQuantity = 0;
productId = 0;
name = null;
}
//mutator
public void setpurchaseQuantity (int purchaseQua) { purchaseQuantity = purchaseQua;}
public void setproductId (int proId) { productId = proId;}
public void setString ( String n){n = name;}
public void setTotalProfit( double tp){tp = totalProfit;}
//accessor
public int getpurchaseQuantity () { return purchaseQuantity;}
public int getproductId () { return productId;}
public String getName () { return name;}
public double getTotalProfit() {return totalProfit;}
public abstract String Receipt(double total);
public double calcTotal()
{
double total = 0;
for(int i = 0; i< purchaseQuantity; i++)
{
if(productId == 1)
{
total += 70.00;
}
if(productId == 2)
{
total += 1.50;
}
if(productId == 3)
{
total += 0.80;
}
if(productId == 4)
{
total += 1.80;
}
if(productId == 5)
{
total += 49.90;
}
}
return total;
}
public String generate_orderID()
{
double order_Id = Math.random();
String formattedID =String.format("%.2f",order_Id);
return formattedID;
}
public void calcProfit(double t)
{
//print profit into txt file
try
{
FileWriter wr = new FileWriter("totalProfit.txt",true);
PrintWriter pw = new PrintWriter(wr);
pw.print("\n"+t + ";");
pw.close();
}
catch (Exception e)
{
System.err.println("Error : " + e.getMessage());
}
}
public static void printProfit()
{
try
{
FileReader fr = new FileReader("totalProfit.txt");
BufferedReader br = new BufferedReader(fr);
String strLine;
while ((strLine = br.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(strLine,";");
profit += Double.parseDouble(st.nextToken());
}
br.close();
}
catch (Exception e)
{
System.err.println("Error : " + e.getMessage());
}
String formattedPrice =String.format("%.2f",profit);
System.out.println("\nYour total profit currently is RM: " + formattedPrice);
}
}