-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise3.java
More file actions
172 lines (155 loc) · 5.41 KB
/
Copy pathExercise3.java
File metadata and controls
172 lines (155 loc) · 5.41 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
class Contacts {
private String firstname;
private String lastname;
private String phone;
public Contacts() {
firstname = "";
lastname = "";
phone = "";
}
public Contacts(String firstname, String lastname, String phone) {
this.firstname = firstname;
this.lastname = lastname;
this.phone = phone;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public String getPhone() {
return phone;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String printContact() {
return firstname + " " + lastname + " " + phone;
}
}
class ContactsIO {
public ArrayList<Contacts> ReadFile(String filename) {
ArrayList<Contacts> contacts = new ArrayList<>();
try {
Scanner input = new Scanner(new File(filename));
while (input.hasNext()) {
String firstname = input.next();
String lastname = input.next();
String phone = input.next();
contacts.add(new Contacts(firstname, lastname, phone));
}
input.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
return contacts;
}
public void WriteFile(String filename, ArrayList<Contacts> contacts) {
try {
PrintWriter output = new PrintWriter(new File(filename));
for (Contacts contact : contacts) {
output.println(contact.printContact());
}
output.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
}
}
class business {
public static void addContact(ArrayList<Contacts> contacts) {
try {
Scanner input = new Scanner(System.in);
System.out.println("Enter the first name: ");
String firstname = input.next();
System.out.println("Enter the last name: ");
String lastname = input.next();
System.out.println("Enter the phone number: ");
String phone = input.next();
contacts.add(new Contacts(firstname, lastname, phone));
input.close();
} catch (Exception e) {
System.out.println("Error");
}
}
public static void deleteContact(ArrayList<Contacts> contacts, int index) {
try {
contacts.remove(index);
} catch (Exception e) {
System.out.println("Error");
}
}
public static void editContact(ArrayList<Contacts> contacts, int index) {
try {
Scanner input = new Scanner(System.in);
System.out.println("Enter the first name: ");
String firstname = input.next();
System.out.println("Enter the last name: ");
String lastname = input.next();
System.out.println("Enter the phone number: ");
String phone = input.next();
contacts.get(index).setFirstname(firstname);
contacts.get(index).setLastname(lastname);
contacts.get(index).setPhone(phone);
input.close();
} catch (Exception e) {
System.out.println("Contact not found");
}
}
}
public class Exercise3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ContactsIO contactsIO = new ContactsIO();
ArrayList<Contacts> contacts = contactsIO.ReadFile("src/Week5/contacts.txt");
while (true) {
System.out.println("1. Add a contact");
System.out.println("2. Delete a contact");
System.out.println("3. Edit a contact");
System.out.println("4. Print contacts");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = input.nextInt();
System.out.print("\n");
switch (choice) {
case 1 -> {
business.addContact(contacts);
}
case 2 -> {
System.out.println("Enter the index of the contact to delete: ");
int index = input.nextInt();
business.deleteContact(contacts, index);
}
case 3 -> {
System.out.println("Enter the index of the contact to edit: ");
int index1 = input.nextInt();
business.editContact(contacts, index1);
}
case 4 -> {
for (Contacts contact : contacts) {
System.out.println(contact.printContact());
}
System.out.print("\n");
}
case 5 -> {
contactsIO.WriteFile("src/Week5/contactoutput.txt", contacts);
input.close();
System.exit(choice);
}
default -> System.out.println("Invalid choice");
}
}
}
}