當我運行以下代碼時,它僅打印「year \ tposition \ tname \ tMonthlySalary \ tAnnualSales \ tStockPrice」,而不是存儲在data.txt中的信息。它不計算2014年和2015年的平均值。我使用的是NetBeans代碼,並沒有看到任何錯誤。將文本文件調用到java
data.txt中有
2014員工史密斯,詹姆斯2000
2014業務員約翰遜,瑪麗3500 10000
2014執行威廉姆斯,羅伯特2800 25
2015年員工瓊斯,芭芭拉4000
2015業務員布朗威廉3700 5000
2015年執行戴維斯,珍妮弗2600 51
2014員工米勒,理查德3900
2014業務員威爾遜,蘇珊4200 7000
2014執行摩爾定律,約瑟夫1900 65
2015年員工泰勒,多蘿西3700
2015業務員Anderson,Christopher 3200 15000
2015年執行托馬斯,南希4400 33
class Employee{
private String year;
private String name;
private String position;
private double monthlySalary;
public Employee(String year, String position, String name, double monthlySalary){
this.year = year;
this.position = position;
this.name = name;
this.monthlySalary = monthlySalary;
}
public double annualSalary(){
return monthlySalary*12;
}
public String toString(){
return year+"\t"+position+"\t"+name +"\t" + monthlySalary +"\t";
}
}class Salesman extends Employee{
private double annualSales;
public Salesman(String year, String position, String name, double monthlySalary, double annualSales){
super(year, position, name, monthlySalary);
this.annualSales = annualSales;
}
public double annualSalary(){
double commission = this.annualSales*0.2;
if(commission>20000){
commission = 20000;
}
return super.annualSalary()+commission;
}
public String toString(){
return super.toString()+annualSalary();
}
}class Executive extends Employee{
private double stockPrice;
public Executive(String year, String position, String name, double monthlySalary, double stockPrice){
super(year, position, name, monthlySalary);
this.stockPrice = stockPrice;
}
public double annualSalary(){
double bonus=0;
if(this.stockPrice>50){
bonus = 30000;
}
return super.annualSalary() + annualSalary();
}
public String toString(){
return super.toString()+"\t"+ this.stockPrice+"\n";
}
}
class TestEmployee {
ArrayList<Employee> employee2014 = new ArrayList<Employee>();
ArrayList<Employee> employee2015 = new ArrayList<Employee>();
public void MakeArray(){
try{
Scanner sc = new Scanner(new File("data.txt"));
while(sc.hasNext()){
String year = sc.nextLine();
String[] data = year.split(" ");
if(data[0] == "2014"){
if(data[1].equalsIgnoreCase("Employee")){
employee2014.add(new Employee(data[0],data[1],data[2],Double.parseDouble(data[3])));
}else if(data[1].equalsIgnoreCase("Salesman")){
employee2014.add(new Salesman(data[0],data[1],data[2],Double.parseDouble(data[3]),Double.parseDouble(data[4])));
}else if(data[1].equalsIgnoreCase("Executive")){
employee2014.add(new Executive(data[0],data[1],data[2],Double.parseDouble(data[3]),Double.parseDouble(data[4])));
}
}if(data[0] == "2015"){
if(data[1].equalsIgnoreCase("Employee")){
employee2015.add(new Employee("2015",data[1],data[2],Double.parseDouble(data[3])));
}else if(data[1].equalsIgnoreCase("Salesman")){
employee2015.add(new Salesman("2015",data[1],data[2],Double.parseDouble(data[3]),Double.parseDouble(data[4])));
}else if(data[1].equalsIgnoreCase("Executive")){
employee2015.add(new Executive("2015",data[1],data[2],Double.parseDouble(data[3]),Double.parseDouble(data[4])));
}
}
}
}catch(FileNotFoundException nf){
System.out.println("File not found");
}
}public void Result(){
double average2014=0;
double total2014=0;
double average2015=0;
double total2015=0;
System.out.print("Data of 2014:\n"
+ "Year\tPosition\tName\tMonthlySalary\tAnnualSales\tStockPrice");
for(Employee employee14: employee2014){
System.out.println(employee14.toString());
total2014 +=employee14.annualSalary();
}average2014 = total2014/employee2014.size();
System.out.print("\nThe average annual salary of 2014 is " + average2014+"\n");
System.out.println("==================================");
System.out.print("Data of 2015:\n"
+ "Year\tPosition\tName\tMonthlySalary\tAnnualSales\tStockPrice");
for(Employee employee15: employee2015){
System.out.println(employee15.toString());
total2015 +=employee15.annualSalary();
}average2015 = total2014/employee2015.size();
System.out.print("\nThe average annual salary of 2015 is " + average2015+"\n");
}
public static void main(String[] args){
TestEmployee te = new TestEmployee();
te.MakeArray();
te.Result();
}
}