-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactManagementSystem.java
More file actions
126 lines (114 loc) · 4.32 KB
/
ContactManagementSystem.java
File metadata and controls
126 lines (114 loc) · 4.32 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
import java.io.*;
import java.util.*;
public class ContactManagementSystem {
private static final String FILE_PATH = "contacts.txt";
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("===============================");
System.out.println("CONTACT MANAGEMENT SYSTEM");
System.out.println("[1] Add contact \n[2] View Contacts \n[3] Update Contact \n[4] Delete Contact \n[5] Exit program");
System.out.println("===============================");
System.out.print("Enter Choice: ");
int choice = scan.nextInt();
scan.nextLine();
switch (choice) {
case 1:
addContact(scan);
break;
case 2:
viewContacts();
break;
case 3:
updateContact(scan);
break;
case 4:
deleteContact(scan);
break;
case 5:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
private static void addContact(Scanner scan) {
System.out.print("Enter contact name: ");
String name = scan.nextLine();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true))) {
writer.write(name);
writer.newLine();
System.out.println("Contact added.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
private static void viewContacts() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
String line;
if ((line = reader.readLine()) != null) {
System.out.println("Contacts:");
do {
System.out.println(line);
} while ((line = reader.readLine()) != null);
} else {
System.out.println("Contact List is empty...");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
private static void updateContact(Scanner scan) {
System.out.print("Enter the name of the contact to update: ");
String oldName = scan.nextLine();
System.out.print("Enter the new name: ");
String newName = scan.nextLine();
List<String> contacts = readContactsFromFile();
if (contacts.contains(oldName)) {
contacts.set(contacts.indexOf(oldName), newName);
writeContactsToFile(contacts);
System.out.println("Contact updated.");
} else {
System.out.println("Contact not found.");
}
}
private static void deleteContact(Scanner scan) {
System.out.print("Enter the name of the contact to delete: ");
String name = scan.nextLine();
List<String> contacts = readContactsFromFile();
if (contacts.remove(name)) {
writeContactsToFile(contacts);
System.out.println("Contact deleted.");
} else {
System.out.println("Contact not found.");
}
}
private static List<String> readContactsFromFile() {
List<String> contacts = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
String line;
while ((line = reader.readLine()) != null) {
contacts.add(line);
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return contacts;
}
private static void writeContactsToFile(List<String> contacts) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
for (String contact : contacts) {
writer.write(contact);
writer.newLine();
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}