-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRODIGY_SD_03.java
153 lines (131 loc) · 4.28 KB
/
PRODIGY_SD_03.java
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
import java.util.*;
class Contact{
public String name;
public String phonenumber;
public String email;
public Contact(String name, String phonenumber, String email){
this.name = name;
this.phonenumber = phonenumber;
this.email = email;
}
public void displayContact() {
System.out.println("Name: " + name);
System.out.println("Phone Number: " + phonenumber);
System.out.println("Email Address: " + email);
}
}
public class PRODIGY_SD_03 {
public static Scanner sc = new Scanner(System.in);
public static ArrayList<Contact> contacts= new ArrayList<>();
public static void main(String[] args) {
int choice;
do{
System.out.println("Contact Management System");
System.out.println("Press 1. Add new contact");
System.out.println("Press 2. View Contact");
System.out.println("Press 3. Modify Contact");
System.out.println("Press 4. Delete Contact");
System.out.println("Press 5. Exit");
System.out.println("Enter the choice: ");
choice = sc.nextInt();
sc.nextLine();
switch(choice)
{
case 1:
addcontact();
break;
case 2:
viewcontact();
break;
case 3:
//modifycontact();
break;
case 4:
//deletecontact();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid Choice.");
}
}while(choice != 5);
sc.close();
}
public static void addcontact()
{
System.out.print("Name: ");
String Name = sc.nextLine();
System.out.print("Phone Number: ");
String phoneno = sc.next();
System.out.print("Email address: ");
String emailaddress = sc.next();
Contact newContact = new Contact(Name, phoneno, emailaddress);
contacts.add(newContact);
System.out.println("Contact Added.");
}
public static void viewcontact()
{
System.out.println("Contact Numbers: ");
for(Contact i : contacts){
if(contacts.isEmpty())
{
System.out.println("No Contacts");
}
else{
i.displayContact();
}
}
}
public static void modifycontact()
{
if(contacts.isEmpty())
{
System.out.println("No Contact");
}
else{
viewcontact();
System.out.println("Enter the index of the contact: ");
int index = sc.nextInt();
if(index >= 0 && index<contacts.size())
{
System.out.println("Previous Contact: ");
contacts.get(index).displayContact();
System.out.println("Enter New Name: ");
String newname = sc.next();
System.out.println("Enter New Phone number: ");
String newphoneno = sc.next();
System.out.println("Enter new Email Address: ");
String newemail = sc.next();
Contact modifiedcontact = new Contact(newname, newphoneno, newemail);
contacts.set(index, modifiedcontact);
System.out.println("Contact updated successfully");
}
else{
System.out.println("Invalid Index.");
}
}
}
public static void deletecontact()
{
if(contacts.isEmpty())
{
System.out.println("No Contacts.");
}
else{
viewcontact();
System.out.print("Enter the index of the contact to be deleted: ");
int index = sc.nextInt();
if(index >= 0 && index <contacts.size())
{
System.out.println("Deleting Contact: ");
contacts.get(index).displayContact();
contacts.remove(index);
System.out.println("Contact deleted.");
}
else{
System.out.println("Invalid Index.");
}
}
}
}