爪哇工薪雇员
嘿伙计们第一次来这里。我开始学习Java,我们的第一个任务之一是:
假设我们想在一份全职、领薪水的公司员工档案中确定薪酬最高的员工。对于受薪员工,文件中的每一行包括姓名、工资和可选奖金。总工资为工资加奖金(如果有奖金);0表示没有奖金。例如,假设文件“salaried.txt”包含以下内容:
Agrawal,Harsh 450.00 0
Chiger,Steve 420.00 60.00
Cromer,Jason 460.00 0
Petkov,Yuli 430.00 40.00
Siddiqi,Amena 460.00 15.00
然后工资最高的员工是奇格,史蒂夫,总工资480.00美元
每次我试图运行程序时,它都不会读取文件名。我不知道我是否在工薪员工或工薪公司遗漏了什么,但我的代码如下。我基本上被卡住了
import java.text.DecimalFormat;
public class SalariedEmployee extends FullTimeEmployee
{
protected double grossPay,
bonus,
salary;
public SalariedEmployee()
{
grossPay=0.00;
bonus=0.00;
salary=0.00;
}
public SalariedEmployee (String name, double salary, double bonus)
{
this.name = name;
this.salary=salary;
this.bonus=bonus;
if (bonus==0)
{
grossPay+=salary;
}
else
{
grossPay=salary+bonus;
}
}
public boolean setSalaryPay(double income)
{
if(income<0)
{
return false;
}
this.salary=income;
return true;
}
public double getSalary()
{
return salary;
}
public double getBonus()
{
return bonus;
}
public String toString()
{
final String FULL_TIME_STATUS = "FULL TIME SALARIED";
return super.toString() + FULL_TIME_STATUS;
} // meth
}
import java.util.*;
import java.io.*;
public class SalariedCompany extends Company
{
public static void main (String[ ] args) throws FileNotFoundException
{
new SalariedCompany().run();
}
protected SalariedEmployee getNextEmployee (Scanner sc)
{
Scanner lineScanner = new Scanner (sc.nextLine());
String name = lineScanner.next();
double salary = lineScanner.nextDouble();
double bonus = lineScanner.nextDouble();
return new SalariedEmployee (name, salary, bonus);
} // method getNextEmployee
} // class HourlyCompany
工薪员工和工薪公司是我需要交作业的文件。 了解上述课程内容的其他文件包括:
public class FullTimeEmployee implements Employee
{
protected /*private*/ String name;
protected /*private*/ double grossPay;
public FullTimeEmployee()
{
final String EMPTY_STRING = "";
name = EMPTY_STRING;
grossPay = 0.00;
}
public FullTimeEmployee (String name, double grossPay)
{
this.name = name;
this.grossPay = grossPay;
} // 2-parameter constructor
public String getName()
{
return name;
} // method getName
public double getGrossPay()
{
return grossPay;
} // method getGrossPay
public String toString()
{
final String EMPLOYMENT_STATUS = " FULL TIME";
return name + MONEY.format (grossPay) + EMPLOYMENT_STATUS;
// the format method returns a String representation of grossPay.
} // method toString
} // class FullTimeE
import java.util.*; // for the Scanner class
import java.io.*; // for the FileNotFoundException class � see Section 2.3
public class Company
{
public static void main (String[ ] args) throws FileNotFoundException
{
new Company().run();
} // method main
/**
* Determines and prints out the best paid of the full-time employees
* scanned in from a specified file.
*
*/
public void run() throws FileNotFoundException // see Section 2.3
{
final String INPUT_PROMPT = "Please enter the path for the file of employees: ";
final String BEST_PAID_MESSAGE =
"\n\nThe best-paid employee (and gross pay) is ";
final String NO_INPUT_MESSAGE =
"\n\nError: There were no employees scanned in.";
String fileName;
System.out.print (INPUT_PROMPT);
fileName = new Scanner (System.in).nextLine();
Scanner sc = new Scanner (new File (fileName));
FullTimeEmployee bestPaid = findBestPaid (sc);
if (bestPaid == null)
System.out.println (NO_INPUT_MESSAGE);
else
System.out.println (BEST_PAID_MESSAGE + bestPaid.toString());
} // method run
/**
* Returns the best paid of all the full-time employees scanned in.
*
* @param sc � the Scanner object used to scan in the employees.
*
* @return the best paid of all the full-time employees scanned in,
* or null there were no employees scanned in.
*
*/
public FullTimeEmployee findBestPaid (Scanner sc)
{
FullTimeEmployee full,
bestPaid = new FullTimeEmployee();
while (sc.hasNext())
{
full = getNextEmployee (sc);
if (full.getGrossPay() > bestPaid.getGrossPay())
bestPaid = full;
} //while
if (bestPaid.getGrossPay() == 0.00)
return null;
return bestPaid;
} // method findBestPaid
protected /*private*/ FullTimeEmployee getNextEmployee (Scanner sc)
{
Scanner lineScanner = new Scanner (sc.nextLine());
String name = lineScanner.next();
double grossPay = lineScanner.nextDouble();
return new FullTimeEmployee (name, grossPay);
} // method getNextEmployee
} // class Company
import java.text.DecimalFormat;
public interface Employee
{
final static DecimalFormat MONEY = new DecimalFormat (" $0.00");
String getName();
double getGrossPay();
String toString();
} // interface Employee
再次感谢你们
共 (0) 个答案