RGAlex

Main

Apr 22nd, 2021 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. package academy.learnprograming;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     private static final Scanner scanner = new Scanner(System.in);
  7.     private static final MobilePhone mobilePhone = new MobilePhone("0044 330 4400");
  8.  
  9.  
  10.     public static void main(String[] args) {
  11.         boolean quit = false;
  12.         startPhone();
  13.         printActions();
  14.         while (!quit) {
  15.             System.out.println("\nEnter action: (to show available actions)");
  16.             int action = scanner.nextInt();
  17.             scanner.nextLine();
  18.             switch (action) {
  19.                 case 0 -> {
  20.                     System.out.println("\nShutting down...");
  21.                     quit = true;
  22.                 }
  23.                 case 1 -> mobilePhone.printContacts();
  24.                 case 2 -> addNewContact();
  25.                 case 3 -> updateContact();
  26.                 case 4 -> removeContact();
  27.                 case 5 -> queryContact();
  28.                 case 6 -> printActions();
  29.             }
  30.         }
  31.     }
  32.  
  33.     private static void addNewContact() {
  34.         System.out.println("Enter new contact name: ");
  35.         String name = scanner.nextLine();
  36.         System.out.println("Enter phone number: ");
  37.         String phone = scanner.nextLine();
  38.         Contact newContact = Contact.createContact(name, phone);
  39.         if (mobilePhone.addNewContact(newContact)) {
  40.             System.out.println("New contact added: name = " + name + ", phone = " + phone);
  41.         } else {
  42.             System.out.println("Cannot add, " + name + " Already on file.");
  43.         }
  44.     }
  45.  
  46.     private static void updateContact() {
  47.         System.out.println("Enter existing contact name: ");
  48.         String name = scanner.nextLine();
  49.         Contact existingContactRecord = mobilePhone.queryContact(name);
  50.         if (existingContactRecord == null) {
  51.             System.out.println("Contact not found.");
  52.             return;
  53.         }
  54.         System.out.println("Enter new contact name: ");
  55.         String newName = scanner.nextLine();
  56.         System.out.println("Enter new contact phone number: ");
  57.         String newNumber = scanner.nextLine();
  58.         Contact newContact = Contact.createContact(newName, newNumber);
  59.         if (mobilePhone.updateContact(existingContactRecord, newContact)) {
  60.             System.out.println("Successfully updated record.");
  61.         } else {
  62.             System.out.println("Error updating record.");
  63.         }
  64.  
  65.     }
  66.  
  67.     private static void removeContact() {
  68.         System.out.println("Enter existing contact name: ");
  69.         String name = scanner.nextLine();
  70.         Contact existingContactRecord = mobilePhone.queryContact(name);
  71.         if (existingContactRecord == null) {
  72.             System.out.println("Contact not found.");
  73.             return;
  74.         }
  75.         if (mobilePhone.removeContact(existingContactRecord)) {
  76.             System.out.println("Successfully deleted.");
  77.         } else {
  78.             System.out.println("Error deleting contact.");
  79.         }
  80.     }
  81.  
  82.     private static void queryContact() {
  83.         System.out.println("Enter existing contact name: ");
  84.         String name = scanner.nextLine();
  85.         Contact existingContactRecord = mobilePhone.queryContact(name);
  86.         if (existingContactRecord == null) {
  87.             System.out.println("Contact not found.");
  88.             return;
  89.         }
  90.         System.out.println("Name: " + existingContactRecord.getName() + " phone number is" +
  91.                 existingContactRecord.getPhoneNumber());
  92.  
  93.     }
  94.  
  95.     private static void startPhone() {
  96.         System.out.println("Starting phone...");
  97.     }
  98.  
  99.     public static void printActions() {
  100.         System.out.println("\nAvailable actions:\npress");
  101.         System.out.println("""
  102.                0 - to shutdown
  103.                1 - to print contacts
  104.                2 - to add a new contact
  105.                3 - to update an existing contact
  106.                4 - to remove an existing contact
  107.                5 - query if an existing contact
  108.                6 - to print a list of available actions.""");
  109.         System.out.println("Choose your action: ");
  110.     }
  111. }
  112.  
  113.  
Add Comment
Please, Sign In to add comment