gpt4 book ai didi

JAVA - 修改员工

转载 作者:行者123 更新时间:2023-12-01 22:18:28 26 4
gpt4 key购买 nike

善良,学习。

这些是我的类(class):Employee、salaryworker、hourlyworker、PayrollTest

我需要能够修改现有员工,但我需要它能够区分是小时工还是薪水 worker ,因为他们有不同的输入。我可以通过员工的 ID 找到该员工,但不知道如何正确修改它。我不知道如何做到这一点。

import java.util.*;
import java.lang.*;

public class PayrollTest {

//Array Lists for Hourly and Salary Workers.
public static hourlyworker hourlyEmp = new hourlyworker(null, 0, 0.0, 0.0, 0.0, 0, 0, 0);
public static ArrayList<hourlyworker> hourlyList = new ArrayList<hourlyworker>();
//(String name, int dependents, double annualSalary, int month, int day, int year) {

public static salaryworker salaryEmp = new salaryworker(null, 0, 0.0, 0, 0, 0);
public static ArrayList<salaryworker> salaryList = new ArrayList<salaryworker>();

//Arraylist Initializing
public static ArrayList emps = new ArrayList(); //<Employee>

//Initializing scanner for user input
public static Scanner scan = new Scanner(System.in);

//Fields needed for PayrollTest
private static char choice;
private static char typeChoice;
private static boolean switcher = true;

@SuppressWarnings("resource")
public static void main(String[] args) {


menuSelection();
System.out.println();


}

@SuppressWarnings("unchecked")
private static void loadHourlyEmployee() {

System.out.println("\nEnter full name, number of dependents, hourly rate, regular hours worked, overtime worked, and date hired (yyyy mm dd)");
System.out.println("[one field per line, except date hired]: ");
hourlyEmp = new hourlyworker(null, 0, 0.0, 0.0, 0.0, 0, 0, 0);

hourlyEmp.setName(scan.nextLine());
hourlyEmp.setDependents(scan.nextInt());
hourlyEmp.setPayrate(scan.nextDouble());
hourlyEmp.setHours(scan.nextDouble());
hourlyEmp.setOvertimeHours(scan.nextDouble());
hourlyEmp.setHireDay(scan.nextInt());
emps.add(hourlyEmp);

System.out.println("\nYou entered an employee type for non-exempt: hourly.");


}

@SuppressWarnings("unchecked")
private static void loadSalaryEmployee() {

System.out.println("\nEnter full name, number of dependents, annual salary, and date hired (yyyy mm dd)");
System.out.println("[one field per line, except date hired]: ");
salaryEmp = new salaryworker(null, 0, 0.0, 0, 0, 0);

salaryEmp.setName(scan.nextLine());
salaryEmp.setDependents(scan.nextInt());
salaryEmp.setAnnualSalary(scan.nextDouble());
salaryEmp.setHireDay(scan.nextInt());
emps.add(salaryEmp);

System.out.println("\nYou entered an employee type for exempt: salary.");


}

private static void menuSelection() {
do {
System.out.println("\n\n\tEmployee Database");
System.out.println("\nEmployee Info Menu");
System.out.println("\tEnter L to (L)oad Employee info");
System.out.println("\tEnter M to (M)odify Employee info");
System.out.println("\tEnter P to (P)rint Employee info");
System.out.println("\tEnter Q to quit");
System.out.print("Please enter your choice: ");
choice = scan.nextLine().toUpperCase().charAt(0);

//Choice validation
while ((choice != 'L') && (choice != 'M') && (choice != 'P') && (choice != 'Q')) {

System.out.println("Invalid choice! Please select (L)oad, (M)odify, (P)rint, or (Q)uit: ");
choice = scan.nextLine().toUpperCase().charAt(0);
}



//Switch Statement
switch (choice) {
case 'L' :

System.out.println("What is the employee type? S = salary H = hourly ");
System.out.print("\n[Please enter letter] : ");
typeChoice = scan.nextLine().toUpperCase().charAt(0);

//typeChoice validation
while ((typeChoice != 'S') && (typeChoice != 'H')) {
System.out.println("Invalid choice! Please select (S)alary, (H)ourly: ");
typeChoice = scan.nextLine().toUpperCase().charAt(0);
}

if (typeChoice == 'H') {
loadHourlyEmployee();}

else if (typeChoice == 'S') {
loadSalaryEmployee();
}

break;

case 'M' :

modifyEmployee();


break;

case 'P' :
printEmployees();
break;

case 'Q' :
switcher = false;
System.out.println("\nProgram Terminated.");
break;

}
}

while (switcher != false);


}



private static void printEmployees() {
System.out.println("\nThere are " + emps.size() + " people in this database.\n");

for (int i = 0; i < emps.size(); i++) {
System.out.println(emps.get(i));
}

System.out.println("\nTotal = " + emps.size());
}
private static void modifyEmployee() {

System.out.print("Employee id#? ");
int findID = scan.nextInt();

boolean found = false;
int index = 0;
boolean deleteMod = false;
int index1 = 0;

while (index < emps.size() && !found) {
if (emps.get(index) != null) {
found = true;
}

deleteMod = true;

System.out.println("\nCurrent Info: ");


}
}
}

最佳答案

使用instanceof来确定它是否是您想要的类。

if (emps.get(index) instanceof hourlyworker) {
// code for hourlyworker
} else {
// code for salaryworker
}

关于JAVA - 修改员工,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30473402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com