假设我们想要在全职,受薪公司员工的档案中确定最佳薪酬员工。对于受薪员工,文件中的每一行都包含姓名,工资和可选奖金。如果给予奖金,总薪酬是工资加奖金; 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
然后,收入最高的受薪员工是Chiger,Steve的总工资为480美元
每当我尝试运行程序时,它都不会读取文件的名称。我不知道我是否遗漏了SalariedEmployee或SalariedCompany中的某些内容,但我的代码如下。我基本上被卡住了。
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
SalariedEmployee和SalariedCompany是我需要为我的作业上交的文件。 其余的文件知道上面的类正在采取的是:
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 :(得分:0)
下面:
fileName = new Scanner (System.in).nextLine();
Scanner sc = new Scanner (new File (fileName));
...您从用户那里获取文件名。错误的基本可能性在于给出了表单文件路径。尝试修改代码:
fileName = new Scanner (System.in).nextLine();
File file = new File (fileName);
// print actual file absolute path
System.out.printf("File absolute path: %s", file.getAbsolutePath());
Scanner sc = new Scanner (file);
像往常一样运行应用程序,并查看实际绝对文件路径的输出。如果它与您的期望不匹配(例如,您输入salaries.txt
并且它为您提供C:\Users\user\...\folder-with-app\dist\salaries.txt
),则表示您将应用指向不同的salaries.txt
文件。
如果是这种情况,请尝试输入真实salaries.txt
文件的绝对路径,或将其移至File absolute path:
- s输出中指定的文件夹。