I need help creating a Java program. The assignment is to create a phonebook pro
ID: 3878189 • Letter: I
Question
I need help creating a Java program. The assignment is to create a phonebook program that can store basic information about people, such as names, phonenumbers, and things of that sort using array lists.
2 classes are needed for the program:
A Contacts class that contains a constructor, mutator methods, accessor methods, and any other required methods
A Phonebook ( runner ) that will
1. Will create a phonebook array list
2. Can display a menu of options where you can:
Display contacts
Add contacts
Edit contacts
Remove contacts
Etc.
Help is greatly appreciated
Explanation / Answer
//JAVA 8 import java.util.*; public class PhoneBook { private final List contacts; private final Scanner scanner; public PhoneBook() { contacts = new ArrayList(); scanner = new Scanner(System.in); } private void addContact(Contact contact) { this.contacts.add(contact); } private void removeContact() { System.out.println("Enter name to remove contact: "); String name = scanner.next(); Optional optional = this.contacts.stream().filter(contact -> contact.getName().equalsIgnoreCase(name)).findFirst(); if (optional.isPresent()) { Contact contact = optional.get(); this.contacts.remove(contact); System.out.println("Contact remove Successfully"); } else { System.out.println("No Contacts present"); } } private void update() { System.out.println("Enter name to remove contact: "); String name = scanner.next(); Optional optional = this.contacts.stream().filter(contact -> contact.getName().equalsIgnoreCase(name)).findFirst(); if (optional.isPresent()) { System.out.println("Enter new Name : "); String newName = scanner.next(); System.out.println("Enter new phoneNumber : "); String newPhoneNumber = scanner.next(); Contact contact = optional.get(); contact.setName(newName); contact.setPhoneNumber(newPhoneNumber); System.out.println("Contact Updated Successfully"); } else { System.out.println("No Contacts present"); } } private Contact createContact() { System.out.println("Enter Name : "); String name = scanner.next(); System.out.println("Enter Phone Number : "); String phoneNumber = scanner.next(); return new Contact(name, phoneNumber); } public void run() { while (true) { System.out.println(); System.out.println("1->Add 2->Delete 3->Update 4->Display 0->Exit"); System.out.printf("Enter your option: "); int option = scanner.nextInt(); switch (option) { case 1: Contact contact = this.createContact(); this.addContact(contact); break; case 2: this.removeContact(); break; case 3: this.update(); break; case 4: this.display(); break; case 0: return; } } } private void display() { System.out.println("Contacts : "); this.contacts.forEach(contact -> System.out.println(contact)); } public static void main(String[] args) { new PhoneBook().run(); } } class Contact { private String name; private String phoneNumber; public Contact(String name, String phoneNumber) { this.name = name; this.phoneNumber = phoneNumber; } @Override public String toString() { return this.name + " " + this.phoneNumber; } public void setName(String name) { this.name = name; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getName() { return name; } public String getPhoneNumber() { return phoneNumber; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Contact contact = (Contact) o; return this.name.equals(contact.name) && this.phoneNumber.equals(contact.phoneNumber); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.