Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.timbuchalka;
- import java.util.ArrayList;
- /**
- * Created by dev on 28/08/15.
- */
- public class MobilePhone {
- private String myNumber;
- private ArrayList<Contact> myContacts;
- public MobilePhone(String myNumber) {
- this.myNumber = myNumber;
- this.myContacts = new ArrayList<Contact>();
- }
- public boolean addNewContact(Contact contact) {
- if(findContact(contact.getName()) >=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;
- }
- private int findContact(Contact contact) {
- return this.myContacts.indexOf(contact);
- }
- private int findContact(String contactName) {
- for(int i=0; i<this.myContacts.size(); i++) {
- Contact contact = this.myContacts.get(i);
- if(contact.getName().equals(contactName)) {
- return i;
- }
- }
- return -1;
- }
- public String queryContact(Contact contact) {
- if(findContact(contact) >=0) {
- return contact.getName();
- }
- return null;
- }
- public Contact queryContact(String name) {
- int position = findContact(name);
- if(position >=0) {
- return this.myContacts.get(position);
- }
- return null;
- }
- public void printContacts() {
- System.out.println("Contact List");
- for(int i=0; i<this.myContacts.size(); i++) {
- System.out.println((i+1) + "." +
- this.myContacts.get(i).getName() + " -> " +
- this.myContacts.get(i).getPhoneNumber());
- }
- }
- }
- ---------------------------------------------------------------------------------------------------------------
- package com.timbuchalka;
- /**
- * Created by dev on 28/08/15.
- */
- 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 com.timbuchalka;
- import java.util.Scanner;
- public class Main {
- private static Scanner scanner = new Scanner(System.in);
- private static MobilePhone mobilePhone = new MobilePhone("0039 330 4404");
- public static void main(String[] args) {
- // Create a program that implements a simple mobile phone with the following capabilities.
- // Able to store, modify, remove and query contact names.
- // You will want to create a separate class for Contacts (name and phone number).
- // Create a master class (MobilePhone) that holds the ArrayList of Contacts
- // The MobilePhone class has the functionality listed above.
- // Add a menu of options that are available.
- // Options: Quit, print list of contacts, add new contact, update existing contact, remove contact
- // and search/find contact.
- // When adding or updating be sure to check if the contact already exists (use name)
- // Be sure not to expose the inner workings of the Arraylist to MobilePhone
- // e.g. no ints, no .get(i) etc
- // MobilePhone should do everything with Contact objects only.
- boolean quit = false;
- startPhone();
- printActions();
- while(!quit) {
- System.out.println("\nEnter action: (6 to show available 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 addNewContact() {
- System.out.println("Enter new contact name: ");
- String name = scanner.nextLine();
- System.out.println("Enter phone number: ");
- String phone = scanner.nextLine();
- Contact newContact = Contact.createContact(name, phone);
- if(mobilePhone.addNewContact(newContact)) {
- System.out.println("New contact added: name = " + name + ", phone = "+ phone);
- } else {
- System.out.println("Cannot add, " + name + " already on file");
- }
- }
- 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.print("Enter new contact name: ");
- String newName = scanner.nextLine();
- System.out.print("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 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 startPhone() {
- System.out.println("Starting phone...");
- }
- private static void printActions() {
- System.out.println("\nAvailable actions:\npress");
- System.out.println("0 - to shutdown\n" +
- "1 - to print contacts\n" +
- "2 - to add a new contact\n" +
- "3 - to update existing an existing contact\n" +
- "4 - to remove an existing contact\n" +
- "5 - query if an existing contact exists\n" +
- "6 - to print a list of available actions.");
- System.out.println("Choose your action: ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement