Skip to content

Commit

Permalink
refactoring due bad smell 'Long Parameter List' on undo, redo stack
Browse files Browse the repository at this point in the history
  • Loading branch information
WilamisAviz15 committed May 22, 2021
1 parent 9acb795 commit b5e5a09
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 57 deletions.
24 changes: 16 additions & 8 deletions Payroll/src/payroll/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ public class Panel {

Stack<List<Employee>> undo = new Stack<>();
Stack<List<Employee>> redo = new Stack<>();
StackUndoRedo stack_undo_redo = new StackUndoRedo(undo, redo);

public static void LaunchSales(List<Employee> list_employee, Stack<List<Employee>> undo,
Stack<List<Employee>> redo) {
public static void LaunchSales(List<Employee> list_employee, StackUndoRedo stack_undo_redo) {
int index = MenuEmployee.findEmployee(list_employee);
Double value;
String tmp = "";
Scanner sc = new Scanner(System.in);
if (index != -1) {
Employee selectedEmployee = list_employee.get(index);
if (selectedEmployee instanceof Commissioned) {
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
undo.push(Utils.cloneList(list_employee));
redo.clear();
Commissioned empl = (Commissioned) selectedEmployee;
Expand All @@ -57,14 +59,16 @@ public static void LaunchSales(List<Employee> list_employee, Stack<List<Employee
}
}

public static void LaunchFee(List<Employee> list_employee, Stack<List<Employee>> undo, Stack<List<Employee>> redo) {
public static void LaunchFee(List<Employee> list_employee, StackUndoRedo stack_undo_redo) {
int index = MenuEmployee.findEmployee(list_employee);
String tmp = "";
Double value;
Scanner sc = new Scanner(System.in);
if (index != -1) {
Employee selectedEmployee = list_employee.get(index);
if (selectedEmployee.getSyndicate().getActive() == true) {
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
undo.push(Utils.cloneList(list_employee));
redo.clear();
Syndicate s = Utils.cloneListSyndicate(selectedEmployee.getSyndicate());
Expand Down Expand Up @@ -107,25 +111,27 @@ public void run() {
opc = Integer.parseInt(option);
switch (opc) {
case 1:
MenuEmployee.Menu(list_employee, paySchedules, undo, redo, payslipSheet);
MenuEmployee.Menu(list_employee, paySchedules, stack_undo_redo, payslipSheet);
break;
case 2:
MenuTimecard.Timecard(list_employee, undo, redo);
MenuTimecard.Timecard(list_employee, stack_undo_redo);
break;
case 3:
LaunchSales(list_employee, undo, redo);
LaunchSales(list_employee, stack_undo_redo);
break;
case 4:
LaunchFee(list_employee, undo, redo);
LaunchFee(list_employee, stack_undo_redo);
break;
case 5:
MenuPayroll.Menu(list_employee, undo, redo);
MenuPayroll.Menu(list_employee, stack_undo_redo);
break;
case 6:
MenuPayoutSchedule.Menu(paySchedules);
break;
case 7:
if (undo.size() > 0) {
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
List<Employee> aux = undo.pop();
redo.push(Utils.cloneList(list_employee));
list_employee = aux;
Expand All @@ -136,6 +142,8 @@ public void run() {
break;
case 8:
if (redo.size() > 0) {
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
List<Employee> aux = redo.pop();
undo.push(Utils.cloneList(list_employee));
list_employee = aux;
Expand Down
30 changes: 30 additions & 0 deletions Payroll/src/payroll/StackUndoRedo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package payroll;

import java.util.List;
import java.util.Stack;
import payroll.employee.model.Employee;

public class StackUndoRedo {
Stack<List<Employee>> undo, redo;

public StackUndoRedo(Stack<List<Employee>> undo, Stack<List<Employee>> redo){
this.undo = undo;
this.redo = redo;
}

public Stack<List<Employee>> getUndo() {
return undo;
}

public void setUndo(Stack<List<Employee>> undo) {
this.undo = undo;
}

public Stack<List<Employee>> getRedo() {
return redo;
}

public void setRedo(Stack<List<Employee>> redo) {
this.redo = redo;
}
}
55 changes: 31 additions & 24 deletions Payroll/src/payroll/employee/MenuEmployee.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Stack;

import payroll.Panel;
import payroll.StackUndoRedo;
import payroll.employee.model.Commissioned;
import payroll.employee.model.Employee;
import payroll.employee.model.Hourly;
Expand All @@ -24,8 +25,8 @@ public class MenuEmployee {
public static int idSyndicateInt = 0;
private static String[] accountType = { "Corrente", "Poupança", "Fácil", "Conjunta" };

public static void Menu(List<Employee> list_employee, PaymentSchedule paySchedule, Stack<List<Employee>> undoStack,
Stack<List<Employee>> redo, List<Payslip> payslipSheet) {
public static void Menu(List<Employee> list_employee, PaymentSchedule paySchedule, StackUndoRedo stack_undo_redo,
List<Payslip> payslipSheet) {
int option;
String tmp = "";
Scanner op = new Scanner(System.in);
Expand All @@ -40,28 +41,31 @@ public static void Menu(List<Employee> list_employee, PaymentSchedule paySchedul
tmp = Utils.consoleReadInputIntegerOptions(tmp, op, 1, 7);
option = Integer.parseInt(tmp);
switch (option) {
case 1:
list_employee.add(MenuEmployee.addEmployee(paySchedule, list_employee, undoStack, redo, payslipSheet));
break;
case 2:
MenuEmployee.removeEmployee(list_employee, undoStack, redo);
break;
case 3:
MenuEmployee.editEmployee(list_employee, paySchedule, undoStack, redo, payslipSheet);
break;
case 4:
MenuEmployee.ListAllEmployee(list_employee);
break;
case 5:
MenuEmployee.listEmployeeById(list_employee);
break;
case 1:
list_employee
.add(MenuEmployee.addEmployee(paySchedule, list_employee, stack_undo_redo, payslipSheet));
break;
case 2:
MenuEmployee.removeEmployee(list_employee, stack_undo_redo);
break;
case 3:
MenuEmployee.editEmployee(list_employee, paySchedule, stack_undo_redo, payslipSheet);
break;
case 4:
MenuEmployee.ListAllEmployee(list_employee);
break;
case 5:
MenuEmployee.listEmployeeById(list_employee);
break;
}
} while (option != 6);
}

public static Employee addEmployee(PaymentSchedule paySchedule, List<Employee> lEmployees,
Stack<List<Employee>> undoStack, Stack<List<Employee>> redo, List<Payslip> payslipSheet) {
undoStack.push(Utils.cloneList(lEmployees));
StackUndoRedo stack_undo_redo, List<Payslip> payslipSheet) {
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
undo.push(Utils.cloneList(lEmployees));
redo.clear();
PaymentMethod paymentMethod = null;
Syndicate sindicalist = null;
Expand Down Expand Up @@ -156,11 +160,12 @@ public static Employee addEmployee(PaymentSchedule paySchedule, List<Employee> l
return newEmployees;
}

public static void removeEmployee(List<Employee> list_employee, Stack<List<Employee>> undoStack,
Stack<List<Employee>> redo) {
public static void removeEmployee(List<Employee> list_employee, StackUndoRedo stack_undo_redo) {
int index = findEmployee(list_employee);
if (index != -1) {
undoStack.push(Utils.cloneList(list_employee));
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
undo.push(Utils.cloneList(list_employee));
redo.clear();
list_employee.remove(index);
System.out.println("Employee removed.");
Expand Down Expand Up @@ -220,7 +225,7 @@ public static boolean findEmployeeSyndicate(List<Employee> list_employee, String
}

public static void editEmployee(List<Employee> list_employee, PaymentSchedule paySchedule,
Stack<List<Employee>> undoStack, Stack<List<Employee>> redo, List<Payslip> payslipSheet) {
StackUndoRedo stack_undo_redo, List<Payslip> payslipSheet) {
String op, attr;
Double salary = 0.00;
String bankId;
Expand All @@ -230,7 +235,9 @@ public static void editEmployee(List<Employee> list_employee, PaymentSchedule pa
Scanner sc = new Scanner(System.in);
int index = findEmployee(list_employee);
if (index != -1) {
undoStack.push(Utils.cloneList(list_employee));
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
undo.push(Utils.cloneList(list_employee));
redo.clear();
Employee selectedEmployee = list_employee.get(index);
System.out.printf("Employed selected: %s. What do you want to edit?\n", selectedEmployee.getName());
Expand Down
23 changes: 14 additions & 9 deletions Payroll/src/payroll/employee/MenuTimecard.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import java.util.Scanner;
import java.util.Stack;

import payroll.StackUndoRedo;
import payroll.employee.model.Employee;
import payroll.employee.model.Hourly;
import payroll.employee.model.Timecard;
import payroll.utils.Utils;

public class MenuTimecard {
public static void Timecard(List<Employee> list_employee, Stack<List<Employee>> undo, Stack<List<Employee>> redo) {
public static void Timecard(List<Employee> list_employee, StackUndoRedo stack_undo_redo) {
int option;
String tmp = "";
Scanner op = new Scanner(System.in);
Expand All @@ -24,23 +25,25 @@ public static void Timecard(List<Employee> list_employee, Stack<List<Employee>>
tmp = Utils.consoleMenuTimecard(tmp, op);
option = Integer.parseInt(tmp);
switch (option) {
case 1:
Login(list_employee, undo, redo);
break;
case 2:
Logout(list_employee, undo, redo);
break;
case 1:
Login(list_employee, stack_undo_redo);
break;
case 2:
Logout(list_employee, stack_undo_redo);
break;
}
} while (option != 3);
}

public static void Login(List<Employee> list_employee, Stack<List<Employee>> undo, Stack<List<Employee>> redo) {
public static void Login(List<Employee> list_employee, StackUndoRedo stack_undo_redo) {
int index = MenuEmployee.findEmployee(list_employee);
String tmp = "";
Scanner sc = new Scanner(System.in);
if (index != -1) {
Employee selectedEmployee = list_employee.get(index);
if (selectedEmployee instanceof Hourly) {
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
undo.push(Utils.cloneList(list_employee));
redo.clear();
Hourly empl = (Hourly) selectedEmployee;
Expand Down Expand Up @@ -71,7 +74,7 @@ public static int findTimecard(List<Timecard> list, LocalDate date) {
return -1;
}

public static void Logout(List<Employee> list_employee, Stack<List<Employee>> undo, Stack<List<Employee>> redo) {
public static void Logout(List<Employee> list_employee, StackUndoRedo stack_undo_redo) {
int index = MenuEmployee.findEmployee(list_employee);
String tmp = "";
Scanner sc = new Scanner(System.in);
Expand All @@ -82,6 +85,8 @@ public static void Logout(List<Employee> list_employee, Stack<List<Employee>> un
LocalDate date = Utils.validateDate(sc);
int aux = findTimecard(empl.getTimecard(), date);
if (aux != -1) {
Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
undo.push(Utils.cloneList(list_employee));
redo.clear();
List<Timecard> t = Utils.cloneListTimecard(empl.getTimecard());
Expand Down
31 changes: 15 additions & 16 deletions Payroll/src/payroll/payment/MenuPayroll.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Scanner;
import java.util.Stack;

import payroll.StackUndoRedo;
import payroll.employee.model.Commissioned;
import payroll.employee.model.Employee;
import payroll.employee.model.Hourly;
Expand All @@ -16,7 +17,7 @@

public class MenuPayroll {

public static void Menu(List<Employee> list_employee, Stack<List<Employee>> undo, Stack<List<Employee>> redo) {
public static void Menu(List<Employee> list_employee, StackUndoRedo stack_undo_redo) {
int option;
String tmp = "";
Scanner op = new Scanner(System.in);
Expand All @@ -29,15 +30,15 @@ public static void Menu(List<Employee> list_employee, Stack<List<Employee>> undo
tmp = Utils.consoleReadInputIntegerOptions(tmp, op, 1, 4);
option = Integer.parseInt(tmp);
switch (option) {
case 1:
LocalDate date = Utils.validateDate(op);
rotate(list_employee, date, undo, redo);
break;
case 2:
printAll(list_employee);
break;
case 3:
break;
case 1:
LocalDate date = Utils.validateDate(op);
rotate(list_employee, date, stack_undo_redo);
break;
case 2:
printAll(list_employee);
break;
case 3:
break;
}
} while (option != 3);
}
Expand Down Expand Up @@ -123,8 +124,7 @@ public static void printAll(List<Employee> list_employee) {
System.out.println("#################END PAYSLIP###############");
}

public static void rotate(List<Employee> list_employee, LocalDate date, Stack<List<Employee>> undo,
Stack<List<Employee>> redo) {
public static void rotate(List<Employee> list_employee, LocalDate date, StackUndoRedo stack_undo_redo) {
String scheduleString = "", monthSchedule = "", weekSchedule = "", daySchedule = "";
List<Payslip> payslip = null;
Calendar c = Calendar.getInstance();
Expand All @@ -135,7 +135,8 @@ public static void rotate(List<Employee> list_employee, LocalDate date, Stack<Li
LocalDate oldDate = date;
int daysHolidayOrWeekn = Utils.countHolidaysOrWeekend(date, holidays);
Scanner sc = new Scanner(System.in);

Stack<List<Employee>> undo = stack_undo_redo.getUndo();
Stack<List<Employee>> redo = stack_undo_redo.getRedo();
undo.push(Utils.cloneList(list_employee));
redo.clear();

Expand Down Expand Up @@ -171,9 +172,7 @@ public static void rotate(List<Employee> list_employee, LocalDate date, Stack<Li
selectedEmployee.setPayslipSheet(clone);
}
}




for (Employee selectedEmployee : list_employee) {
if (selectedEmployee instanceof Salaried) {
GeneratePayslipEmployee.genEmployee(selectedEmployee, scheduleString, monthSchedule, daySchedule,
Expand Down

0 comments on commit b5e5a09

Please sign in to comment.