-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOS_System.java
More file actions
174 lines (144 loc) · 5.37 KB
/
POS_System.java
File metadata and controls
174 lines (144 loc) · 5.37 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.io.*;
import java.util.Scanner;
class Item implements Serializable{
private String itemCode;
private double price;
private String size;
private String product;
public Item(String cd){
try {
String filepath = "products.csv";
FileReader filereader = new FileReader(filepath);
BufferedReader bufferedreader = new BufferedReader(filereader);
bufferedreader.readLine();
String line;
while((line = bufferedreader.readLine())!= null){
String[] det = line.split(",");
if(det[0].equals(cd)){
this.itemCode = det[0];
this.price = Double.parseDouble(det[1]);
this.size = det[2];
this.product = det[6];
break;
}
}
} catch (IOException e) {
System.out.println("Error Finding File.");
}
}
public String getItemName(){
return this.product;
}
public double getUnitPrice(){
return this.price;
}
}
class Bill implements Serializable{
private int billNo;
private String cashier;
private String branch;
private String customer;
private Item[] items;
private int[] quantity;
private double tot_price;
private int count;
public Bill(int num, String branch, String cashier){
this.billNo = num;
this.count = 0;
this.items = new Item[10];
this.quantity = new int[10];
Scanner scanner1 = new Scanner(System.in);
this.cashier = cashier;
this.branch = branch;
System.out.print("Customer Name: ");
this.customer = scanner1.nextLine();
String cd;
while(true){
System.out.print("Item Code: ");
cd = scanner1.nextLine();
if(cd.equals("-1")){
break;
}
else if(cd.equals("-2")){
this.serialize();
}
else{
inputItems(cd);
}
}
}
public void inputItems(String cd){
Item item = new Item(cd);
this.items[count] = item;
Scanner scanner2 = new Scanner(System.in);
System.out.print("Quantity: ");
this.quantity[count] = scanner2.nextInt();
count++;
}
public void serialize(){
try {
FileOutputStream file = new FileOutputStream("bills.txt");
ObjectOutputStream oos = new ObjectOutputStream(file);
oos.writeObject(this);
oos.close();
file.close();
System.out.println("Bill has been saved.");
} catch (Exception e) {
System.out.println("Error Serializing...");
}
}
public void printBill() {
System.out.println("=========================================");
System.out.printf("Cashier : %s\n", this.cashier);
System.out.printf("Branch : %s\n", this.branch);
System.out.printf("Customer: %s\n", this.customer);
System.out.println("-----------------------------------------");
System.out.printf("%-15s %-10s %-12s %-10s\n", "Item", "Quantity", "Unit Price", "Total");
System.out.println("-----------------------------------------");
int i = 0;
this.tot_price = 0; // reset before calculating
while (items[i] != null) {
String name = items[i].getItemName();
int qn = quantity[i];
double unit_p = items[i].getUnitPrice();
double tot_p = (unit_p * qn);
this.tot_price += tot_p;
System.out.printf("%-15s %-10d %-12.2f %-10.2f\n", name, qn, unit_p, tot_p);
i++;
}
System.out.println("-----------------------------------------");
System.out.printf("%-39s %-10.2f\n", "TOTAL:", this.tot_price);
System.out.println("=========================================");
System.out.println(" THANK YOU - COME AGAIN! ");
System.out.println("=========================================");
}
}
public class POS_System{
public static void main(String[] args){
Scanner scanner3 = new Scanner(System.in);
System.out.print("Input Cashier Name: ");
String cashier_name = scanner3.nextLine();
System.out.print("Input Branch Name: ");
String branch = scanner3.nextLine();
int billNo = 0;
System.out.print("0 - New Bill, 1 - Deserialize Bill: ");
int resp = scanner3.nextInt();
if (resp == 0){
Bill bill = new Bill(billNo, branch, cashier_name);
System.out.println("\n");
bill.printBill();
}
else if(resp == 1){
try {
FileInputStream file = new FileInputStream("bills.txt");
ObjectInputStream ois = new ObjectInputStream(file);
Bill bill = (Bill) ois.readObject();
ois.close();
file.close();
bill.printBill();
} catch (Exception e) {
System.out.println("File Not Found.");
}
}
}
}