2
\$\begingroup\$

I'm creating a DataBase for looking up employee's different stats in a company.

Is there any way I can make this more compact/efficient?

/**
 * Write a description of class DataBase here.
 *
 * @author (name)
 * @version (date)
 */
import java.util.Scanner;
import java.util.Arrays;
public class DataBase {
    public static void main(String args[]) {
        System.out.println('\u000C');
        int y = 1;
        while (y == 1) {
        System.out.println("Enter employee name from list: ");
        System.out.println("\n");
        String[] employee = new String[3];
            employee[0] = "Dave Johnson";
            employee[1] = "Hugh Man";
            employee[2] = "Ethan Isakson";
        System.out.println(Arrays.toString(employee));
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String [] Inquiry = new String[4];
            Inquiry[0] = "Job";
            Inquiry[1] = "Salary";
            Inquiry[2] = "Employed";
            Inquiry[3] = "All";
        if (input.equals(employee[0])) {
            System.out.println("Type inquiry from list: ");
            System.out.println("\n");
            System.out.println(Arrays.toString(Inquiry));
            Scanner dave = new Scanner(System.in);
            String input2 = dave.nextLine();
            String [] Dave = new String[3];
                Dave[0] = "Technician";
                Dave[1] = "$87,000";
                Dave[2] = "3 Years";
            if (input2.equalsIgnoreCase("Job")) {
                System.out.println("\n");
                System.out.println(Dave[0]);
                System.out.println("\n");
            } else if (input2.equalsIgnoreCase("Salary")) {
                System.out.println("\n");
                System.out.println(Dave[1]);
                System.out.println("\n");
            } else if (input2.equalsIgnoreCase("Employed")) {
                System.out.println("\n");
                System.out.println(Dave[2]);
                System.out.println("\n");
            } else if (input2.equalsIgnoreCase("All")) {
                System.out.println("\n");
                System.out.println(Arrays.toString(Dave));
                System.out.println("\n");
            } else { 
            System.out.println("Enter A Valid Inquiry");
            continue;
        }
    }
            if (input.equals(employee[1])) {
            System.out.println("Type inquiry from list: ");
            System.out.println("\n");
            System.out.println(Arrays.toString(Inquiry));
            Scanner hugh = new Scanner(System.in);
            String input3 = hugh.nextLine();
            String [] Hugh = new String[3];
                Hugh[0] = "Stock Broker";
                Hugh[1] = "$135,000";
                Hugh[2] = "7 Years";
            if (input3.equalsIgnoreCase("Job")) {
                System.out.println("\n");
                System.out.println(Hugh[0]);
                System.out.println("\n");
            } else if (input3.equalsIgnoreCase("Salary")) {
                System.out.println("\n");
                System.out.println(Hugh[1]);
                System.out.println("\n");
            } else if (input3.equalsIgnoreCase("Employed")) {
                System.out.println("\n");
                System.out.println(Hugh[2]);
                System.out.println("\n");
            } else if (input3.equalsIgnoreCase("All")) {
                System.out.println("\n");
                System.out.println(Arrays.toString(Hugh));
                System.out.println("\n");
            } else { 
            System.out.println("Enter A Valid Inquiry");
            continue;
        }
    }
            if (input.equals(employee[2])) {
            System.out.println("Type inquiry from list: ");
            System.out.println("\n");
            System.out.println(Arrays.toString(Inquiry));
            Scanner ethan = new Scanner(System.in);
            String input4 = ethan.nextLine();
            String [] Ethan = new String[3];
                Ethan[0] = "Janitor";
                Ethan[1] = "$67,000";
                Ethan[2] = "15 Years";
            if (input4.equalsIgnoreCase("Job")) {
                System.out.println("\n");
                System.out.println(Ethan[0]);
                System.out.println("\n");
            } else if (input4.equalsIgnoreCase("Salary")) {
                System.out.println("\n");
                System.out.println(Ethan[1]);
                System.out.println("\n");
            } else if (input4.equalsIgnoreCase("Employed")) {
                System.out.println("\n");
                System.out.println(Ethan[2]);
                System.out.println("\n");
            } else if (input4.equalsIgnoreCase("All")) {
                System.out.println("\n");
                System.out.println(Arrays.toString(Ethan));
                System.out.println("\n");
            } else { 
            System.out.println("Enter A Valid Inquiry");
            continue;
        }
    }
            System.out.println("Any other Inquiries? Y or N");
            Scanner last = new Scanner(System.in);
            String resp = last.nextLine();
            if (resp.equals("Y")) {
                continue;
        } else if (resp.equals("N")) {
            System.exit(0);
        } else { 
            System.out.println("Enter A Valid Inquiry");
            continue;
}
}
}
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Your code is very redundant, you write the same thing over and over again. Also instead of using String Arrays use a Class to represent information.

So the first thing i noticed was that you had the inquiry array. Since there are only four values. You can create an Enum.

public enum Inquiry {
    JOB, SALARY, EMPLOYED, ALL;
}

An Enum can only be 1 thing at a time. (more on this later)

Then i noticed that you were working with employees in a database, so can create these classes. For Employees we see that they can have a name, job, salary and employed.

public class Employee {
    private String name;
    private String job;
    private String salary;
    private String employed;

    public Employee(String name, String job, String salary, String employed) {
        this.name = name;
        this.job = job;
        this.salary = salary;
        this.employed = employed;
    }

    public String getName() {
        return name;
    }

    public String getJob() {
        return job;
    }

    public String getSalary() {
        return salary;
    }

    public String getEmployed() {
        return employed;
    }

    public String getAll(){
        return "job=" + job + ", salary=" + salary + ", employed=" + employed;
    }
}

And the database has to maintain the list of employees. Now we need to consider how to get the employees from the database. We could store it in an Array, but if we had 1000 employees, this might be slow. So instead we store the employees in a map, and we can find them by using their names (This doesn't allow duplicate names, but it doesn't matter in this context)

The database needs to be able to add employees, tell you which employees are selectable and to get a certain employee.

import java.util.HashMap;
import java.util.Map;      

public class Database {

    private Map<String, Employee> employees;

    public Database() {
        employees = new HashMap<>();
    }

    public void addEmployee(String name, String job, String salary, String employed){
        employees.put(name, new Employee(name, job, salary, employed));
    }

    public Employee getEmployee(String name){
        return employees.get(name);
    }

    public String getEmployeeNames(){
        return employees.keySet().toString();
    }
}

Now we have everything, so we just need the terminal where we print stuff.

import java.util.Arrays;
import java.util.Scanner;

public class Terminal {
    public static void main(String args[]) {
        Database database = new Database();
        //Notice how employees are added
        database.addEmployee("Dave Johnson", "Technician", "$87,000", "3 Years");
        database.addEmployee("Hugh Man", "Stock Broker", "$135,000", "7 Years");
        database.addEmployee("Ethan Isakson", "Janitor", "$67,000", "15 Years");

        boolean running = true;
        while (running) {
            System.out.println("Enter employee name from list: ");
            System.out.println(database.getEmployeeNames());
            Scanner sc = new Scanner(System.in);
            String nameInput = sc.nextLine();

            Employee employee = database.getEmployee(nameInput);

            //If name doesn't exists in the database, then it will return null
            if (employee != null) {
                System.out.println("Type inquiry from list: ");
                System.out.println(Arrays.asList(Inquiry.values()));
                String inquiryInput = sc.nextLine();

                //If a user types something else than the four values, then we get an exception, so we need to catch it
                try {
                    //toUpperCase ignores the case of the inquiry that is typed
                    Inquiry inquiry = Inquiry.valueOf(inquiryInput.toUpperCase());
                    //Now you can see the inquiry in action. It can only be one thing
                    switch (inquiry) {
                        case JOB:
                            System.out.println(employee.getJob());
                            break;
                        case SALARY:
                            System.out.println(employee.getSalary());
                            break;
                        case EMPLOYED:
                            System.out.println(employee.getEmployed());
                            break;
                        case ALL:
                            System.out.println(employee.getAll());
                            break;
                    }
                }catch (IllegalArgumentException e){
                    System.out.println("Inquiry doesn't exist");
                }
            } else {
                System.out.println("That employee does not exist within the data");
            }

            System.out.println("Any other Inquiries? Y or N");
            Scanner last = new Scanner(System.in);
            String response = last.nextLine();
            if (response.equalsIgnoreCase("N")) {
                running = false;
            } else {
                System.out.println("Enter A Valid Inquiry");
            }
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.