Java Code Example: User Management with Customers and Employees

The following program demonstrates the concept of object-oriented programming. There are different classes, in our case customers and employees. You have the possibility to display the list of customers and employees or to create new employees. These are stored in ArrayLists. There are also functions that control the formatting of the screen output.

import java.io.*;
import java.util.*;

public class UserManagement {
    public static class Employee {
        private String name;
        private String prename;
        private Role role;
        private String user;
        private String pw;
        private String tel;
        private int age;
        private int employeeID;
        private int idCounter = 2000;

        public Employee() {
        }

        public Employee(String prename, String name, Role role, int age, String user, String pw, String tel) {
            this.prename = prename;
            this.name = name;
            this.role = role;
            this.age = age;
            this.user = user;
            this.pw = pw;
            this.tel = tel;
            this.employeeID = idCounter;
            idCounter++;
        }

        // set methods of class Employee
        public void setRole(Role role) {
            this.role = role;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public void setEmployeeID(int employeeID) {
            this.employeeID = employeeID;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setPrename(String prename) {
            this.prename = prename;
        }

        public void setTel(String tel) {
            this.tel = tel;
        }

        public void setUser(String user) {
            this.user = user;
        }

        public void setPW(String pw) {
            this.pw = pw;
        }

        // get methods of class Employee
        public Role getRole() {
            return this.role;
        }

        public int getAge() {
            return this.age;
        }

        public int getEmployeeID() {
            return this.employeeID;
        }

        public String getName() {
            return this.name;
        }

        public String getPrename() {
            return this.prename;
        }

        public String getTel() {
            return this.tel;
        }

        public String getUser() {
            return this.user;
        }

        public String getPW() {
            return this.pw;
        }
    }

    public static class Customer {
        private String name;
        private String prename;
        private int age;
        private int customerID;
        private int idCounter = 3000;

        public Customer() {
        }

        public Customer(String prename, String name, int age) {
            this.prename = prename;
            this.name = name;
            this.age = age;
            this.customerID = idCounter;
            idCounter++;
        }

        // set methods of class Customer
        public void setAge(int age) {
            this.age = age;
        }

        public void setCustomerID(int customerID) {
            this.customerID = customerID;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setPrename(String prename) {
            this.prename = prename;
        }

        // get methods of class Customer
        public int getAge() {
            return this.age;
        }

        public int getCustomerID() {
            return this.customerID;
        }

        public String getName() {
            return this.name;
        }

        public String getPrename() {
            return this.prename;
        }
    }

    public enum Role {
        CH("Cashier", 1),
        WM("Warehouse manager", 2),
        MD("Managing Director", 3);

        private String type;
        private int ratingNumber;

        // constructor
        Role(String roleType, int ratingNumber) {
            this.type = roleType;
            this.ratingNumber = ratingNumber;
        }

        // get methods
        public int getRatingNumber() {
            return this.ratingNumber;
        }

        public String getType() {
            return this.type;
        }
    }

    public static void showCustomerList(ArrayList customerList) {
        String s;

        printCentered("Prename");
        printCentered("Name");
        printCentered("Age");
        printCentered("Customer-ID");
        printLF();
        printSeperator(4);

        Iterator<Customer> iter = customerList.iterator();
        while (iter.hasNext()) {
            Customer i = iter.next();

            printLeftJustified(i.getPrename());
            printLeftJustified(i.getName());
            s = castInt2String(i.getAge());
            printCentered(s);
            s = castInt2String(i.getCustomerID());
            printCentered(s);

            printLF();
        }
        printLF();
    }

    public static void showEmployeeList(ArrayList employeeList) {
        String s;
        try {
            printCentered("Employee-ID");
            printCentered("Prename");
            printCentered("Name");
            printCentered("Role");
            printCentered("Age");
            printCentered("Tel");

            printLF();
            printSeperator(6);

            Iterator<Employee> iter = employeeList.iterator();
            while (iter.hasNext()) {
                Employee i = iter.next();

                s = castInt2String(i.getEmployeeID());
                printCentered(s);
                printLeftJustified(i.getPrename());
                printLeftJustified(i.getName());
                printLeftJustified(i.getRole().getType());
                s = castInt2String(i.getAge());
                printCentered(s);
                printLeftJustified(i.getTel());

                printLF();
            }
            printLF();
        } catch (Exception e) {
            System.out.println("Error during input, please try again.");
        }
    }

    private static void createEmployee(ArrayList employeeList) {
        Scanner scan = new Scanner(System.in);
        try {
            System.out.print("Please enter the first name of the employee: ");
            String prename = scan.next();
            System.out.print("Please enter the last name of the employee: ");
            String name = scan.next();
            System.out.print("Please enter the age of the employee: ");
            int age = scan.nextInt();
            System.out.print("Please enter the user name of the employee: ");
            String user = scan.next();
            System.out.print("Please enter the password of the employee: ");
            String pw = scan.next();
            System.out.print("Please enter the phone number of the employee: ");
            String tel = scan.next();

            System.out.println("Please enter the role of the employee: ");
            System.out.println("[1] Cashier");
            System.out.println("[2] Warehouse manager");
            System.out.println("[3] Managing Director");
            System.out.print("Please make a selection ...\n");
            int userInput = scan.nextInt();

            scan.close();

            switch (userInput) {
                case 1:
                    Employee m1 = new Employee(prename, name, Role.CH, age, user, pw, tel);
                    employeeList.add(m1);
                    break;
                case 2:
                    Employee m2 = new Employee(prename, name, Role.WM, age, user, pw, tel);
                    employeeList.add(m2);
                    break;
                case 3:
                    Employee m3 = new Employee(prename, name, Role.MD, age, user, pw, tel);
                    employeeList.add(m3);
                    break;
                default:
                    System.out.print("Your input was not recognized.\n");
            }
        } catch (Exception e) {
            System.out.println("Error during input, please try again.");
        }
    }

    public static void main(String[] args) {
        ArrayList<Employee> employeeList = new ArrayList<Employee>();
        ArrayList<Customer> customerList = new ArrayList<Customer>();

        Employee m1 = new Employee("James", "Williams", Role.CH, 45, "james_w", "123456", "5352525252");
        Employee m2 = new Employee("Robert", "Smith", Role.WM, 27, "SmithR", "123456", "0160 52525252");
        Employee m3 = new Employee("John", "Brown", Role.MD, 36, "john_br", "123456", "0160 52525252");
        Customer k1 = new Customer("Jennifer", "Jones", 23);
        Customer k2 = new Customer("Linda", "Miller", 31);

        employeeList.add(m1);
        employeeList.add(m2);
        employeeList.add(m3);
        customerList.add(k1);
        customerList.add(k2);

        createEmployee(employeeList);

        showCustomerList(customerList);
        showEmployeeList(employeeList);
    }

    // functions to format the outputs
    private static String castInt2String(int meinInt) {
        return Integer.toString(meinInt);
    }

    private static void printLF() {
        System.out.println();
    }

    private static void printCentered(String s) {
        System.out.print(createCenteredString(s, 10));
    }

    private static void printLeftJustified(String s) {
        System.out.print(createLeftJustifiedString(s, 10));
    }

    private static void printSeperator(int number) {
        StringBuilder s = new StringBuilder();
        int laenge = number * (10 + 3);
        for (int i = 1; i <= laenge; i++) {
            s = s.append("-");
        }
        System.out.println(s);
    }

    private static String createCenteredString(String s, int l) {
        s.trim();
        if (s.length() > l) {
            s.substring(0, l);
        } else {
            int differenzLinks = (l - s.length()) / 2;
            for (int i = 1; i <= differenzLinks; i++)
                s = " " + s + " ";
            if (s.length() < l)
                s = s + " ";
        }
        return s + " | ";
    }

    private static String createLeftJustifiedString(String s, int l) {
        s.trim();
        if (s.length() > l) {
            s.substring(0, l);
        } else {
            int differenz = (l - s.length());
            for (int i = 1; i <= differenz; i++)
                s = s + " ";
        }
        return s + " | ";
    }
}
Output
Please enter the first name of the employee: Eva
Please enter the last name of the employee: Smith
Please enter the age of the employee: 44
Please enter the user name of the employee: smith_Eva
Please enter the password of the employee: iav43j4iKSDL$wsfd
Please enter the phone number of the employee: 01604456245
Please enter the role of the employee: 
[1] Cashier
[2] Warehouse manager
[3] Managing Director
Please make a selection ...
2
 Prename   |    Name    |    Age     | Customer-ID | 
----------------------------------------------------
Jennifer   | Jones      |     23     |    3000    | 
Linda      | Miller     |     31     |    3000    | 

Employee-ID |  Prename   |    Name    |    Role    |    Age     |    Tel     | 
------------------------------------------------------------------------------
   2000    | James      | Williams   | Cashier    |     45     | 5352525252 | 
   2000    | Robert     | Smith      | Warehouse manager |     27     | 0160 52525252 | 
   2000    | John       | Brown      | Managing Director |     36     | 0160 52525252 | 
   2000    | Eva        | Smith      | Warehouse manager |     44     | 01604456245 |