Hyperskill Contacts Project Stage 2- Main.java

mail@pastecode.io avatar
unknown
java
4 years ago
3.9 kB
2
Indexable
Never
package contacts;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //System.out.println("Hello World!");
        //Have a single instance of Contact class:
        Contact con = new Contact();
        var scan = new Scanner(System.in);
        String userInput = "";
        while (!userInput.equals("exit")){
            System.out.println("Enter action (add, remove, edit, count, list, exit):");
            userInput = scan.nextLine();
            switch (userInput) {
                case "count":
                {
                    int result = con.count();
                    System.out.println("The Phone Book has " + result + " records.");
                    break;
                }

                case "list": {
                    con.listRecords();
                    break;
                }

                case "add": {
                    String name, surname, number;
                    System.out.println("Enter the name: ");
                    name = scan.nextLine();
                    System.out.println("Enter the surname: ");
                    surname = scan.nextLine();
                    System.out.println("Enter the number: ");
                    number = scan.nextLine();
                    var record = new Record(name, surname, number);
                    con.addRecord(record);
                    System.out.println("The record added.");
                    break;
                }

                case "remove": {
                    if (con.count() == 0) {
                        System.out.println("No records to remove!");
                    }
                    else {
                        con.listRecords();
                        System.out.println("Select a record:");
                        int input = scan.nextInt();
                        scan.nextLine();
                        con.removeRecord(input);
                        System.out.println("The record removed!");
                    }
                }

                case "edit": {
                    if (con.count() == 0) {
                        System.out.println("No records to edit!");

                    } else {
                        con.listRecords();
                        int recordIndex;
                        System.out.println("Select a record: ");
                        recordIndex = scan.nextInt();
                        scan.nextLine();
                        Record rec = con.getRecord(recordIndex);
                        System.out.println("Select a field (name, surname, number): ");
                        String inp = scan.nextLine();
                        switch(inp) {
                            
                            case "name": {
                                System.out.println("Enter name: ");
                                String val = scan.nextLine();
                                rec.setName(val);
                                break;
                            }

                            case "surname": {
                                System.out.println("Enter surname: ");
                                String val = scan.nextLine();
                                rec.setSurname(val);
                                break;
                            }

                            case "number": {
                                System.out.println("Enter number: ");
                                String val = scan.nextLine();
                                rec.setNumber(val);
                                break;
                            }
                        }
                        System.out.println("The record updated!");
                    }
                }

                default: {
                    break;
                }
            }
        }
    }


}