-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSave.java
More file actions
94 lines (70 loc) · 3.09 KB
/
Save.java
File metadata and controls
94 lines (70 loc) · 3.09 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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public class Save {
// Method to save all products
public static void saveAllProducts(List<Product> productList, String filename) {
File file = new File(filename);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
for (Product product : productList) {
String productInfo = product.getProductID() + " " + product.getName() + " " +
product.getDescription() + " " + product.getPrice() + " " +
product.getStockQuantity();
writer.write(productInfo);
writer.newLine();
}
} catch (IOException e) {
System.out.println("An error occurred while writing to the file: " + e.getMessage());
}
}
// Method to save all customers
public static void saveAllCustomers(List<Customer> customerList, String filename) {
File file = new File(filename);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
for (Customer customer : customerList) {
String customerInfo = customer.getCustomerID() + " " + customer.getCustomerName();
writer.write(customerInfo);
writer.newLine();
}
} catch (IOException e) {
System.out.println("An error occurred while writing to the file: " + e.getMessage());
}
}
// Method to save all suppliers
public static void saveAllSuppliers(List<Supplier> supplierList, String filename) {
File file = new File(filename);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
for (Supplier supplier : supplierList) {
String supplierInfo = supplier.getSupplierID() + " " + supplier.getSupplierName();
writer.write(supplierInfo);
writer.newLine();
}
} catch (IOException e) {
System.out.println("An error occurred while writing to the file: " + e.getMessage());
}
}
// Method to save sales transactions
public static void saveSalesByCustomers(String customerName, int quantity, String name, String filename) {
File file = new File(filename);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))){
String salesInfo = quantity + " kg's of " + name + " have been soled to " + customerName;
writer.write(salesInfo);
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
// Method to save sales transactions
public static void saveSalesFromSupplier(String supplierName, int quantity, String productName, String filename) {
File file = new File(filename);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))){
String salesInfo = quantity + " kg's of " + productName + " have been bought from " + supplierName;
writer.write(salesInfo);
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}