-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
430 lines (350 loc) · 16.5 KB
/
Copy pathCustomer.java
File metadata and controls
430 lines (350 loc) · 16.5 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import java.util.Scanner;
import java.io.IOException;
import java.io.*;
import java.util.*;
import java.io.BufferedWriter;
import java.io.BufferedReader;
public class Customer
{
public String cus_id;
public String CNIC;
public String Name;
public String Phone_no;
public String Address;
public Character Cus_type;
public String Meter_type;
public String connec_date;
public int reg_hour_units;
public int peak_hour_units;
static String filename;
public Customer(String filename1)
{
this.filename=filename1;
}
public Customer(){}
public Customer(String cus_id, String CNIC, String Name, String Address, String Phone_no, char Cus_type, String Meter_type, String connec_date, int reg_hour_units, int peak_hour_units)
{
this.cus_id = cus_id;
this.CNIC = CNIC;
this.Name = Name;
this.Address = Address;
this.Phone_no = Phone_no;
this.Cus_type = Cus_type;
this.Meter_type = Meter_type;
this.connec_date = connec_date;
this.reg_hour_units = reg_hour_units;
this.peak_hour_units = peak_hour_units;
}
public void displayCustomerInfo() {
System.out.println("Customer ID: " + cus_id);
System.out.println("CNIC: " + CNIC);
System.out.println("Name: " + Name);
System.out.println("Address: " + Address);
System.out.println("Phone No: " + Phone_no);
System.out.println("Customer Type: " + Cus_type);
System.out.println("Meter Type: " + Meter_type);
System.out.println("Connection Date: " + connec_date);
System.out.println("Regular Hour Units: " + reg_hour_units);
System.out.println("Peak Hour Units: " + peak_hour_units);
System.out.println();
}
public static List<Customer> loadCustomerData() {
List<Customer> customerList = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
// Read file line by line
while ((line = reader.readLine()) != null) {
// Split line into parts by comma
String[] parts = line.split(",");
if (parts.length == 10) {
String cus_id = parts[0];
String CNIC = parts[1];
String Name = parts[2];
String Address = parts[3];
String Phone_no = parts[4];
char Cus_type = parts[5].charAt(0); // Assuming D or C for Domestic or Commercial
String Meter_type = parts[6];
String connec_date = parts[7];
int reg_hour_units = Integer.parseInt(parts[8]);
int peak_hour_units = Integer.parseInt(parts[9]);
// Create new Customer object and add to list
Customer customer = new Customer(cus_id, CNIC, Name, Address, Phone_no, Cus_type, Meter_type, connec_date, reg_hour_units, peak_hour_units);
customerList.add(customer);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return customerList;
}
public void view_curr_bill(List<BillingInfo> billingInfos, List<Tax_tariff_info> pricingInfos) {
Scanner scanner = new Scanner(System.in);
// Step 1: Collect input from the user
System.out.print("Enter your 4-digit Customer ID: ");
String customerId = scanner.nextLine();
System.out.print("Enter your CNIC: ");
String enteredCNIC = scanner.nextLine();
System.out.print("Enter your Meter Type (1Phase/3Phase): ");
String enteredMeterType = scanner.nextLine();
System.out.print("Enter your current regular meter reading: ");
int currRegMeterReading = scanner.nextInt();
int currPeakMeterReading = 0;
if (enteredMeterType.equalsIgnoreCase("3Phase")) {
System.out.print("Enter your current peak meter reading: ");
currPeakMeterReading = scanner.nextInt();
}
// Step 2: Find matching customer
Customer matchedCustomer = null;
for (Customer customer : loadCustomerData()) {
if (customer.cus_id.equals(customerId) && customer.CNIC.equals(enteredCNIC)
&& customer.Meter_type.equalsIgnoreCase(enteredMeterType)) {
matchedCustomer = customer;
break;
}
}
if (matchedCustomer == null) {
System.out.println("Invalid Customer ID, CNIC, or Meter Type.");
return;
}
// Step 3: Find the tariff for the entered meter type
Tax_tariff_info matchedTariff = null;
for (Tax_tariff_info tariff : pricingInfos) {
if (tariff.meterType.equalsIgnoreCase(enteredMeterType)) {
matchedTariff = tariff;
break;
}
}
if (matchedTariff == null) {
System.out.println("No tariff information found for your meter type.");
return;
}
// Step 4: Find the billing information for the matched customer
BillingInfo matchedBillingInfo = null;
for (BillingInfo billingInfo : billingInfos) {
if (billingInfo.cus_id.equals(customerId)) {
matchedBillingInfo = billingInfo;
break;
}
}
if (matchedBillingInfo == null) {
System.out.println("No billing information found for your customer ID.");
return;
}
// Step 5: Calculate electricity cost
int regularUnitsConsumed = currRegMeterReading - matchedCustomer.reg_hour_units;
int peakUnitsConsumed = (enteredMeterType.equalsIgnoreCase("3Phase"))
? (currPeakMeterReading - matchedCustomer.peak_hour_units)
: 0;
int regCost = regularUnitsConsumed * matchedTariff.regularUnitPrice;
int peakCost = peakUnitsConsumed * (matchedTariff.peakHourUnitPrice != null ? matchedTariff.peakHourUnitPrice : 0);
int totalElectricityCost = regCost + peakCost;
// Step 6: Calculate total cost with tax and fixed charges
int taxAmount = (totalElectricityCost * matchedTariff.taxPercentage) / 100;
int totalAmountDue = totalElectricityCost + taxAmount + matchedTariff.fixedCharges;
// Step 7: Display the bill
System.out.println("\n--- Customer Information ---");
matchedCustomer.displayCustomerInfo();
System.out.println("--- Bill Information ---");
System.out.println("Billing Month: " + matchedBillingInfo.billing_month);
System.out.println("Current Regular Meter Reading: " + currRegMeterReading);
if (enteredMeterType.equalsIgnoreCase("3Phase")) {
System.out.println("Current Peak Meter Reading: " + currPeakMeterReading);
}
System.out.println("Electricity Cost (Regular): " + regCost);
if (enteredMeterType.equalsIgnoreCase("3Phase")) {
System.out.println("Electricity Cost (Peak): " + peakCost);
}
System.out.println("Sales Tax: " + taxAmount);
System.out.println("Fixed Charges: " + matchedTariff.fixedCharges);
System.out.println("Total Amount Due: " + totalAmountDue);
System.out.println("Due Date: " + matchedBillingInfo.due_date);
System.out.println("Bill Payment Status: " + matchedBillingInfo.bill_paid_status);
if (!matchedBillingInfo.payment_date.isEmpty()) {
System.out.println("Payment Date: " + matchedBillingInfo.payment_date);
}
System.out.println();
}
public void update_customer_data(List<Customer> cusInfos) {
Scanner scanner = new Scanner(System.in);
// Step 1: Ask the user for the customer ID to update
System.out.print("Enter the Customer ID of the customer you want to update: ");
String customerId = scanner.nextLine();
// Step 2: Find the customer in the list
Customer matchedCustomer = null;
for (Customer customer : cusInfos) {
if (customer.cus_id.equals(customerId)) {
matchedCustomer = customer;
break;
}
}
if (matchedCustomer == null) {
System.out.println("Customer with the given ID not found.");
return;
}
// Step 3: Ask the user what they want to update
System.out.println("What would you like to update?");
System.out.println("1. Customer ID");
System.out.println("2. Phone Number");
System.out.println("3. Address");
System.out.println("4. Regular Hour Units");
System.out.println("5. Peak Hour Units");
System.out.print("Enter your choice (1-5): ");
int choice = scanner.nextInt();
scanner.nextLine(); // Clear the buffer
switch (choice) {
case 1:
// Update Customer ID
System.out.print("Enter new Customer ID: ");
String newCusId = scanner.nextLine();
matchedCustomer.cus_id = newCusId;
break;
case 2:
// Update Phone Number
System.out.print("Enter new Phone Number: ");
String newPhone = scanner.nextLine();
matchedCustomer.Phone_no = newPhone;
break;
case 3:
// Update Address
System.out.print("Enter new Address: ");
String newAddress = scanner.nextLine();
matchedCustomer.Address = newAddress;
break;
case 4:
// Update Regular Hour Units
System.out.print("Enter new Regular Hour Units: ");
int newRegHours = scanner.nextInt();
matchedCustomer.reg_hour_units = newRegHours;
break;
case 5:
// Update Peak Hour Units (only for customers with 3-phase meters)
if (matchedCustomer.Meter_type.equalsIgnoreCase("3Phase")) {
System.out.print("Enter new Peak Hour Units: ");
int newPeakHours = scanner.nextInt();
matchedCustomer.peak_hour_units = newPeakHours;
} else {
System.out.println("This customer does not have a 3-phase meter.");
}
break;
default:
System.out.println("Invalid choice.");
return;
}
// Step 4: Save the updated customer list back into the file
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (Customer customer : cusInfos) {
writer.write(customer.cus_id + "," + customer.CNIC + "," + customer.Name + "," + customer.Address + ","
+ customer.Phone_no + "," + customer.Cus_type + "," + customer.Meter_type + "," + customer.connec_date
+ "," + customer.reg_hour_units + "," + customer.peak_hour_units);
writer.newLine();
}
writer.close();
System.out.println("Customer data updated successfully.");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error updating customer data.");
}
}
public void add_row(List<Customer> cusInfos, List<BillingInfo> billInfos, List<NadraDB> nadraInfos) {
Scanner scanner = new Scanner(System.in);
// Step 1: Ask for CNIC
System.out.print("Enter CNIC of the customer: ");
String cnic = scanner.nextLine();
// Step 2: Check if the customer already has 3 meters installed
int meterCount = 0;
for (Customer customer : cusInfos) {
if (customer.CNIC.equals(cnic)) {
meterCount++;
}
}
// If meter limit reached, display message and return
if (meterCount >= 3) {
System.out.println("Meter limit reached for this customer.");
return;
}
// Step 3: Ask for customer details
System.out.println("Enter new meter information for the customer:");
System.out.print("Enter Customer ID: ");
String cusId = scanner.nextLine();
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Address: ");
String address = scanner.nextLine();
System.out.print("Enter Phone Number: ");
String phone = scanner.nextLine();
System.out.print("Enter Customer Type (D for Domestic, C for Commercial): ");
char cusType = scanner.nextLine().charAt(0);
System.out.print("Enter Meter Type (1Phase/3Phase): ");
String meterType = scanner.nextLine();
System.out.print("Enter Connection Date (dd/mm/yyyy): ");
String connecDate = scanner.nextLine();
System.out.print("Enter Regular Hour Units: ");
int regHourUnits = scanner.nextInt();
int peakHourUnits = 0;
if (meterType.equalsIgnoreCase("3Phase")) {
System.out.print("Enter Peak Hour Units: ");
peakHourUnits = scanner.nextInt();
}
// Step 4: Create and add the new customer to the list
Customer newCustomer = new Customer(cusId, cnic, name, address, phone, cusType, meterType, connecDate, regHourUnits, peakHourUnits);
cusInfos.add(newCustomer);
// Step 5: Ask for billing information and add to the billing list
System.out.println("Enter billing information:");
System.out.print("Enter Billing Month (as an integer, e.g., 1 for January): ");
int billingMonth = scanner.nextInt();
System.out.print("Enter Current Regular Meter Reading: ");
int currRegMeter = scanner.nextInt();
int currPeakMeter = 0;
if (meterType.equalsIgnoreCase("3Phase")) {
System.out.print("Enter Current Peak Meter Reading: ");
currPeakMeter = scanner.nextInt();
}
System.out.print("Enter Reading Date (dd/mm/yyyy): ");
scanner.nextLine(); // Clear the buffer
String readingDate = scanner.nextLine();
System.out.print("Enter Cost of Electricity: ");
int costOfElec = scanner.nextInt();
System.out.print("Enter Sales Tax: ");
int salesTax = scanner.nextInt();
System.out.print("Enter Fixed Charges: ");
int fixedCharges = scanner.nextInt();
int totalAmnt = costOfElec + salesTax + fixedCharges;
System.out.print("Enter Due Date (dd/mm/yyyy): ");
scanner.nextLine(); // Clear the buffer
String dueDate = scanner.nextLine();
System.out.print("Enter Bill Paid Status (Paid/Unpaid): ");
String billPaidStatus = scanner.nextLine();
System.out.print("Enter Payment Date (if paid, otherwise leave blank): ");
String paymentDate = scanner.nextLine();
// Step 6: Create and add the new billing info to the list
BillingInfo newBillingInfo = new BillingInfo(cusId, billingMonth, currRegMeter, currPeakMeter, readingDate, costOfElec, salesTax, fixedCharges, totalAmnt, dueDate, billPaidStatus, paymentDate);
billInfos.add(newBillingInfo);
// Step 7: Save the new customer and billing info to their respective files
try {
// Save customer info
BufferedWriter cusWriter = new BufferedWriter(new FileWriter(filename, true)); // Append mode
cusWriter.write(newCustomer.cus_id + "," + newCustomer.CNIC + "," + newCustomer.Name + "," + newCustomer.Address + ","
+ newCustomer.Phone_no + "," + newCustomer.Cus_type + "," + newCustomer.Meter_type + "," + newCustomer.connec_date + ","
+ newCustomer.reg_hour_units + "," + newCustomer.peak_hour_units);
cusWriter.newLine();
cusWriter.close();
// Save billing info
BufferedWriter billWriter = new BufferedWriter(new FileWriter("C:\\Users\\city\\Desktop\\java\\LESCO_Billing_System_in_Java\\data\\Billinginfo.txt", true));
// Append mode
billWriter.newLine();
billWriter.write(newBillingInfo.cus_id + "," + newBillingInfo.billing_month + "," + newBillingInfo.curr_reg_meter + "," + newBillingInfo.curr_reg_peak + ","
+ newBillingInfo.reading_date + "," + newBillingInfo.cost_of_elec + "," + newBillingInfo.sales_tax + ","
+ newBillingInfo.fixed_charges + "," + newBillingInfo.total_amnt + "," + newBillingInfo.due_date + ","
+ newBillingInfo.bill_paid_status + "," + newBillingInfo.payment_date);
billWriter.newLine();
billWriter.close();
System.out.println("New meter installed and customer/billing information saved successfully.");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error saving customer or billing data.");
}
}
}