Question

In: Computer Science

Add Instance to the Array List Instances in an Array Search for an Instance in the...



Add Instance to the Array

List Instances in an Array

Search for an Instance in the Array

Delete an Instance from the Array

Update an Instance in the Array

Save Array Elements to CSV File

Exit Program










The requirement of this assignment is to design and implement a system in Java that will manage instantiated instances of the Objects created in the User Designed Object Assignment. This is the Young Adult Object. The system should be able to add an instance of a Young Adult Object to the array, remove an instance of a Young Adult Object from the array, list all instances of Young Adult Objects in the array, Update an instance of a Young Adult object in the array and Store all instances in the array to a Comma Separated Value (CSV) File. It should also be able to load a set of starter Young Adult Objects. The task to load the starter set of objects can be a menu option or an automated process when the program starts.


The System should read data for 3 to 5 sample instances from a text file, load the data into the appropriate objects, and add the objects to the array. After loading the sample instances into the array, the system should display a menu similar to the one listed below. Your menu does not have to exactly match this one





Solutions

Expert Solution

Person.java

public class Person {
private String firstName, lastName, address, phone;
  
public Person()
{
this.firstName = this.lastName = this.address = this.phone = "";
}

public Person(String firstName, String lastName, String address, String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phone = phone;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}
  
public String getName(){ return(getFirstName() + " " + getLastName()); }

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}
  
public String strToFile()
{
return(firstName + "," + lastName + "," + address + "," + phone);
}
  
@Override
public String toString()
{
return("Name: " + getFirstName() + " " + getLastName() + ", Address: " + getAddress() + ", Phone: " + getPhone());
}
}

AddressBook.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class AddressBook {
private ArrayList<Person> people;
  
public AddressBook()
{
this.people = new ArrayList<>();
}
  
public void readFile(String filename)
{
Scanner fileReader;
try
{
fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
String[] data = fileReader.nextLine().trim().split(",");
String firstName = data[0];
String lastName = data[1];
String address = data[2];
String phone = data[3];
people.add(new Person(firstName, lastName, address, phone));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(filename + " could not be found! Exiting..");
System.exit(0);
}
}
  
public void writeFile(String filename)
{
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(filename));
pw = new PrintWriter(fw);
for(Person p : people)
pw.write(p.strToFile() + System.lineSeparator());
  
pw.flush();
fw.close();
pw.close();
System.out.println("Address book has been saved to " + filename + ".\n");
} catch (IOException ex) {
System.out.println("Error in writing data to " + filename + "! Exiting..");
System.exit(0);
}
}
  
private int indexOf(String name)
{
int index = -1;
for(int i = 0; i < people.size(); i++)
{
if(people.get(i).getFirstName().equalsIgnoreCase(name))
{
index = i;
break;
}
}
return index;
}
  
public void addPerson()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first name: ");
String firstName = sc.nextLine().trim();
int index = indexOf(firstName);
if(index == -1)
{
System.out.print("Enter the last name: ");
String lastName = sc.nextLine().trim();
System.out.print("Enter the address: ");
String address = sc.nextLine().trim();
System.out.print("Enter the phone number: ");
String phone = sc.nextLine().trim();
people.add(new Person(firstName, lastName, address, phone));
System.out.println(firstName + " is added to the address book successfully.\n");
}
else
System.out.println(firstName + " is already added to the address book!\n");
}
  
public void removePerson()
{
if(people.isEmpty())
{
System.out.println("List is empty!\n");
return;
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first name: ");
String firstName = sc.nextLine().trim();
int index = indexOf(firstName);
if(index == -1)
System.out.println("Sorry, no such person found!\n");
else
{
people.remove(index);
System.out.println(firstName + " is removed from the address book successfully.\n");
}
}
  
public void searchPerson()
{
if(people.isEmpty())
{
System.out.println("List is empty!\n");
return;
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first name: ");
String firstName = sc.nextLine().trim();
int index = indexOf(firstName);
if(index == -1)
System.out.println("Sorry, no such person found!\n");
else
{
System.out.println("Match found:\n" + people.get(index) + "\n");
}
}
  
public void updatePerson()
{
if(people.isEmpty())
{
System.out.println("List is empty!\n");
return;
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first name: ");
String firstName = sc.nextLine().trim();
int index = indexOf(firstName);
if(index == -1)
System.out.println("Sorry, no such person found!\n");
else
{
System.out.println("Provide new information for " + people.get(index).getName() + ":");
System.out.print("Enter new address: ");
String newAddress = sc.nextLine().trim();
System.out.print("Enter new phone: ");
String newPhone = sc.nextLine().trim();
people.get(index).setAddress(newAddress);
people.get(index).setPhone(newPhone);
System.out.println("Information for " + people.get(index).getName() + " is successfully updated.\n");
}
}
  
public void displayAll()
{
if(people.isEmpty())
{
System.out.println("List is empty!\n");
return;
}
for(Person p : people)
System.out.println(p);
System.out.println();
}
}

Main.java (Main class)

import java.util.Scanner;

public class Main {
  
private static final String FILENAME = "my_address_book.txt";
  
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
AddressBook book = new AddressBook();
book.readFile(FILENAME);
int choice;
do
{
displayMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
System.out.println("\nADD A PERSON:\n"
+ "-------------");
book.addPerson();
break;
}
case 2:
{
System.out.println("\nREMOVE A PERSON:\n"
+ "----------------");
book.removePerson();
break;
}
case 3:
{
System.out.println("\nSEARCH A PERSON:\n"
+ "----------------");
book.searchPerson();
break;
}
case 4:
{
System.out.println("\nUPDATE A PERSON:\n"
+ "----------------");
book.updatePerson();
break;
}
case 5:
{
System.out.println("\nDISPLAY ALL PEOPLE:\n"
+ "-------------------");
book.displayAll();
break;
}
case 0:
{
book.writeFile(FILENAME);
System.out.println("\nThanks..Goodbye!\n");
System.exit(0);
}
default:
System.out.println("\nInvalid selection!\n");
}
}while(choice != 0);
}
  
private static void displayMenu()
{
System.out.print("Choose from the following options:\n"
+ "1. Add a person\n"
+ "2. Remove a person\n"
+ "3. Search a person\n"
+ "4. Update a person\n"
+ "5. Display all people\n"
+ "0. Exit\n"
+ "Your selection >> ");
}
}

************************************************************ SCREENSHOT ****************************************************

INPUT FILE (my_address_book.txt) - This file needs to be created before running the code and this file should be created within the same working directory where the above .java files will be residing.

CONSOLE OUTPUT :


Related Solutions

[Point: 10] The instance of a list ADT using array is L = (10, 20, 30,...
[Point: 10] The instance of a list ADT using array is L = (10, 20, 30, 40, 50, 60). Find the output of following code segment or what is returned by each statement. remove(30);          find(50):                insert(7, 3):           findKth(4)             [Point: 5] The complexity of remove operation from a LIST ADT using array implementation is O(N). Explain why? [Point: 10] Show that the running time for the following segment of code is O(N3) without using the rule for loop. Make sure to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
Given the array a and the binary search function below, list all ACTIVATIONS IN FINDING t=19....
Given the array a and the binary search function below, list all ACTIVATIONS IN FINDING t=19. (15 Points) -9 -5 -2 0 1 3 7 11 17 19 21 25 27 31 37 41 a    index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int search(int a[], int t, int l, int r){ if(l<=r){ int m=(l+r)/2; if(t==a[m]) return m; else if (t<a[m]) return search(a, t, l,m-1); else return search(a, t, m+1,...
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
Search the Internet for one instance of a securitybreach that took advantage of each:(A)...
Search the Internet for one instance of a security breach that took advantage of each:(A) Desktop/Laptop PC with full client operating system,(B) Mobile Device with mobile OS,(C) IOT or other device with embedded operating system.Identify each operating system and at least one step that could have been taken on each of the three operating systems to protect from that attack.
python 3 We would like to add cursor-based APIs to the array-backed list API. To this...
python 3 We would like to add cursor-based APIs to the array-backed list API. To this end, we are including a cursor attribute, and two related methods. cursor_set will set the cursor to its argument index, and cursor_insert will insert its argument value into the list at the current cursor position and advance the cursor by 1. E.g, given an array-backed list l that contains the values [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], running the following...
Search the internet and find an instance of "Earnings Mismanagement" and "Fraud" in your pathway. In...
Search the internet and find an instance of "Earnings Mismanagement" and "Fraud" in your pathway. In a minimum of 3 paragraphs, tell us: The company name (in the subject line of your post). Why this company interests you. When they did it. Describe in detail what they did and how they got caught. Tell us what the consequences were to the individuals responsible. Cite at least two sources for your information.
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT