I'm self teaching programming via text I've found online and a course on Udemy. We had a challenge today to make a simple cell phone interface to add, remove, modify, etc. At this point I have learned the basic OOP principles, arrays, arraylists, and recently input.
I was wondering if someone could check my basic code and tell me if I'm doing anything terribly wrong. It's difficult to know if my custom answers to challenge problems are going okay. I'm trying to get the principles of OOP down well and this took me a bit to figure out on some parts.
MobilePhone.java
package com.company;
import java.util.Scanner;
import java.util.ArrayList;
public class MobilePhone {
private static Scanner scannerStr = new Scanner(System.in);
private ArrayList<Contacts> myMobilePhone = new ArrayList<Contacts>();
public void addContacts(Contacts contact) {
myMobilePhone.add(contact);
}
public void printContacts() {
System.out.println("Your contact list has " + myMobilePhone.size() + " contacts");
for (int i = 0; i < myMobilePhone.size(); i++) {
System.out.println(i + 1 + ". " + myMobilePhone.get(i));
}
}
private int removeContacts(String name, String num) {
for (int i = 0; i < myMobilePhone.size(); i++) {
Contacts contact = this.myMobilePhone.get(i);
if (contact.getName().equals(name) && contact.getPhoneNumber().equals(num)) {
return i;
}
}
return -1;
}
public void removeTheContacts(String name, String num) {
int position = removeContacts(name, num);
if (position >= 0) {
myMobilePhone.remove(position);
System.out.println("The contact " + name + ", " + num + " was removed");
} else
System.out.println("The contact doesn't exist");
}
private int findContactName(String name) {
for (int i = 0; i < myMobilePhone.size(); i++) {
Contacts contact = this.myMobilePhone.get(i);
if (contact.getName().equals(name)) {
return i;
}
}
return -1;
}
public void findContacts(String name) {
int position = findContactName(name);
if (position >= 0)
System.out.println("Contact " + name + " was found");
else
System.out.println("Contact " + name + " was not found");
}
public void modifyContacts(String name, String number) {
int position = findContactName(name);
if (position >= 0) {
System.out.println("Enter a new name");
String newName = scannerStr.nextLine();
System.out.println("Enter a new phone number");
String newNumber = scannerStr.nextLine();
modifyContacts(name, newName, newNumber);
} else
System.out.println("The contact doesn't exit");
}
private Contacts modifyContacts(String oldName, String newName, String newNumber) {
int position = findContactName(oldName);
Contacts contact = new Contacts(newName, newNumber);
myMobilePhone.set(position, contact);
return contact;
}
}
Contacts.java
public class Contacts {
private String name;
private String phoneNumber;
public Contacts(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
@Override
public String toString() {
return "Contact: " + getName() + ", " + getPhoneNumber();
}
}
Main.java
import com.company.Contacts;
import com.company.MobilePhone;
import java.util.Scanner;
public class Main {
private static Scanner scannerInt = new Scanner(System.in);
private static Scanner scannerStr = new Scanner(System.in);
private static MobilePhone mobilePhone = new MobilePhone();
public static void main(String[] args) {
printInstructions();
boolean quit = false;
int choice;
while(!quit){
choice = scannerInt.nextInt();
switch(choice) {
case 0:
printInstructions();
break;
case 1:
printContacts();
break;
case 2:
addContacts();
break;
case 3:
removeContacts();
break;
case 4:
modifyContacts();
break;
case 5:
searchContacts();
break;
case 6:
quit = true;
break;
}
}
}
private static void printInstructions(){
System.out.println("Please choose from the following options: ");
System.out.println("\t Press 0 to print instructions again");
System.out.println("\t Press 1 to show your contacts");
System.out.println("\t Press 2 to add a contact");
System.out.println("\t Press 3 to remove a contact");
System.out.println("\t Press 4 to update a contact");
System.out.println("\t Press 5 to search for a contact");
System.out.println("\t Press 6 to quit");
}
private static void printContacts(){
mobilePhone.printContacts();
}
private static void removeContacts(){
System.out.print("Enter the name for the contact you want to remove: ");
String name = scannerStr.nextLine();
System.out.print("Enter the number for the same contact you wish to remove: ");
String number = scannerStr.nextLine();
mobilePhone.removeTheContacts(name, number);
}
private static Contacts addContacts(){
System.out.print("Enter the name for the contact you want to add: ");
String name = scannerStr.nextLine();
System.out.print("Enter the number for the same contact you wish to add: ");
String number = scannerStr.nextLine();
Contacts contact = new Contacts(name, number);
mobilePhone.addContacts(contact);
System.out.println("Contact " + name + ", " + number + " was added!");
return contact;
}
private static void searchContacts (){
System.out.print("Enter the name for the contact you want to find: ");
String name = scannerStr.nextLine();
mobilePhone.findContacts(name);
}
private static void modifyContacts() {
System.out.print("Enter the name for the contact you want to change: ");
String name = scannerStr.nextLine();
System.out.print("Enter the number for the same contact you wish to change: ");
String number = scannerStr.nextLine();
mobilePhone.modifyContacts(name, number);
}
}