Problem Write a program to keep track of business contacts. Specification 1. Cre
ID: 3725729 • Letter: P
Question
Problem
Write a program to keep track of business contacts.
Specification
1. Create a BusinessContact class with the following fields:
firstName:String
lastName:String
phoneNumber:String
emailAddress:String
company:String
2. Store the records in memory in an array.
3. Store the records on disk in the text file contacts.txt.
4. Create a menu that allows for the following operations:
Add a Contact
Delete a Contact
View a Contact
Display the Contact List
5. Automatically read the text file and store the records in an array when the program begins.
6. Automatically write the records to the file when the program ends.
Explanation / Answer
#Please asks me if you have any doubts I ll replay as soon as Possibe import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Scanner; import java.util.stream.Collectors; public class BusinessContactRecord { private Scanner scanner = new Scanner(System.in); ArrayList businessContacts = new ArrayList(); public void add() { this.businessContacts.add(createContact()); } public void delete() { System.out.println("Enter email Address to delete : "); String email = scanner.next(); boolean b = this.businessContacts.removeIf(x -> x.getEmailAddress().equalsIgnoreCase(email)); if (b) { System.out.println("Record Success fully deleted"); } else { System.out.println("No record found"); } } public void displayAContact() { System.out.println("Enter email Address to delete : "); String email = scanner.next(); List contacts = this.businessContacts .stream() .filter(x -> x.getEmailAddress().equalsIgnoreCase(email)) .collect(Collectors.toList()); if (contacts.size() > 0) { System.out.println(contacts.get(0)); } else { System.out.println("Account Not found"); } } public void displayAllContacts() { System.out.println("Contacts"); this.businessContacts.forEach(System.out::println); } public void readFromFile(String fileName) throws IOException { BufferedReader br = new BufferedReader(new FileReader(fileName)); try { String line; do { line = br.readLine(); if (line != null) { String[] split = line.split(":"); this.businessContacts.add(new BusinessContact(split[0].trim(), split[1].trim(), split[2].trim(), split[3].trim(), split[4].trim())); } } while (line != null); } catch (IOException e) { e.printStackTrace(); } finally { br.close(); } } public void writeToFile(String fileName) throws FileNotFoundException, UnsupportedEncodingException { PrintWriter writer = new PrintWriter(fileName, "UTF-8"); this.businessContacts.forEach(writer::println); System.out.println("Successfully write to file"); writer.close(); } private BusinessContact createContact() { System.out.printf("Enter firstName : "); String firstName = scanner.next(); System.out.printf("Enter lastName : "); String lastName = scanner.next(); System.out.printf("Enter phoneNumber : "); String phoneNumber = scanner.next(); System.out.printf("Enter Email address : "); String emailAddress = scanner.next(); System.out.printf("Enter Company : "); String company = scanner.next(); return new BusinessContact(firstName, lastName, phoneNumber, emailAddress, company); } public static void main(String[] args) throws IOException { int option = 0; Scanner scanner = new Scanner(System.in); BusinessContactRecord businessContactRecord = new BusinessContactRecord(); businessContactRecord.readFromFile("contacts.txt"); do { System.out.println("Enter 1->Add 2->Remove 3->Display Single Record 4->Display All 0->Exit"); System.out.printf("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } if (option == 1) { businessContactRecord.add(); } if (option == 2) { businessContactRecord.delete(); } if (option == 3) { businessContactRecord.displayAContact(); } if (option == 4) { businessContactRecord.displayAllContacts(); } else { System.out.println("Invalid contact"); } } while (true); businessContactRecord.writeToFile("contacts.txt"); } } class BusinessContact { private String firstName; private String lastName; private String phoneNumber; private String emailAddress; private String company; public BusinessContact(String firstName, String lastName, String phoneNumber, String emailAddress, String company) { this.firstName = firstName; this.lastName = lastName; this.phoneNumber = phoneNumber; this.emailAddress = emailAddress; this.company = company; } public BusinessContact() { } @Override public String toString() { return firstName + " : " + lastName + " : " + phoneNumber + " : " + emailAddress + " : " + company; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getPhoneNumber() { return phoneNumber; } public String getEmailAddress() { return emailAddress; } public String getCompany() { return company; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } public void setCompany(String company) { this.company = company; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; BusinessContact that = (BusinessContact) o; return Objects.equals(firstName, that.firstName) && Objects.equals(lastName, that.lastName) && Objects.equals(phoneNumber, that.phoneNumber) && Objects.equals(emailAddress, that.emailAddress) && Objects.equals(company, that.company); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.