Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Contact {
  4. private String name;
  5. private String phoneNumber;
  6.  
  7. public Contact(String name, String phoneNumber) {
  8. this.name = name;
  9. this.phoneNumber = phoneNumber;
  10. }
  11.  
  12. public String getName() {
  13. return name;
  14. }
  15.  
  16. public String getPhoneNumber() {
  17. return phoneNumber;
  18. }
  19. public static Contact createContact(String name, String phoneNumber){
  20. return new Contact(name,phoneNumber);
  21. }
  22.  
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26.  
  27. public void setPhoneNumber(String phoneNumber) {
  28. this.phoneNumber = phoneNumber;
  29. }
  30. }
  31. package com.company;
  32.  
  33. import java.util.ArrayList;
  34.  
  35. public class MobilePhone {
  36. private String myNumber;
  37. private ArrayList<Contact> myContacts;
  38. public MobilePhone(String phoneNumber){
  39. this.myNumber = phoneNumber;
  40. this.myContacts = new ArrayList<>();
  41. }
  42.  
  43. public boolean addNewContact(Contact contact) {
  44. if (findContact(contact.getName()) >=0){
  45. System.out.println("Contact is already on name");
  46. System.out.println("The contact that is already on name has index " + findContact(contact.getName()));
  47. return false;
  48. } else {
  49. myContacts.add(contact);
  50. System.out.println("A new contact was added to the list");
  51. return true;
  52.  
  53. }
  54. }
  55.  
  56. public boolean removeContact(Contact contact) {
  57. if(findContact(contact) >=0){
  58. System.out.println(contact.getName() + "was removed from the contact list");
  59. this.myContacts.remove(contact);
  60. return true;
  61. }else{
  62. System.out.println("Contact to be removed was not found in the contact list");
  63. return false;
  64. }
  65.  
  66. }
  67.  
  68. private int findContact(Contact contact) {
  69. return this.myContacts.indexOf(contact);
  70. }
  71. public int findContact(String contactName){
  72. boolean foundMyContact = false;
  73.  
  74. for(int i = 0; i < this.myContacts.size(); i++){
  75. foundMyContact = true;
  76. Contact contact = this.myContacts.get(i);
  77. if (contact.getName().equals(contactName)){
  78.  
  79. return i;
  80.  
  81.  
  82. }
  83.  
  84. }
  85.  
  86. return -1;
  87.  
  88. }
  89.  
  90. public int getMobilePhoneLength() {
  91. return this.myContacts.size();
  92. }
  93. public String queryContact(Contact contact){
  94. if(findContact(contact) >= 0){
  95. return contact.getName();
  96. } else return null;
  97.  
  98. }
  99. public boolean updateContact(Contact oldContact, Contact newContact){
  100. int foundPosition = findContact(oldContact);
  101. if(foundPosition < 0){
  102. System.out.println("Old contact.getName was not found");
  103. return false;
  104.  
  105.  
  106. }
  107. myContacts.set(foundPosition,newContact);
  108. return true;
  109. }
  110.  
  111. public String getMyNumber() {
  112. return myNumber;
  113. }
  114.  
  115. public ArrayList<Contact> getMyContacts() {
  116. return myContacts;
  117. }
  118. public String[] myStringArray(){
  119. Contact[] myContactString = new Contact[myContacts.size()];
  120. for(int i = 0; i < myContactString.length; i++){
  121. myContactString[i] = myContacts.get(i);
  122. }
  123. String[] myStringArray2;
  124. myStringArray2 = new String[myContactString.length];
  125. for(int i = 0; i < myContactString.length; i++){
  126. myStringArray2[i] = myContacts.get(i).getName();
  127. }
  128. return myStringArray2;
  129.  
  130.  
  131. }
  132. public void printContacts(){
  133. for (int i = 0; i < myContacts.size(); i++){
  134. System.out.println("Contact name is " + myContacts.get(i).getName());
  135.  
  136. }
  137.  
  138. }
  139.  
  140. }
  141. package com.company;
  142.  
  143. import java.util.Scanner;
  144.  
  145. public class Main {
  146. private static Scanner scanner = new Scanner(System.in);
  147. public static MobilePhone mobilePhone = new MobilePhone("07885599015");
  148.  
  149.  
  150. public static void main(String[] args) {
  151. boolean quit = false;
  152.  
  153. startPhone();
  154. printActions();
  155.  
  156. while(!quit){
  157. System.out.println("\n Enter action: (6 to show available actions)");
  158. int action = scanner.nextInt();
  159. if (action >= 0 && action < 7){
  160. switch(action){
  161. case 0:
  162. System.out.println("\nShutting down");
  163. break;
  164. case 1:
  165. printContacts();
  166. break;
  167. case 2:
  168. addContacts();
  169. break;
  170. case 3:
  171. updateContact();
  172. break;
  173. case 4:
  174. removeContact();
  175. break;
  176. case 5:
  177. queryContact();
  178. break;
  179. case 6:
  180. printActions();
  181.  
  182.  
  183.  
  184. }
  185.  
  186. }
  187. else{
  188. System.out.println("Invalid input");
  189. }
  190.  
  191.  
  192. }
  193.  
  194.  
  195. }
  196. private static void startPhone(){
  197. System.out.println("Starting phone.......");
  198. }
  199. private static void printContacts(){
  200. mobilePhone.printContacts();
  201. }
  202.  
  203. private static void printActions(){
  204. System.out.println("\nAvailable actions:\npress");
  205. System.out.println("0 - to shut down\n"
  206. + "1 - to print contacts\n"
  207. + "2 - to add a new contact\n"
  208. + "3 - to update an existing contact\n"
  209. + "4 - to remove an existing contact\n"
  210. + "5 - to query whether an an input exists\n"
  211. + "6 - to print a list of existing actions\n"
  212. );
  213. }
  214. private static void addContacts(){
  215. System.out.println("Enter the name of the contact you would like to add...");
  216. String contactName = scanner.nextLine();
  217. scanner.nextLine();
  218. System.out.println("Enter the phone number of the contact you would like to add...");
  219. String phoneNumber = scanner.nextLine();
  220.  
  221. Contact contact = new Contact(contactName,phoneNumber);
  222. mobilePhone.addNewContact(contact);
  223. }
  224.  
  225. private static void removeContact(){
  226. System.out.println("Enter the name of the contact you would like to remove");
  227. String removedContactName = scanner.nextLine();
  228. int indexOfRemovedContact = mobilePhone.findContact(removedContactName);
  229. if(indexOfRemovedContact >=0){
  230. Contact contactToBeRemoved = mobilePhone.getMyContacts().get(indexOfRemovedContact);
  231. mobilePhone.removeContact(contactToBeRemoved);
  232. System.out.println("The mobile phone contact with name " + removedContactName + " has been removed");
  233.  
  234. }
  235. else{
  236. System.out.println("The name of the contact you have inputted is not in the contact list");
  237. }
  238.  
  239. }
  240. private static void updateContact(){
  241. System.out.println("Enter the name of the contact you would like to update...");
  242. String modifiedContactName = scanner.nextLine();
  243. int indexOfModifiedContact = mobilePhone.findContact(modifiedContactName);
  244. scanner.nextLine();
  245. System.out.println(indexOfModifiedContact);
  246.  
  247. System.out.println("Would you like to change the name of the contact you would like to change? (input yes or no)");
  248. String BooleanOfName = scanner.nextLine();
  249. BooleanOfName.toLowerCase();
  250. System.out.println(BooleanOfName);
  251. if (BooleanOfName.equals("yes")){
  252. System.out.println("Enter the new name of the contact");
  253. String newContactName = scanner.nextLine();
  254. mobilePhone.getMyContacts().get(indexOfModifiedContact + 1).setName(newContactName);
  255.  
  256.  
  257.  
  258. }
  259. else if (BooleanOfName == "no"){
  260. System.out.println("You do not want to change the inputted contact");
  261.  
  262. } else {
  263.  
  264. System.out.println("You did not enter either yes or no");
  265.  
  266. }
  267. System.out.println("Would you like to change the phone number of the contact you would like to change?(input YES or NO ");
  268. String BooleanOfName2 = scanner.nextLine();
  269. BooleanOfName2.toLowerCase();
  270. if (BooleanOfName2 == "Yes"){
  271. System.out.println("Enter the new phone number of the contact");
  272. String newPhoneNumber = scanner.nextLine();
  273. mobilePhone.getMyContacts().get(indexOfModifiedContact).setPhoneNumber(newPhoneNumber);
  274. }
  275. if (BooleanOfName2.equals("no")){
  276. System.out.println(BooleanOfName2 + " Was not edited");
  277. }
  278.  
  279. }
  280. private static void queryContact(){
  281. System.out.println("Enter the name of the contact you would like to query");
  282. String queryContactName = scanner.nextLine();
  283. scanner.nextLine();
  284. int indexOfQueriedContact = mobilePhone.findContact(queryContactName);
  285. System.out.println(indexOfQueriedContact);
  286. if(indexOfQueriedContact >=0){
  287. System.out.println("The item you entered exists in the list and has position " + indexOfQueriedContact);
  288.  
  289. }else {
  290. System.out.println("The item you entered does not exist in the contact list");
  291. }
  292.  
  293. }
  294.  
  295.  
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement