-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNadraDB.java
More file actions
186 lines (144 loc) · 6.24 KB
/
Copy pathNadraDB.java
File metadata and controls
186 lines (144 loc) · 6.24 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class NadraDB
{
public String CNIC;
public String issued_date;
public String expiry_date;
public static String filename;
public NadraDB(String filename1)
{
this.filename=filename1;
}
public NadraDB(){}
public NadraDB(String CNIC1,String iss_date,String exp_date)
{
this.CNIC=CNIC1;
this.issued_date=iss_date;
this.expiry_date=exp_date;
}
public static List<NadraDB> Load_NBData() {
List<NadraDB> NadraDB = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
// Read file line by line
while ((line = reader.readLine()) != null) {
// Split the line by comma
String[] parts = line.split(",");
if (parts.length == 3)
{
String CNIC = parts[0];
String iss_date = parts[1];
String exp_date=parts[2];
// Handle blank value for peak hour unit price (assign null if it's blank)
// Create a MeterPricingInfo object and add to list
NadraDB ndb = new NadraDB(CNIC,iss_date, exp_date);
NadraDB.add(ndb);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return NadraDB;
}
public void displayInfo()
{
System.out.println("CNIC: "+CNIC);
System.out.print("Issued Date: "+issued_date+" ");
System.out.print("Expiry Date: "+expiry_date);
System.out.println();
}
public static void Check_expiry(List<NadraDB> nadraList, List<Customer> customers) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate currentDate = LocalDate.now(); // Get the current date
for (Customer customer : customers) {
String customerCNIC = String.valueOf(customer.CNIC); // CNIC from customer
for (NadraDB ndb : nadraList) {
String nadraCNIC = ndb.CNIC; // CNIC from NADRA database
// If CNIC matches
if (customerCNIC.equals(nadraCNIC)) {
// Parse the expiry date from the NADRA database
LocalDate expiryDate = LocalDate.parse(ndb.expiry_date, formatter);
// Calculate the number of days between the current date and the expiry date
long daysUntilExpiry = ChronoUnit.DAYS.between(currentDate, expiryDate);
// Check if the expiry is within the next 30 days
if (daysUntilExpiry > 0 && daysUntilExpiry <= 30) {
System.out.println("Customer ID: " + customer.cus_id + " has a CNIC expiring in " + daysUntilExpiry + " days.");
}
}
}
}
}
public void update_expiry_date(List<NadraDB> nadraList, List<Customer> customers) {
Scanner scanner = new Scanner(System.in);
// Get input from the user
System.out.print("Enter your CNIC number: ");
String inputCNIC = scanner.nextLine();
System.out.print("Enter your Customer number: ");
String inputCustomerID = scanner.nextLine();
// scanner.nextLine(); // Consume the newline
boolean foundCustomer = false;
boolean foundCNIC = false;
// Find the customer with matching CNIC and customer number
for (Customer customer : customers) {
if (customer.CNIC.equals(inputCNIC) && customer.cus_id.equals( inputCustomerID)){
foundCustomer = true;
// Now search for this CNIC in the Nadra database
for (NadraDB ndb : nadraList) {
if (ndb.CNIC.equals(inputCNIC)) {
foundCNIC = true;
// Prompt the user to enter a new expiry date
System.out.print("Enter the new expiry date (dd/MM/yyyy): ");
String newExpiryDate = scanner.nextLine();
// Validate date format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try {
LocalDate parsedDate = LocalDate.parse(newExpiryDate, formatter);
// Update the expiry date in the NadraDB object
ndb.expiry_date = newExpiryDate;
System.out.println("Expiry date updated successfully for CNIC: " + inputCNIC);
// Save the updated data back to the file
saveUpdatedDataToFile(nadraList);
} catch (Exception e) {
System.out.println("Invalid date format. Please enter the date in dd/MM/yyyy format.");
}
break;
}
}
break;
}
}
if (!foundCustomer) {
System.out.println("Customer number or CNIC not found.");
} else if (!foundCNIC) {
System.out.println("CNIC not found in Nadra database.");
}
}
// Method to save the updated data back to the file
public void saveUpdatedDataToFile(List<NadraDB> nadraList) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
// Write each entry back to the file
for (NadraDB ndb : nadraList) {
writer.write(ndb.CNIC + "," + ndb.issued_date + "," + ndb.expiry_date);
writer.newLine();
}
writer.close();
System.out.println("Data saved successfully.");
} catch (IOException e) {
System.out.println("Error saving data to file.");
e.printStackTrace();
}
}
}