基本上我编写了这段代码,用户为类的成员输入了某些值。我也做了一个ArrayList
课程。我希望以这样的方式对其进行编码,即用户可以要求程序打印他/她输入的所有数据,但我似乎无法使其正常工作。
工作:
public class Jobs {
protected float CostPerHour, HoursWorked, CostOfMaterials, CostOfJob;
protected String Date, JobNumber;
public Jobs(String JobNumber, String Date, float CostPerHour, float CostOfMaterials, float HoursWorked){
this.JobNumber = JobNumber;
this.Date = Date;
this.CostPerHour = CostPerHour;
this.HoursWorked = HoursWorked;
this.CostOfMaterials = CostOfMaterials;
this.CostOfJob = CostOfJob(CostPerHour, CostOfMaterials, HoursWorked);
System.out.println("Job has been added to the list.");
}
public float CostOfJob(float CostPerHour, float CostOfMaterials, float HoursWorked){
CostOfJob = CostPerHour*HoursWorked + CostOfMaterials;
System.out.println("The cost for this job is "+ CostOfJob);
return CostOfJob;
}
public float getCostPerHour() {
return CostPerHour;
}
public float getHoursWorked() {
return HoursWorked;
}
public float getCostOfMaterials() {
return CostOfMaterials;
}
public float getCostOfJob() {
return CostOfJob;
}
public String getDate() {
return Date;
}
public String getJobNumber() {
return JobNumber;
}
}
JobTester:
public class JobTester {
static ArrayList<Jobs> jobs = new ArrayList<>();
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
float CostPerHour, HoursWorked, CostOfMaterials;
String Date, JobNumber;
String option = "y";
while((option.equals("y"))||(option.equals("Y"))){
System.out.print("Please Enter the Date : ");
Date = keyboard.nextLine();
System.out.print("Please Enter the Job Number : ");
JobNumber = keyboard.nextLine();
System.out.print("Please Enter the Cost Per Hour : ");
CostPerHour = keyboard.nextFloat();
System.out.print("Please Enter the Number of Hours : ");
HoursWorked = keyboard.nextFloat();
System.out.print("Please Enter the Cost of Materials : ");
CostOfMaterials = keyboard.nextFloat();
keyboard.nextLine();
jobs.add(new Jobs(JobNumber, Date, CostPerHour, CostOfMaterials, HoursWorked));
System.out.print("Press Y to create a new Job : ");
option = keyboard.nextLine();
}
System.out.print("Press Y to print the list of jobs you created : ");
option = keyboard.nextLine();
if((option.equals("y"))||(option.equals("Y"))){
PrintJobs();
}
System.out.print("Press any key to exit.");
keyboard.nextLine();
}
public static void PrintJobs(){
System.out.println("Date \tJobNumber \tCostPerHour x HoursWorked + CostOfMaterials = CostOfJob");
for(int i = 0; i>jobs.size(); i++){
System.out.println(jobs.get(i).getDate()+"\t"+jobs.get(i).getJobNumber()+"\t"+jobs.get(i).getCostPerHour()+" x "+jobs.get(i).getHoursWorked()+" + "+jobs.get(i).getCostOfMaterials()+" = "+jobs.get(i).getCostOfJob());
}
}
}
答案 0 :(得分:2)
这更干净。您甚至可以考虑使用System.out.printf
。
public static void PrintJobs()
{
System.out.println("Date \tJobNumber \tCostPerHour x HoursWorked +
CostOfMaterials = CostOfJob");
for(Job j: jobs)
{
System.out.println(j.getDate()+ "\t"+ j.getJobNumber()+ ...
}
}