package ArrayListChallenge;
public class Contact
{
private String name;
private String phoneNumber;
public Contact(String name, String phoneNumber)
{
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName()
{
return name;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public static Contact createContact(String name, String phoneNumber)
{
return new Contact(name, phoneNumber);
}
}
package ArrayListChallenge;
import java.util.ArrayList;
public class MobilePhone
{
private String myNumber;
private ArrayList<Contact> myContacts;
public MobilePhone(String myNumber)
{
this.myNumber = myNumber;
this.myContacts = new ArrayList<>();
}
public boolean addNewContact(Contact contact)
{
if (findContact(contact) >= 0)
{
System.out.println("Contact is already on file");
return false;
}
myContacts.add(contact);
return true;
}
public boolean updateContact(Contact oldContact, Contact newContact)
{
int foundPosition = findContact(oldContact);
if (foundPosition < 0)
{
System.out.println(oldContact.getName() + ", was not found.");
return false;
}
this.myContacts.set(foundPosition, newContact);
System.out.println(oldContact.getName() + ", was replaced with " + newContact.getName());
return true;
}
public boolean removeContact(Contact contact)
{
int foundPosition = findContact(contact);
if (foundPosition < 0)
{
System.out.println(contact.getName() + ", was not found.");
return false;
}
this.myContacts.remove(foundPosition);
System.out.println(contact.getName() + ", was deleted.");
return true;
}
public String queryContact(Contact contact)
{
if (findContact(contact) >= 0)
{
return contact.getName();
}
return null;
}
public Contact queryContact(String name)
{
int index = findContact(name);
if (index >= 0)
{
return this.myContacts.get(index);
}
return null;
}
private int findContact(Contact contact)
{
return this.myContacts.indexOf(contact);
}
private int findContact(String contactName)
{
for (int index = 0; index < this.myContacts.size(); index++)
{
Contact contact = this.myContacts.get(index);
if (contact.getName().equals(contactName))
{
return index;
}
}
return -1;
}
public void printContacts()
{
System.out.println("Contact List");
for (int index = 0; index < this.myContacts.size(); index++)
{
System.out.println((index + 1) + ", " + myContacts.get(index).getName() + " -> "
+ this.myContacts.get(index).getPhoneNumber());
}
}
}
package ArrayListChallenge;
import java.util.Scanner;
public class Main
{
private static Scanner scanner = new Scanner(System.in);
private static MobilePhone mobilePhone = new MobilePhone("012 345 6789");
public static void main(String[] args)
{
boolean quit = false;
startPhone();
printActions();
while (!quit)
{
System.out.println("\nEnter actions: (6 to show availe actions)");
int action = scanner.nextInt();
scanner.nextLine();
switch (action)
{
case 0:
System.out.println("\nShutting down...");
quit = true;
break;
case 1:
mobilePhone.printContacts();
break;
case 2:
addNewContact();
break;
case 3:
updateContact();
break;
case 4:
removeContact();
break;
case 5:
queryContact();
break;
case 6:
printActions();
break;
}
}
}
private static void updateContact()
{
System.out.println("Enter existing contact name: ");
String name = scanner.nextLine();
Contact existingContactRecord = mobilePhone.queryContact(name);
if (existingContactRecord == null)
{
System.out.println("Contact not found");
return;
}
System.out.println("Enter new contact name: ");
String newName = scanner.nextLine();
System.out.println("Enter new contact phone number: ");
String newNumber = scanner.nextLine();
Contact newContact = Contact.createContact(newName, newNumber);
if (mobilePhone.updateContact(existingContactRecord, newContact))
{
System.out.println("Successfully updated record");
} else
{
System.out.println("Error updating record");
}
}
private static void addNewContact()
{
System.out.println("Enter new contact name: ");
String name = scanner.nextLine();
System.out.println("Enter phone number: ");
String phoneNumber = scanner.nextLine();
Contact newContact = Contact.createContact(name, phoneNumber);
if (mobilePhone.addNewContact(newContact))
{
System.out.println("New contact added: name = " + name + ", phone = " + phoneNumber);
} else
{
System.out.println("Cannot add, " + name + " already on file");
}
}
private static void removeContact()
{
System.out.println("Enter existing contact name: ");
String name = scanner.nextLine();
Contact existingContactRecord = mobilePhone.queryContact(name);
if (existingContactRecord == null)
{
System.out.println("Contact not found");
return;
}
if (mobilePhone.removeContact(existingContactRecord))
{
System.out.println("Successfully deleted");
} else
{
System.out.println("Error deleting contact");
}
}
private static void queryContact()
{
System.out.println("Enter existing contact name: ");
String name = scanner.nextLine();
Contact existingContactRecord = mobilePhone.queryContact(name);
if (existingContactRecord == null)
{
System.out.println("Contact not found");
return;
}
System.out.println("Name: " + existingContactRecord.getName() + " phone number is "
+ existingContactRecord.getPhoneNumber());
}
private static void printActions()
{
System.out.println("\nAvailable actions:\n\npress");
System.out.println("\t0 = to shutdown\n" + "\t1 = to print contacts\n" + "\t2 = to add a new contact\n"
+ "\t3 = to update existing an existing contact\n" + "\t4 = to remove an existing contact\n"
+ "\t5 = query if an existing contact exist\n" + "\t6 = to print a list of available actions.");
System.out.println("\nChoose your action: ");
}
private static void startPhone()
{
System.out.println("Starting phone...");
}
}
# 실행 화면Available actions:press0 = to shutdown1 = to print contacts2 = to add a new contact3 = to update existing an existing contact4 = to remove an existing contact5 = query if an existing contact exist6 = to print a list of available actions.Choose your action:Enter actions: (6 to show availe actions)
댓글 영역