-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
220 lines (178 loc) · 7.14 KB
/
Customer.java
File metadata and controls
220 lines (178 loc) · 7.14 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Customer {
private int customerID;
private String customerName;
//private int numberOfSales = 0;
Customer() {}
Customer(int customerID, String name) {
this.customerID=customerID;
this.customerName=name;
}
public int getCustomerID(){
return customerID;
}
public String getCustomerName(){
return customerName;
}
/*public int getNumberOfSales(){
return numberOfSales;
}
public void setNumberOfSales(int numberOfSales) {
this.numberOfSales = numberOfSales;
}*/
public static void buyItem(List<Product> ProductListArray, List<Customer> CustomerListArray, String customerName, String name, int quantity) {
for (Product product : ProductListArray) {
if (name.equalsIgnoreCase(product.getName())) {
if (product.getStockQuantity() < quantity) {
System.out.println("Sorry, we do not have sufficcient quantity to complete this transaction");
}
else {
System.out.println("You have successfully bought " + product.getName());
product.setStockQuantity(product.getStockQuantity()-quantity);
Save.saveSalesByCustomers(customerName, quantity, name, "SalesData.txt");
}
}
}
/*for(Customer customer : CustomerListArray) {
if (customerName.equalsIgnoreCase(customer.getCustomerName())) {
customer.setNumberOfSales(customer.getNumberOfSales() + quantity);
}
}*/
}
/*public static void salesReport(List<Product> productList, String filename) {
try {
for (Product product : productList) {
File file = new File(filename);
int sales = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.toLowerCase().contains(product.getName().toLowerCase())) {
String[] data = line.split(" ");
sales += Integer.parseInt(data[0]);
}
}
System.out.println("There have been sold " + sales + " of " + product.getName());
}
}
} catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
}*/
public static void salesReportByCustomers(List<Product> productList, List<Customer> customerList) {
try {
for (Customer customer : customerList) {
File file = new File("SalesData.txt");
int sales;
String line;
System.out.println("Sales report for " + customer.getCustomerName() + ":");
for (Product product : productList) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
sales = 0;
while ((line = reader.readLine()) != null) {
if (line.toLowerCase().contains(product.getName().toLowerCase())) {
if (line.toLowerCase().contains(customer.getCustomerName().toLowerCase())) {
String[] data = line.split(" ");
sales += Integer.parseInt(data[0]);
}
}
}
System.out.println(" - " + product.getName() + ": " + sales);
}
}
}
} catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
}
// Method to read Customer information from file and save into ArrayList
public static List<Customer> readCustomerFromFile(String filename) {
List<Customer> CustomerList = new ArrayList<>();
File file = new File(filename);
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(" ");
int customerID = Integer.parseInt(data[0]);
String customerName = data[1];
// Create product object and add to list
CustomerList.add(new Customer(customerID, customerName));
}
}
catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
return CustomerList;
}
public static void addCustomer(int customerID,String customerName,List<Customer> customerList){
boolean customerExists = false;
for (Customer customer : customerList) {
if (customer.getCustomerName() == customerName) {
System.out.println("A customer with the same name already exists!");
customerExists = true;
break;
}
}
if (!customerExists) {
customerList.add(new Customer(customerID, customerName));
System.out.println("Customer added successfully!");
}
else{
System.out.println("Customer Already exists!");
}
}
public static void deleteCustomer(List<Customer> customerList, String customerName) {
Iterator<Customer> iterator = customerList.iterator();
while (iterator.hasNext()) {
Customer customer = iterator.next();
if (customerName.equalsIgnoreCase(customer.getCustomerName())) {
iterator.remove(); // Remove the current product using the iterator
}
}
System.out.println("You have successfully deleted the customer !!");
deleteFromSales(customerName);
}
public static void deleteFromSales(String customerName) {
String tempFile = "tempFile.txt";
File oldFile = new File("SalesData.txt");
File newFile = new File(tempFile);
try (BufferedReader reader = new BufferedReader(new FileReader(oldFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(newFile))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.toLowerCase().contains(customerName.toLowerCase())) {
writer.write(line);
writer.newLine();
}
}
writer.flush();
reader.close();
writer.close();
oldFile.delete();
File dump = new File ("SalesData.txt");
newFile.renameTo(dump);
} catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
}
public static void displayCustomers() {
File file = new File("CustomerData.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
System.out.print("\n");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
}
}