Add Delete Update Search Display Records Menu Driven Console Based Java Application

Add Delete Update Search Display Records Menu Driven Console Based Java Application

This java program uses console user interface to Add Delete Update Search Display Records from Java Linked List Data Structure.

SOURCE CODE

HumanResourceOffice.java


/*
 * Add Delete Update Search Display Records Menu Driven Console Based Java Application.
 * https://mauricemuteti.info/add-delete-update-search-display-records-menu-driven-console-based-java-application/
 */
package employeesrecordslinkedlist;

import java.util.LinkedList;
import java.util.Scanner;

/**
 *
 * @author HP
 */
public class HumanResourceOffice {

    // creating an empty LinkedList
    LinkedList<Record> list;

    /**
     * Default Constructor.
     */
    public HumanResourceOffice() {
        list = new LinkedList<>();
    }

    /**
     * Add Record.
     *
     * @param record
     */
    public void add(Record record) {
        //Check if a record already exists or not, 
        //if not add it to Record list, Otherwise
        //error dispaly message
        if (!find(record.getIdNumber())) {
            list.add(record);

        } else {
            System.out.println("Record already exists in the Record list");
        }
    }

    /**
     * Search Record.
     *
     * @param idNimber
     * @return
     */
    public boolean find(int idNimber) {

        // Iterate Record list
        for (Record l : list) {
            // Check record by id Number
            if (l.getIdNumber() == idNimber) {
                System.out.println(l);
                return true;
            }
        }
        return false;
    }

    /**
     * Delete Record.
     *
     * @param recIdNumber
     */
    public void delete(int recIdNumber) {
        Record recordDel = null;
        // Iterate Record list.
        for (Record ll : list) {
            //Find record to be deleted by id Number.
            if (ll.getIdNumber() == recIdNumber) {
                recordDel = ll;
            }
        }
        //If recordDel is null, then show error message, 
        //otherwise remove the record from Record list.
        if (recordDel == null) {
            System.out.println("Invalid record Id");
        } else {
            list.remove(recordDel);
            System.out.println("Successfully removed record from the list");
        }
    }

    /**
     * Find Record.
     *
     * @param idNumber
     * @return
     */
    public Record findRecord(int idNumber) {
        // Iterate Record list
        for (Record l : list) {
            // Check record by id Number.
            if (l.getIdNumber() == idNumber) {
                return l;
            }
        }
        return null;
    }

    /**
     * Update Record.
     *
     * @param id
     * @param input
     */
    public void update(int id, Scanner input) {

        if (find(id)) {
            Record rec = findRecord(id);

            System.out.print("What is the new record id Number (integer value)? ");
            int idNumber = input.nextInt();
            System.out.print("What is the new record contact Number (integer value)? ");
            int contactNumber = input.nextInt();
            input.nextLine();
            System.out.print("What is the new record Name ? ");
            String name = input.nextLine();

            rec.setIdNumber(idNumber);
            rec.setName(name);
            rec.setContactNumber(contactNumber);
            System.out.println("Record Updated Successfully");
        } else {
            System.out.println("Record Not Found in the Record list");
        }
    }

    /**
     * Display Records
     */
    public void display() {
        // If Record list is empty then print the message below.
        if (list.isEmpty()) {
            System.out.println("The list has no records\n");
        }
        // Iterate Record list.
        for (Record record : list) {
            // Print list.
            System.out.println(record.toString());
        }
    }

}



Record.java

/*
 * Add Delete Update Search Display Records Menu Driven Console Based Java Application.
 * https://mauricemuteti.info/add-delete-update-search-display-records-menu-driven-console-based-java-application/
 */
package employeesrecordslinkedlist;

/**
 *
 * @author HP
 */
public class Record {

    //Instance Variables.
    private String name;
    private int idNumber;
    private int contactNumber;

    /**
     * Default Constructor.
     */
    public Record() {
    }

    /**
     * Parameterized Constructor.
     *
     * @param name
     * @param idNumber
     * @param contactNumber
     */
    public Record(String name, int idNumber, int contactNumber) {
        this.name = name;
        this.idNumber = idNumber;
        this.contactNumber = contactNumber;
    }

    /**
     * Get the value of contactNumber
     *
     * @return the value of contactNumber
     */
    public int getContactNumber() {
        return contactNumber;
    }

    /**
     * Set the value of contactNumber
     *
     * @param contactNumber new value of contactNumber
     */
    public void setContactNumber(int contactNumber) {
        this.contactNumber = contactNumber;
    }

    /**
     * Get the value of idNumber
     *
     * @return the value of idNumber
     */
    public int getIdNumber() {
        return idNumber;
    }

    /**
     * Set the value of idNumber
     *
     * @param idNumber new value of idNumber
     */
    public void setIdNumber(int idNumber) {
        this.idNumber = idNumber;
    }

    /**
     * Get the value of name
     *
     * @return the value of name
     */
    public String getName() {
        return name;
    }

    /**
     * Set the value of name
     *
     * @param name new value of name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * toString Method.
     *
     * @return
     */
    @Override
    public String toString() {
        return "Records{" + "name=" + name + ", idNumber=" + idNumber + ", contactNumber=" + contactNumber + '}';
    }

}

EmployeesRecordsLinkedListDriver.java

/*
 * Add Delete Update Search Display Records Menu Driven Console Based Java Application.
 * https://mauricemuteti.info/add-delete-update-search-display-records-menu-driven-console-based-java-application/
 */
package employeesrecordslinkedlist;

import java.util.Scanner;

/**
 *
 * @author HP
 */
public class EmployeesRecordsLinkedListDriver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        //Creating HumanResourceOffice Object.
        HumanResourceOffice hr = new HumanResourceOffice();

        Record record = new Record();
        //Hard Coded record on the program. 
        //it serves as the initial Employee record:
        //Using mutators to had code the data.
        record.setIdNumber(69420);
        record.setContactNumber(123456789);
        record.setName("Kael Ramos");
        // Call add record, to add static data/(Hard Coded Data) to linked List. 
        hr.add(record);

        //Creating Scanner Object.
        Scanner input = new Scanner(System.in);

        //Creating Option Integer Variable.
        int option = 0;
        //Do - While loop
        do {
            menu();
            option = input.nextInt();

            switch (option) {
                case 1:
                    System.out.print("What is the record id Number (integer value)? ");
                    int idNumber = input.nextInt();
                    System.out.print("What is the record contact Number (integer value)? ");
                    int contactNumber = input.nextInt();
                    input.nextLine();
                    System.out.print("What is the Record Name ? ");
                    String name = input.nextLine();
                    // Create record object and pass constructor parameters.
                    record = new Record(name, idNumber, contactNumber);
                    // Call add record
                    hr.add(record);
                    System.out.println(record.toString());
                    break;
                case 2:
                    System.out.print("What is the record id number(integer value)? ");
                    int rId = input.nextInt();
                    // invoke remove/delete record
                    hr.delete(rId);
                    break;
                case 3:
                    System.out.print("What is the record id number(integer value)? ");
                    int rIdNo = input.nextInt();
                    hr.update(rIdNo, input);
                    break;

                case 4:
                    System.out.print("What is the record id (integer value)? ");
                    int bookId = input.nextInt();

                    if (!hr.find(bookId)) {
                        System.out.println("Record id does not exist\n");
                    }

                    break;
                case 5:
                    hr.display();
                    break;
                case 9:
                    System.out.println("\nThank you for using the program. Goodbye!\n");
                    System.exit(0);
                    break;
                default:
                    System.out.println("\nInvalid input\n");
                    break;

            }
        } while (option != 9);
    }

    /**
     * Menu - Static menu for displaying options.
     */
    public static void menu() {
        System.out.println("MENU");
        System.out.println("1: Add Record");
        System.out.println("2: Delete Record");
        System.out.println("3: Update Record");
        System.out.println("4: Search Record");
        System.out.println("5: Display Records");
        System.out.println("9: Exit program");
        System.out.print("Enter your selection : ");
    }

}

NETBEANS IDE OUTPUT

run:
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 5
Records{name=Kael Ramos, idNumber=69420, contactNumber=123456789}
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 1
What is the record id Number (integer value)? 12345
What is the record contact Number (integer value)? 123456789
What is the Record Name ? Jane Doe
Records{name=Jane Doe, idNumber=12345, contactNumber=123456789}
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 1
What is the record id Number (integer value)? 321
What is the record contact Number (integer value)? 987654321
What is the Record Name ? John Doe
Records{name=John Doe, idNumber=321, contactNumber=987654321}
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 5
Records{name=Kael Ramos, idNumber=69420, contactNumber=123456789}
Records{name=Jane Doe, idNumber=12345, contactNumber=123456789}
Records{name=John Doe, idNumber=321, contactNumber=987654321}
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 4
What is the record id (integer value)? 321
Records{name=John Doe, idNumber=321, contactNumber=987654321}
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 4
What is the record id (integer value)? 11
Record id does not exist

MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 11

Invalid input

MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 2
What is the record id number(integer value)? 11
Invalid record Id
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 2
What is the record id number(integer value)? 321
Successfully removed record from the list
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 5
Records{name=Kael Ramos, idNumber=69420, contactNumber=123456789}
Records{name=Jane Doe, idNumber=12345, contactNumber=123456789}
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 3
What is the record id number(integer value)? 123455
Record Not Found in the Record list
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program


NETBEANES IDE OUTPUT 2

run:
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 1
What is the record id Number (integer value)? 1
What is the record contact Number (integer value)? 123454321
What is the Record Name ? George William
Records{name=George William, idNumber=1, contactNumber=123454321}
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 3
What is the record id number(integer value)? 1
Records{name=George William, idNumber=1, contactNumber=123454321}
What is the new record id Number (integer value)? 1
What is the new record contact Number (integer value)? 12345555
What is the new record Name ? Lilian Mary
Record Updated Successfully
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 4
What is the record id (integer value)? 1
Records{name=Lilian Mary, idNumber=1, contactNumber=12345555}
MENU
1: Add Record
2: Delete Record
3: Update Record
4: Search Record
5: Display Records
9: Exit program
Enter your selection : 9

Thank you for using the program. Goodbye!

BUILD SUCCESSFUL (total time: 57 seconds)

NETBEANS OUTPUT SCREENSHOT

Leave a Reply

Your email address will not be published. Required fields are marked *