Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. package com.timbuchalka;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6. * Created by dev on 28/08/15.
  7. */
  8. public class MobilePhone {
  9. private String myNumber;
  10. private ArrayList<Contact> myContacts;
  11.  
  12. public MobilePhone(String myNumber) {
  13. this.myNumber = myNumber;
  14. this.myContacts = new ArrayList<Contact>();
  15. }
  16.  
  17. public boolean addNewContact(Contact contact) {
  18. if(findContact(contact.getName()) >=0) {
  19. System.out.println("Contact is already on file");
  20. return false;
  21. }
  22.  
  23. myContacts.add(contact);
  24. return true;
  25.  
  26. }
  27.  
  28. public boolean updateContact(Contact oldContact, Contact newContact) {
  29. int foundPosition = findContact(oldContact);
  30. if(foundPosition <0) {
  31. System.out.println(oldContact.getName() +", was not found.");
  32. return false;
  33. }
  34.  
  35. this.myContacts.set(foundPosition, newContact);
  36. System.out.println(oldContact.getName() + ", was replaced with " + newContact.getName());
  37. return true;
  38. }
  39.  
  40. public boolean removeContact(Contact contact) {
  41. int foundPosition = findContact(contact);
  42. if(foundPosition <0) {
  43. System.out.println(contact.getName() +", was not found.");
  44. return false;
  45. }
  46. this.myContacts.remove(foundPosition);
  47. System.out.println(contact.getName() + ", was deleted.");
  48. return true;
  49. }
  50.  
  51. private int findContact(Contact contact) {
  52. return this.myContacts.indexOf(contact);
  53. }
  54.  
  55. private int findContact(String contactName) {
  56. for(int i=0; i<this.myContacts.size(); i++) {
  57. Contact contact = this.myContacts.get(i);
  58. if(contact.getName().equals(contactName)) {
  59. return i;
  60. }
  61. }
  62. return -1;
  63. }
  64.  
  65. public String queryContact(Contact contact) {
  66. if(findContact(contact) >=0) {
  67. return contact.getName();
  68. }
  69. return null;
  70. }
  71.  
  72. public Contact queryContact(String name) {
  73. int position = findContact(name);
  74. if(position >=0) {
  75. return this.myContacts.get(position);
  76. }
  77.  
  78. return null;
  79. }
  80.  
  81. public void printContacts() {
  82. System.out.println("Contact List");
  83. for(int i=0; i<this.myContacts.size(); i++) {
  84. System.out.println((i+1) + "." +
  85. this.myContacts.get(i).getName() + " -> " +
  86. this.myContacts.get(i).getPhoneNumber());
  87. }
  88.  
  89. }
  90.  
  91.  
  92. }
  93.  
  94. ---------------------------------------------------------------------------------------------------------------
  95.  
  96. package com.timbuchalka;
  97.  
  98. /**
  99. * Created by dev on 28/08/15.
  100. */
  101. public class Contact {
  102. private String name;
  103. private String phoneNumber;
  104.  
  105. public Contact(String name, String phoneNumber) {
  106. this.name = name;
  107. this.phoneNumber = phoneNumber;
  108. }
  109.  
  110. public String getName() {
  111. return name;
  112. }
  113.  
  114. public String getPhoneNumber() {
  115. return phoneNumber;
  116. }
  117.  
  118. public static Contact createContact(String name, String phoneNumber) {
  119. return new Contact(name, phoneNumber);
  120. }
  121. }
  122.  
  123. ------------------------------------------------------------------------------------
  124.  
  125.  
  126.  
  127. package com.timbuchalka;
  128.  
  129. import java.util.Scanner;
  130.  
  131. public class Main {
  132. private static Scanner scanner = new Scanner(System.in);
  133. private static MobilePhone mobilePhone = new MobilePhone("0039 330 4404");
  134.  
  135. public static void main(String[] args) {
  136. // Create a program that implements a simple mobile phone with the following capabilities.
  137. // Able to store, modify, remove and query contact names.
  138. // You will want to create a separate class for Contacts (name and phone number).
  139. // Create a master class (MobilePhone) that holds the ArrayList of Contacts
  140. // The MobilePhone class has the functionality listed above.
  141. // Add a menu of options that are available.
  142. // Options: Quit, print list of contacts, add new contact, update existing contact, remove contact
  143. // and search/find contact.
  144. // When adding or updating be sure to check if the contact already exists (use name)
  145. // Be sure not to expose the inner workings of the Arraylist to MobilePhone
  146. // e.g. no ints, no .get(i) etc
  147. // MobilePhone should do everything with Contact objects only.
  148.  
  149. boolean quit = false;
  150. startPhone();
  151. printActions();
  152. while(!quit) {
  153. System.out.println("\nEnter action: (6 to show available actions)");
  154. int action = scanner.nextInt();
  155. scanner.nextLine();
  156.  
  157. switch (action) {
  158. case 0:
  159. System.out.println("\nShutting down...");
  160. quit = true;
  161. break;
  162.  
  163. case 1:
  164. mobilePhone.printContacts();
  165. break;
  166.  
  167. case 2:
  168. addNewContact();
  169. break;
  170.  
  171. case 3:
  172. updateContact();
  173. break;
  174.  
  175. case 4:
  176. removeContact();
  177. break;
  178.  
  179. case 5:
  180. queryContact();
  181. break;
  182.  
  183. case 6:
  184. printActions();
  185. break;
  186. }
  187.  
  188. }
  189.  
  190. }
  191.  
  192. private static void addNewContact() {
  193. System.out.println("Enter new contact name: ");
  194. String name = scanner.nextLine();
  195. System.out.println("Enter phone number: ");
  196. String phone = scanner.nextLine();
  197. Contact newContact = Contact.createContact(name, phone);
  198. if(mobilePhone.addNewContact(newContact)) {
  199. System.out.println("New contact added: name = " + name + ", phone = "+ phone);
  200. } else {
  201. System.out.println("Cannot add, " + name + " already on file");
  202. }
  203. }
  204.  
  205. private static void updateContact() {
  206. System.out.println("Enter existing contact name: ");
  207. String name = scanner.nextLine();
  208. Contact existingContactRecord = mobilePhone.queryContact(name);
  209. if(existingContactRecord == null) {
  210. System.out.println("Contact not found.");
  211. return;
  212. }
  213.  
  214. System.out.print("Enter new contact name: ");
  215. String newName = scanner.nextLine();
  216. System.out.print("Enter new contact phone number: ");
  217. String newNumber = scanner.nextLine();
  218. Contact newContact = Contact.createContact(newName, newNumber);
  219. if(mobilePhone.updateContact(existingContactRecord, newContact)) {
  220. System.out.println("Successfully updated record");
  221. } else {
  222. System.out.println("Error updating record.");
  223. }
  224. }
  225.  
  226. private static void removeContact() {
  227. System.out.println("Enter existing contact name: ");
  228. String name = scanner.nextLine();
  229. Contact existingContactRecord = mobilePhone.queryContact(name);
  230. if (existingContactRecord == null) {
  231. System.out.println("Contact not found.");
  232. return;
  233. }
  234.  
  235. if(mobilePhone.removeContact(existingContactRecord)) {
  236. System.out.println("Successfully deleted");
  237. } else {
  238. System.out.println("Error deleting contact");
  239. }
  240. }
  241.  
  242. private static void queryContact() {
  243. System.out.println("Enter existing contact name: ");
  244. String name = scanner.nextLine();
  245. Contact existingContactRecord = mobilePhone.queryContact(name);
  246. if (existingContactRecord == null) {
  247. System.out.println("Contact not found.");
  248. return;
  249. }
  250.  
  251. System.out.println("Name: " + existingContactRecord.getName() + " phone number is " + existingContactRecord.getPhoneNumber());
  252. }
  253.  
  254. private static void startPhone() {
  255. System.out.println("Starting phone...");
  256. }
  257.  
  258. private static void printActions() {
  259. System.out.println("\nAvailable actions:\npress");
  260. System.out.println("0 - to shutdown\n" +
  261. "1 - to print contacts\n" +
  262. "2 - to add a new contact\n" +
  263. "3 - to update existing an existing contact\n" +
  264. "4 - to remove an existing contact\n" +
  265. "5 - query if an existing contact exists\n" +
  266. "6 - to print a list of available actions.");
  267. System.out.println("Choose your action: ");
  268. }
  269.  
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement