-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupplier.java
More file actions
233 lines (215 loc) · 7.25 KB
/
Copy pathSupplier.java
File metadata and controls
233 lines (215 loc) · 7.25 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
221
222
223
224
225
226
227
228
229
230
231
232
233
package javalibrary;
import java.io.*;
import java.util.*;
public class Supplier implements Serializable
{
private static final long serialVersionUID = 1L;
private int suppid;
private String namesupp;
private int phoneno;
private int ordercount;
private double revenue;
static Scanner input = new Scanner(System.in);
static HashMap<Integer, Supplier> suppmap = new HashMap<>();
public Supplier(int suppid, String namesupp, int phoneno, int ordercount, double revenue)
{
this.suppid = suppid;
this.namesupp = namesupp;
this.phoneno = phoneno;
this.ordercount = ordercount;
this.revenue = revenue;
}
public int getSuppid()
{
return suppid;
}
public void setSuppid(int suppid)
{
this.suppid = suppid;
}
public String getNamesupp()
{
return namesupp;
}
public void setNamesupp(String namesupp)
{
this.namesupp = namesupp;
}
public int getPhoneno()
{
return phoneno;
}
public void setPhoneno(int phoneno)
{
this.phoneno = phoneno;
}
public int getOrdercount()
{
return ordercount;
}
public void setOrdercount(int ordercount)
{
this.ordercount = ordercount;
}
public double getRevenue()
{
return revenue;
}
public void setRevenue(double revenue)
{
this.revenue = revenue;
}
public static void savefile()
{
try (ObjectOutputStream writing = new ObjectOutputStream(new FileOutputStream("supplier.bin")))
{
writing.writeObject(suppmap);
writing.close();
System.out.println("Suppliers data saved to file successfully.");
}
catch (IOException e)
{
System.out.println("Error while saving suppliers: " + e.getMessage());
}
}
public static void readfile()
{
try (ObjectInputStream loading = new ObjectInputStream(new FileInputStream("supplier.bin")))
{
suppmap = (HashMap<Integer, Supplier>) loading.readObject();
System.out.println("Suppliers loaded from file successfully.");
loading.close();
}
catch (IOException | ClassNotFoundException e)
{
System.err.println("Error while loading suppliers: " + e.getMessage());
}
}
public static void displaySuppliers()
{
readfile();
if (suppmap.isEmpty())
{
System.out.println("No suppliers available.");
}
else
{
System.out.println ("Supplier List:");
for (HashMap.Entry<Integer, Supplier> entry : suppmap.entrySet())
{
Supplier supp = entry.getValue();
System.out.println("ID: " + supp.getSuppid() + " Name: " + supp.getNamesupp()
+ " Phone: " + supp.getPhoneno() + " Orders: " + supp.getOrdercount()
+ " Revenue: " + supp.getRevenue());
}
}
}
public static boolean add(int id, String name, int phone) {
readfile();
if (suppmap.containsKey(id)) {
return false; // Supplier with this ID already exists
}
Supplier supplier = new Supplier(id, name, phone, 0, 0);
suppmap.put(id, supplier);
savefile();
return true;
}
public static void editSupplier(int supplierId, String newName, String newPhone, String newOrderCount, String newRevenue) {
readfile();
if (!suppmap.containsKey(supplierId)) {
System.out.println("No data found for ID " + supplierId + ".");
return;
}
Supplier supplier = suppmap.get(supplierId);
System.out.println("Editing supplier: " + supplier.getNamesupp());
// Update name if provided
if (newName != null && !newName.isEmpty()) {
supplier.setNamesupp(newName);
}
// Update phone number if provided
if (newPhone != null && !newPhone.isEmpty()) {
try {
int phone = Integer.parseInt(newPhone);
supplier.setPhoneno(phone);
} catch (NumberFormatException ex) {
System.out.println("Invalid phone number format. Update skipped.");
}
}
// Update order count if provided
if (newOrderCount != null && !newOrderCount.isEmpty()) {
try {
int orderCount = Integer.parseInt(newOrderCount);
supplier.setOrdercount(orderCount);
} catch (NumberFormatException ex) {
System.out.println("Invalid order count format. Update skipped.");
}
}
// Update revenue if provided
if (newRevenue != null && !newRevenue.isEmpty()) {
try {
double revenue = Double.parseDouble(newRevenue);
supplier.setRevenue(revenue);
} catch (NumberFormatException ex) {
System.out.println("Invalid revenue format. Update skipped.");
}
}
suppmap.put(supplierId, supplier); // Save updated supplier back to the map
savefile();
System.out.println("Supplier data updated successfully.");
}
public static void deleteSupplier(int supplierId) {
readfile();
if (suppmap.containsKey(supplierId)) {
suppmap.remove(supplierId);
savefile();
System.out.println("Supplier with ID " + supplierId + " has been deleted.");
} else {
System.out.println("No Supplier found with ID " + supplierId + ".");
}
}
public static void searchSupplier(String searchTerm) {
boolean found = false;
// Iterate through supplier map
for (Supplier supplier : suppmap.values()) {
if (String.valueOf(supplier.getSuppid()).equals(searchTerm) ||
supplier.getNamesupp().equalsIgnoreCase(searchTerm) ||
String.valueOf(supplier.getPhoneno()).equals(searchTerm)) {
System.out.println("Supplier found:");
System.out.println("ID: " + supplier.getSuppid());
System.out.println("Name: " + supplier.getNamesupp());
System.out.println("Phone: " + supplier.getPhoneno());
System.out.println("Orders: " + supplier.getOrdercount());
System.out.println("Revenue: " + supplier.getRevenue());
found = true;
break;
}
}
if (!found) {
System.out.println("No supplier found with the given search term.");
}
}
public static void MaxOrderCountSupplier() {
readfile();
if (suppmap.isEmpty()) {
return;
}
Supplier maxOrderSupplier = null;
for (Supplier supplier : suppmap.values()) {
if (maxOrderSupplier == null || supplier.getOrdercount() > maxOrderSupplier.getOrdercount()) {
maxOrderSupplier = supplier;
}
}
}
public static void MaxRevenueSupplier() {
readfile();
if (suppmap.isEmpty()) {
return;
}
Supplier maxRevenueSupplier = null;
for (Supplier supplier : suppmap.values()) {
if (maxRevenueSupplier == null || supplier.getRevenue() > maxRevenueSupplier.getRevenue()) {
maxRevenueSupplier = supplier;
}
}
}
}