Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. package com.timbuchalka;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6. private static Scanner scanner = new Scanner(System.in);
  7. private static MobilePhone mobilePhone = new MobilePhone("0039 330 4404");
  8.  
  9. public static void main(String[] args) {
  10. // Create a program that implements a simple mobile phone with the following capabilities.
  11. // Able to store, modify, remove and query contact names.
  12. // You will want to create a separate class for Contacts (name and phone number).
  13. // Create a master class (MobilePhone) that holds the ArrayList of Contacts
  14. // The MobilePhone class has the functionality listed above.
  15. // Add a menu of options that are available.
  16. // Options: Quit, print list of contacts, add new contact, update existing contact, remove contact
  17. // and search/find contact.
  18. // When adding or updating be sure to check if the contact already exists (use name)
  19. // Be sure not to expose the inner workings of the Arraylist to MobilePhone
  20. // e.g. no ints, no .get(i) etc
  21. // MobilePhone should do everything with Contact objects only.
  22.  
  23. boolean quit = false;
  24. startPhone();
  25. printActions();
  26. while(!quit) {
  27. System.out.println("\nEnter action: (6 to show available actions)");
  28. int action = scanner.nextInt();
  29. scanner.nextLine();
  30.  
  31. switch (action) {
  32. case 0:
  33. System.out.println("\nShutting down...");
  34. quit = true;
  35. break;
  36.  
  37. case 1:
  38. mobilePhone.printContacts();
  39. break;
  40.  
  41. case 2:
  42. addNewContact();
  43. break;
  44.  
  45. case 3:
  46. updateContact();
  47. break;
  48.  
  49. case 4:
  50. removeContact();
  51. break;
  52.  
  53. case 5:
  54. queryContact();
  55. break;
  56.  
  57. case 6:
  58. printActions();
  59. break;
  60. }
  61.  
  62. }
  63.  
  64. }
  65.  
  66. private static void addNewContact() {
  67. System.out.println("Enter new contact name: ");
  68. String name = scanner.nextLine();
  69. System.out.println("Enter phone number: ");
  70. String phone = scanner.nextLine();
  71. Contact newContact = Contact.createContact(name, phone);
  72. if(mobilePhone.addNewContact(newContact)) {
  73. System.out.println("New contact added: name = " + name + ", phone = "+ phone);
  74. } else {
  75. System.out.println("Cannot add, " + name + " already on file");
  76. }
  77. }
  78.  
  79. private static void updateContact() {
  80. System.out.println("Enter existing contact name: ");
  81. String name = scanner.nextLine();
  82. Contact existingContactRecord = mobilePhone.queryContact(name);
  83. if(existingContactRecord == null) {
  84. System.out.println("Contact not found.");
  85. return;
  86. }
  87.  
  88. System.out.print("Enter new contact name: ");
  89. String newName = scanner.nextLine();
  90. System.out.print("Enter new contact phone number: ");
  91. String newNumber = scanner.nextLine();
  92. Contact newContact = Contact.createContact(newName, newNumber);
  93. if(mobilePhone.updateContact(existingContactRecord, newContact)) {
  94. System.out.println("Successfully updated record");
  95. } else {
  96. System.out.println("Error updating record.");
  97. }
  98. }
  99.  
  100. private static void removeContact() {
  101. System.out.println("Enter existing contact name: ");
  102. String name = scanner.nextLine();
  103. Contact existingContactRecord = mobilePhone.queryContact(name);
  104. if (existingContactRecord == null) {
  105. System.out.println("Contact not found.");
  106. return;
  107. }
  108.  
  109. if(mobilePhone.removeContact(existingContactRecord)) {
  110. System.out.println("Successfully deleted");
  111. } else {
  112. System.out.println("Error deleting contact");
  113. }
  114. }
  115.  
  116. private static void queryContact() {
  117. System.out.println("Enter existing contact name: ");
  118. String name = scanner.nextLine();
  119. Contact existingContactRecord = mobilePhone.queryContact(name);
  120. if (existingContactRecord == null) {
  121. System.out.println("Contact not found.");
  122. return;
  123. }
  124.  
  125. System.out.println("Name: " + existingContactRecord.getName() + " phone number is " + existingContactRecord.getPhoneNumber());
  126. }
  127.  
  128. private static void startPhone() {
  129. System.out.println("Starting phone...");
  130. }
  131.  
  132. private static void printActions() {
  133. System.out.println("\nAvailable actions:\npress");
  134. System.out.println("0 - to shutdown\n" +
  135. "1 - to print contacts\n" +
  136. "2 - to add a new contact\n" +
  137. "3 - to update existing an existing contact\n" +
  138. "4 - to remove an existing contact\n" +
  139. "5 - query if an existing contact exists\n" +
  140. "6 - to print a list of available actions.");
  141. System.out.println("Choose your action: ");
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement