首先,这里是说明: http:
//ideone.com/eRHwUo
是的,这是作业!话虽如此(因为我只是喜欢这种语言),当您发布答案时,请尝试使用伪代码,这样我就不会复制并粘贴到我的程序中。谢谢!
就代码而言,我已经完成了用户输入所有输入部分所需的一切。但是,我需要帮助将数据从类“传输”到我们应该使用的“ArrayList”。我想一旦我把它记下来,我就可以对数组进行排序以找到选择“B”的 ID 号,如果用户输入“C”,我将循环显示数组中的所有内容它。
无论如何,进入代码(这是主要的):
/*
* Name:
* Date:
* Assignment 2
*/
import java.util.Scanner;
public class homework
{
public static void main(String[] args)
{
char userSelection;
String convertString;
String userStrings;
Scanner kbd = new Scanner(System.in);
do
{
System.out.println("Here are your choices:");
System.out.println("A. Enter employee data" +
"\nB. Search for employee data" +
"\nC. List all data" +
"\nD. Exit");
convertString = kbd.next();
userSelection = convertString.charAt(0);
switch(userSelection)
{
case 'A':
GetUserInfo();
break;
case 'B':
// Stuff;
break;
case 'C':
// Display all data;
break;
case 'D':
System.out.println("Goodbye!");
break;
default:
System.out.println("Error, that is not a valid entry. Please try again.");
}
} while (userSelection > 'D');
}
// Write functions here
public static void GetUserInfo()
{
String firstName;
String lastName;
String empID;
double hourlyRate;
int hoursWorked;
double withPercent;
Scanner kbd = new Scanner(System.in);
System.out.println("What is your first name?");
firstName = kbd.next();
System.out.println("What is your last name?");
lastName = kbd.next();
System.out.println("What is your employee ID?");
empID = kbd.next();
Employee user = new Employee(empID);
user.setFirstName(firstName);
user.setLastName(lastName);
System.out.println("What is your hourly rate?");
hourlyRate = kbd.nextDouble();
System.out.println("How many hours did you work?");
hoursWorked = kbd.nextInt();
System.out.println("What is your withholding percentage?");
withPercent = kbd.nextDouble();
Pay user1 = new Pay();
user1.setHourlyRate(hourlyRate);
user1.setHoursWorked(hoursWorked);
user1.setWithPercent(withPercent);
}
}
这是员工类:
public class Employee
{
// Members of the class
String firstName;
String lastName;
String employeeID;
// remember about the pay object
// EmployeeID constructor
public Employee(String empID)
{
this.employeeID = empID;
}
// Below are the various getters and setters of the Employee class
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmployeeID()
{
return employeeID;
}
}
这是 Pay 类:
public class Pay
{
// Members of the class
double hourlyRate;
int hoursWorked;
double withPercent;
// Various getters and setters of the Pay class
public double getHourlyRate()
{
return hourlyRate;
}
public void setHourlyRate(double hourlyRate)
{
this.hourlyRate = hourlyRate;
}
public int getHoursWorked()
{
return hoursWorked;
}
public void setHoursWorked(int hoursWorked)
{
this.hoursWorked = hoursWorked;
}
public double getWithPercent()
{
return withPercent;
}
public void setWithPercent(double withPercent)
{
this.withPercent = withPercent;
}
// Calculates the raw payment
public double CalcPayRate(double hourlyRate, int hoursWorked)
{
return hourlyRate * hoursWorked;
}
// If the employee has worked overtime, calculates the new payment
public double CalcOvertimePay(double hourlyRate, int hoursWorked)
{
double rawPay = 0;
rawPay = hourlyRate * hoursWorked;
if (hoursWorked > 40)
{
rawPay *= 1.5;
}
return rawPay;
}
// Calculates final amount that the employee will be paid
public double CalcTotalPay(double hourlyRate, int hoursWorked, double withPercent)
{
double rawPay = 0;
double subTotalPay = 0;
double finalPay = 0;
rawPay = hourlyRate * hoursWorked;
subTotalPay = rawPay * withPercent;
finalPay = rawPay - subTotalPay;
return finalPay;
}
}
那么,请思考?
另外,最后的评论:
(1)我不明白“支付对象”在 Employee 类下应该做什么?
(2) 如何使用输入数据创建“支付对象”,然后创建员工对象?
这些是我不太清楚的说明中的两个部分,因此,如果您可以对此有所了解,那将会很有帮助!
(3) 如果我的语法有什么不对的地方,请告诉我,以便我可以相应地更改它。我还是这种语言的新手,所以任何帮助都会很棒。
编辑:我用评论更新了我的代码:
public class homework
{
public static void main(String[] args)
{
// ArrayList<Employee> employeeList = new ArrayList<Employee>();
do
{
// Stuff commented out for readability
case 'A':
PromptForInput();
// Employee emp = PromptForInput();
// employeeList.add(emp);
break;
} while (userSelection > 'D');
}
// public static Employee PromptInput()
public static void PromptForInput()
{
Employee user = new Employee(empID);
// Isn't this supposed to be returned as well?
Pay user1 = new Pay();
user1.setHourlyRate(hourlyRate);
user1.setHoursWorked(hoursWorked);
user1.setWithPercent(withPercent);
//return user;
}
}
这是它应该看起来的样子吗?
我仍然不明白作业的“支付对象”部分..