PLease help me add four Java OOP concepts iin the program. Add comment so I can identify what concept it is .Thank you! ______ public class Person { private int id; private String name; private String contactNumber; private int age; Was this answer helpful? private String address; private String emailAddress; / constructor public Person(int id, String name, String contactNumber, int age, String address, String emailAddress) { this.id = id; this.name = name; this.contactNumber = contactNumber; this.age = age; this.address = address; this.emailAddress = emailAddress; } / start of getter and setter public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContactNumber() { return contactNumber; } public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } / end of getter and setter / toString @Override public String toString() { return "Id: " + id + "\n" + "Name: " + name + "\n" + "Contact Number: " + contactNumber + "\n" + "Age: " + age + "\n" + "Address: " + address + "\nEmail: " + emailAddress; } } import java.util.ArrayList; import java.util.Scanner; public class PhoneBook { public static void main(String[] args) { / creating scanner object Scanner scanner = new Scanner(System.in); char task; ArrayList phoneBook = new ArrayList<>(); do { / printing menu to the user System.out.println("Tasks: "); System.out.println("\t\tA - Add a contact"); System.out.println("\t\tV - View a contact"); System.out.println("\t\tU - Update a contact"); System.out.println("\t\tD - Delete a contact"); System.out.print("Chosen Task is: "); task = scanner.next().charAt(0); switch (task) { case 'A': System.out.println(""); / taking id input System.out.print("Enter id : "); int id = scanner.nextInt(); / clearing buffer scanner.nextLine(); / taking name input System.out.print("Enter name: "); String name = scanner.nextLine(); / taking contact number input System.out.print("Enter contact number: "); String contactNumber = scanner.nextLine(); / taking age input System.out.print("Enter age: "); int age = scanner.nextInt(); / clearing buffer scanner.nextLine(); / taking address input System.out.print("Enter address: "); String address = scanner.nextLine(); / taking email input System.out.print("Enter email: "); String email = scanner.nextLine(); Person person = new Person(id, name, contactNumber, age, address, email); phoneBook.add(person); System.out.println(); break; case 'V': System.out.println(""); System.out.print("\nEnter id of the contact to be viewed: "); int idToView = scanner.nextInt(); / clearing buffer scanner.nextLine(); for (Person item : phoneBook) { if (item.getId() == idToView) { System.out.println(item); } } System.out.println(); break; case 'U': System.out.println(""); System.out.print("\nEnter id of the contact to be updated: "); int idToUpdate = scanner.nextInt(); System.out.println("\nWhat you want to update? 1. Name, 2. Phone Number, 3. Age, 4. Address or 5. Email"); int number = scanner.nextInt(); / clearing buffer scanner.nextLine(); switch (number) { case 1: System.out.print("Enter name to update: "); String nameToUpdate = scanner.nextLine(); for (Person item : phoneBook) { if (item.getId() == idToUpdate) { item.setName(nameToUpdate); } } break; case 2: System.out.print("Enter contact number to update: "); String contactNumberToUpdate = scanner.nextLine(); for (Person item : phoneBook) { if (item.getId() == idToUpdate) { item.setContactNumber(contactNumberToUpdate); } } break; case 3: System.out.print("Enter age to update: "); int ageToUpdate = scanner.nextInt(); / clearing buffer scanner.nextLine(); for (Person item : phoneBook) { if (item.getId() == idToUpdate) { item.setAge(ageToUpdate); } } break; case 4: System.out.print("Enter address to update: "); String addressToUpdate = scanner.nextLine(); for (Person item : phoneBook) { if (item.getId() == idToUpdate) { item.setAddress(addressToUpdate); } } break; case 5: System.out.print("Enter email to update: "); String emailToUpdate = scanner.nextLine(); for (Person item : phoneBook) { if (item.getId() == idToUpdate) { item.setEmailAddress(emailToUpdate); } } } System.out.println(); break; case 'D': System.out.println(""); System.out.print("\nEnter id of the contact to be deleted: "); int idToDelete = scanner.nextInt(); / clearing buffer scanner.nextLine(); boolean b = phoneBook.removeIf(item -> item.getId() == idToDelete); if (b){ System.out.println("Item Deleted"); }else{ System.out.println("Item not exist in the Phone Book"); } System.out.println(); } } while (task == 'A' || task == 'V' || task == 'U' || task == 'D'); } }
PLease help me add four Java OOP concepts iin the program. Add comment so I can identify what concept it is .Thank you!
______
public class Person {
private int id; private String name;
private String contactNumber; private int age;
Was this answer helpful?
private String address; private String emailAddress;
/ constructor
public Person(int id, String name, String contactNumber, int age, String address, String emailAddress) { this.id = id;
this.name = name; this.contactNumber = contactNumber; this.age = age;
this.address = address; this.emailAddress = emailAddress;
}
/ start of getter and setter public int getId() {
return id;
}
public String getName() { return name;
}
public void setName(String name) { this.name = name;
}
public String getContactNumber() { return contactNumber;
}
public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber;
}
public int getAge() { return age;
}
public void setAge(int age) { this.age = age;
}
public String getAddress() { return address;
}
public void setAddress(String address) { this.address = address;
}
public String getEmailAddress() { return emailAddress;
}
public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress;
}
/ end of getter and setter
/ toString @Override
public String toString() {
return "Id: " + id + "\n" + "Name: " + name + "\n" + "Contact Number: " + contactNumber + "\n"
+ "Age: " + age + "\n" + "Address: " + address + "\nEmail: " + emailAddress;
}
}
import java.util.ArrayList; import java.util.Scanner;
public class PhoneBook {
public static void main(String[] args) {
/ creating scanner object
Scanner scanner = new Scanner(System.in); char task;
ArrayList<Person> phoneBook = new ArrayList<>();
do {
/ printing menu to the user System.out.println("Tasks: "); System.out.println("\t\tA - Add a contact"); System.out.println("\t\tV - View a contact"); System.out.println("\t\tU - Update a contact"); System.out.println("\t\tD - Delete a contact"); System.out.print("Chosen Task is: ");
task = scanner.next().charAt(0);
switch (task) { case 'A':
System.out.println("<Add a contact>");
/ taking id input System.out.print("Enter id : "); int id = scanner.nextInt();
/ clearing buffer scanner.nextLine();
/ taking name input System.out.print("Enter name: "); String name = scanner.nextLine();
/ taking contact number input System.out.print("Enter contact number: "); String contactNumber = scanner.nextLine();
/ taking age input System.out.print("Enter age: "); int age = scanner.nextInt();
/ clearing buffer scanner.nextLine();
/ taking address input System.out.print("Enter address: "); String address = scanner.nextLine();
/ taking email input System.out.print("Enter email: "); String email = scanner.nextLine();
Person person = new Person(id, name, contactNumber, age, address, email); phoneBook.add(person);
System.out.println(); break;
case 'V':
System.out.println("<View a contact>"); System.out.print("\nEnter id of the contact to be viewed: "); int idToView = scanner.nextInt();
/ clearing buffer scanner.nextLine();
for (Person item : phoneBook) { if (item.getId() == idToView) { System.out.println(item);
}
}
System.out.println();
break; case 'U':
System.out.println("<Update a contact>"); System.out.print("\nEnter id of the contact to be updated: "); int idToUpdate = scanner.nextInt();
System.out.println("\nWhat you want to update? 1. Name, 2. Phone Number, 3. Age, 4. Address or 5. Email");
int number = scanner.nextInt();
/ clearing buffer scanner.nextLine(); switch (number) { case 1:
System.out.print("Enter name to update: "); String nameToUpdate = scanner.nextLine(); for (Person item : phoneBook) {
if (item.getId() == idToUpdate) { item.setName(nameToUpdate);
}
}
break;
case 2:
System.out.print("Enter contact number to update: "); String contactNumberToUpdate = scanner.nextLine(); for (Person item : phoneBook) {
if (item.getId() == idToUpdate) {
item.setContactNumber(contactNumberToUpdate);
}
}
break;
case 3:
System.out.print("Enter age to update: "); int ageToUpdate = scanner.nextInt();
/ clearing buffer scanner.nextLine();
for (Person item : phoneBook) {
if (item.getId() == idToUpdate) { item.setAge(ageToUpdate);
}
}
break;
case 4:
System.out.print("Enter address to update: "); String addressToUpdate = scanner.nextLine(); for (Person item : phoneBook) {
if (item.getId() == idToUpdate) { item.setAddress(addressToUpdate);
}
}
break;
case 5:
System.out.print("Enter email to update: "); String emailToUpdate = scanner.nextLine(); for (Person item : phoneBook) {
if (item.getId() == idToUpdate) { item.setEmailAddress(emailToUpdate);
}
}
}
System.out.println(); break;
case 'D':
System.out.println("<Delete a contact>"); System.out.print("\nEnter id of the contact to be deleted: "); int idToDelete = scanner.nextInt();
/ clearing buffer scanner.nextLine();
boolean b = phoneBook.removeIf(item -> item.getId() == idToDelete); if (b){
System.out.println("Item Deleted");
}else{
System.out.println("Item not exist in the Phone Book");
}
System.out.println();
}
} while (task == 'A' || task == 'V' || task == 'U' || task == 'D');
}
}
Step by step
Solved in 4 steps with 2 images