Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package academy.learnprograming;
- import java.util.Scanner;
- public class Main {
- private static final Scanner scanner = new Scanner(System.in);
- private static final MobilePhone mobilePhone = new MobilePhone("0044 330 4400");
- public static void main(String[] args) {
- boolean quit = false;
- startPhone();
- printActions();
- while (!quit) {
- System.out.println("\nEnter action: (to show available actions)");
- int action = scanner.nextInt();
- scanner.nextLine();
- switch (action) {
- case 0 -> {
- System.out.println("\nShutting down...");
- quit = true;
- }
- case 1 -> mobilePhone.printContacts();
- case 2 -> addNewContact();
- case 3 -> updateContact();
- case 4 -> removeContact();
- case 5 -> queryContact();
- case 6 -> printActions();
- }
- }
- }
- 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.println("Enter new contact name: ");
- String newName = scanner.nextLine();
- System.out.println("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...");
- }
- public static void printActions() {
- System.out.println("\nAvailable actions:\npress");
- System.out.println("""
- 0 - to shutdown
- 1 - to print contacts
- 2 - to add a new contact
- 3 - to update an existing contact
- 4 - to remove an existing contact
- 5 - query if an existing contact
- 6 - to print a list of available actions.""");
- System.out.println("Choose your action: ");
- }
- }
Add Comment
Please, Sign In to add comment