Question: Write a program that creates a contact list using an array. The program should contain two classes, a Contact.java class which has the following fields,

Write a program that creates a contact list using an array. The program should contain two classes, a Contact.java class which has the following fields, name, number, and address. The second class, MyContacts.java should present the user with the following options: Add, Remove, Change Order, Print, and Exit. The Add option should allow the user to enter in a new contact (name, number, and address) to the end of the list. The Remove option should print the contact list with its array index. Once a user selects an index, the contact will be deleted from the array and the other items will be moved to fill the spot. The Change Order option will print the contact list with the array index order. The user will be able to enter the numbers. The first number will be the contact they want to move and the second number will be the index to where the first item will be moved. The second contact will be moved to the index of the first. The Print option will print the contact list with the array index and the Exit option will terminate the program.

Here is my work so far:

public class Contact { public static void main(String[] args) { } private String name; private String number; private String address; public Contact(String name, String number, String address) { name = name; number = number; address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }

import java.util.*; public class MyContact { public static void main(String[] args) { final int MAX_CONTACTS = 8; Scanner sc = new Scanner(System.in); Contact[] contacts = new Contact[MAX_CONTACTS]; int status = 0; while(true) { System.out.println("Press 1 to Add, 2 to Remove, 3 to Change Order, 4 to Print, 5 to Exit"); int choice = sc.nextInt(); switch(choice) { case 1: if(status < MAX_CONTACTS) { add(contacts, status); System.out.println("This contact has been added"); status++; } else { System.out.println("You've reached the limit"); } break; case 2: if(status > 0) { remove(contacts, status); System.out.println("This contact has been deleted"); status--; } else { System.out.println("There are no contacts available"); } break; case 3: swap(contacts, status); System.out.println("Your contacts have been rearranged"); break; case 4: print(contacts, status); System.out.println("Here is your current list"); break; case 5: System.out.println("Program Terminated!"); System.exit(0); break; } } } public static void add(Contact[] contacts, int j) { Scanner sc = new Scanner(System.in); System.out.println("Enter the person's name: "); String name = sc.next(); System.out.println("Enter the person's number: "); String number = sc.next(); System.out.println("Enter the person's address: "); String address = sc.next(); Contact ct = new Contact(name, number, address); contacts[j] = ct; } public static void remove(Contact[] contacts, int n) { Scanner sc = new Scanner(System.in); print(contacts, n); System.out.println("Enter the index to remove the contact:"); int j = sc.nextInt(); if(j < 1 || j > n) { System.out.println("Invalid!"); } else { for(int i = j; i < n - 1; i++) { contacts[i] = contacts[i+1]; } } } public static void swap(Contact[] contacts, int n) { Scanner sc = new Scanner(System.in); print(contacts, n); System.out.println("Enter the first index: "); int x = sc.nextInt(); System.out.println("Enter the second index: "); int y = sc.nextInt(); if(x < 1 || x > n || y < 1 || y > n) { System.out.println("Invalid!"); } else { Contact rec = contacts[x-1]; contacts[x-1] = contacts[y-1]; contacts[y-1] = rec; } } public static void print(Contact[] contacts, int n) { for(int i = 0; i < n; i++) { System.out.print(i+1 + " " + "Name: " + contacts[i].getName() + " Number: " + contacts[i].getNumber() + " Address: " + contacts[i].getAddress()); } } }

--for the my contact program when choosing to print it does not print the previously added name, address, number, instead it displays nullName, nullNumber... etc.

I would appreciate any advice/guidance on how to make the program work.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!