manager

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.4 kB
1
Indexable
Never
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package s000013;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

/**
 *
 * @author 
 */
public class Manager {

    Validate vd = new Validate();
    String id;
    String name;
    int salary;
    String workLocation;
    int age;
            
    /**
     *
     */
    public static ArrayList<Worker> lw = new ArrayList<>();

    /**
     *
     */
    public static ArrayList<History> lh = new ArrayList<>();

    //show menu

    /**
     *
     */
    public void menu() {
        System.out.println("1. Add worker.");
        System.out.println("2. Increase salary for worker.");
        System.out.println("3. Decrease for worker");
        System.out.println("4. Show adjusted salary worker information");
        System.out.println("5. Exist");
        System.out.print("Enter your choice: ");
    }

    //allow user add worker

    /**
     *
     */
    public void addWorker() {
        System.out.print("Enter code: ");
        id = vd.checkInputString();
        System.out.print("Enter name: ");
        name = vd.checkInputString();
        System.out.print("Enter age: ");
        age = vd.checkInputAge();
        System.out.print("Enter salary: ");
        salary = vd.checkInputSalary();
        System.out.print("Enter work location: ");
        workLocation = vd.checkInputString();
        if (!vd.checkWorkerExist(lw, id, name, age, salary, workLocation)) {
            System.out.println("Duplicate.");
        } else {
            lw.add(new Worker(id, name, age, salary, workLocation));
            System.out.println("Add success.");
        }
    }

    //allow user increase salary for user

    /**
     *
     */
    public void changeSalary() {
        int status = 0;
        if (lw.isEmpty()) {
            System.out.println("List empty.");
            return;
        }
        System.out.print("Enter code: ");
        id = vd.checkInputString();
        Worker worker = getWorkerByCode(id);
        if (worker == null) {
            System.out.println("Not exist worker.");
        } else {
            int salaryCurrent = worker.getSalary();
            int salaryUpdate;
            //check user want to update salary
            if (status == 1) {
                System.out.print("Enter salary: ");
                //loop until user input salary update > salary current
                while (true) {
                    salaryUpdate = vd.checkInputSalary();
                    //check user input salary update > salary current
                    if (salaryUpdate >= salaryCurrent) {
                        System.out.println("Must be greater than current salary.");
                        System.out.print("Enter again: ");
                    } else {
                        break;
                    }
                }
                lh.add(new History("UP", getCurrentDate(), worker.getId(),
                        worker.getName(), worker.getAge(), salaryUpdate,
                        worker.getWorkLocation()));
            } else {
                System.out.print("Enter salary: ");
                //loop until user input salary update < salary current
                while (true) {
                    salaryUpdate = vd.checkInputSalary();
                    //check user input salary update < salary current
                    if (salaryUpdate >= salaryCurrent) {
                        System.out.println("Must be smaller than current salary.");
                        System.out.print("Enter again: ");
                    } else {
                        break;
                    }
                }
                lh.add(new History("DOWN", getCurrentDate(), worker.getId(),
                        worker.getName(), worker.getAge(), salaryUpdate,
                        worker.getWorkLocation()));
            }
            worker.setSalary(salaryUpdate);
            System.out.println("Update success");
        }
    }

    //allow user print history

    /**
     *
     */
    public static void printListHistory() {
        if (lh.isEmpty()) {
            System.out.println("List empty.");
            return;
        }
        System.out.printf("%-5s%-15s%-5s%-10s%-10s%-20s\n", "Code", "Name", "Age",
                "Salary", "Status", "Date");
        Collections.sort(lh);
        //print history from first to last list
        for (History history : lh) {
            printHistory(history);
        }
    }

    //get worker by code

    /**
     *
     * @param id
     * @return
     */
    public static Worker getWorkerByCode(String id) {
        for (Worker worker : lw) {
            if (id.equalsIgnoreCase(worker.getId())) {
                return worker;
            }
        }
        return null;
    }

    //get current date 

    /**
     *
     * @return
     */
    public static String getCurrentDate() {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Calendar calendar = Calendar.getInstance();
        return dateFormat.format(calendar.getTime());
    }

    //print history

    /**
     *
     * @param history
     */
    public static void printHistory(History history) {
        System.out.printf("%-5s%-15s%-5d%-10d%-10s%-20s\n", history.getId(),
                history.getName(), history.getAge(), history.getSalary(),
                history.getStatus(), history.getDate());
    }

    /**
     *
     */
    public void openMenu() {
        while (true) {
            menu();
            int choice = vd.checkInputIntLimit();
            switch (choice) {
                case 1:
                    addWorker();
                    break;
                case 2:
                    changeSalary();
                    break;
                case 3:
                    changeSalary();
                    break;
                case 4:
                    printListHistory();
                    break;
                case 5:
                    return;
            }
        }
    }
}