I coded a simple program that asks the user to add/delete/modify/print/etc. their contact list names. Now, I had learned a little about encapsulation. However, I still could not figure out which methods to set to private and which ones to public. Plus, in this part of the MobilePhone class,
public static void menu() {
boolean quit = false;
int choice = 0;
while (!quit) {
System.out.println("Enter your choice:");
choice = scanner.nextInt();
scanner.nextLine();
//switch case...
The program did not work without the scanner.nextLine();
part. What is the point of it? Is it because I left scanner
as an Integer input before that by typing choice = scanner.nextInt();
?
And also, if there are any other things you would like to mention about the program or the mistakes, I am open to criticism.
import java.util.Scanner;
public class MobilePhone {
private static Scanner scanner = new Scanner(System.in);
private static Contacts contactsList = new Contacts();
public static void main(String[] args) {
instructions();
menu();
}
public static void menu() {
boolean quit = false;
int choice = 0;
while (!quit) {
System.out.println("Enter your choice:");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 0:
instructions();
case 1:
quit = true;
break;
case 2:
contactsList.printContacts();
break;
case 3:
addContact();
break;
case 4:
updateContact();
break;
case 5:
removeContact();
break;
case 6:
searchContact();
break;
default:
break;
}
}
}
public static void addContact() {
System.out.println("Enter the name of the new contact");
String newContact = scanner.nextLine();
contactsList.addNewContact(newContact);
}
public static void updateContact() {
System.out.println("Enter the name of the contact that you want to replace");
String replacedName = scanner.nextLine();
System.out.println("Enter the updated name");
String newName = scanner.nextLine();
contactsList.updateContact(replacedName, newName);
}
public static void removeContact() {
System.out.println("Which contact do you wish to remove?");
String removedContact = scanner.nextLine();
contactsList.removeContact(removedContact);
}
public static void searchContact() {
System.out.println("Which contact are you looking for?");
String contactName = scanner.nextLine();
int index = contactsList.findContact(contactName);
if (index >= 0) {
System.out.println(contactsList.findContact(contactName) + 1 + ") " + contactName);
} else {
System.out.println("No such contact found.");
}
}
public static void instructions() {
System.out.println("1)Quit\n2)List of contacts\n3)Add new contact\n4}Update existing contact\n5}Remove contact\n6)Search/Find contact");
}
}
And the Contacts Class:
import java.util.ArrayList;
public class Contacts {
private ArrayList<String> contactsList = new ArrayList<>();
public void printContacts() {
System.out.println("You have " + contactsList.size() + " contacts.");
for (int i = 0; i < contactsList.size(); i++) {
System.out.println((i + 1) + ". " + contactsList.get(i));
}
}
public void addNewContact(String contact) {
contactsList.add(contact);
}
public void removeContact(String contact) {
int index = findContact(contact);
if (index >= 0) {
deleteContact(index);
} else {
System.out.println("No such contact found");
}
}
public void updateContact(String replacedContact, String newContact) {
int index = findContact(replacedContact);
if (index >= 0) {
contactsList.set(findContact(replacedContact), newContact);
} else {
System.out.println("No such contact found to update");
}
}
public int findContact(String contact) {
return contactsList.indexOf(contact);
}
private void deleteContact(int index) {
contactsList.remove(index);
}
public ArrayList<String> getContactsList() {
return contactsList;
}
}