我的代码中是否有任何错误?

时间:2016-05-29 17:54:43

标签: java class override text-files superclass

问题:最后应该有一个包含main方法的第四个类。它应该从文本文件中读取员工信息。文本文件的每一行代表一名员工一年的信息。文本文件的外观示例如下所示:

读入所有员工数据后,应在控制台上显示两年中的每一年的报告。报告的每一行应包含为每位员工提供的所有原始数据以及该员工当年的年薪。对于这两年中的每一年,应计算并显示该年度所有员工的所有工资的平均值。

问题:我的代码中是否有任何错误?或者我能解决的问题?

超级

public class Employee {

//Instance variables of super
private String name;
private double monthlySal;
private double annualSalary;

//Super constructor
public Employee(String name, double monthlySal) {
 this.name = name;//Initialization
 this.monthlySal = monthlySal;//Initialization
}//End of Constructor


//Method for annualSalary
public void annualSalary(double annualSalary){
    this.annualSalary = monthlySal * 12;
    System.out.println("Annual Salary: " +annualSalary);
}//End of Method


//toString Method
public String toString() {
    return "Employee Name: " + name + "\n" + "monthly Salary: " +monthlySal;
   }//End of toString Method
}//End of Employee Method

第一个子类

public class Salesman extends Employee {
private String name;
private double monthlySal;
private double annualSales;
private double commission;
private double annualSalary;//Annual Salary

//subclass Constructor
  public Salesman(String name, double monthlySal, double annualSalary,  double   commission) {
    super(name, monthlySal);
    this.annualSales = annualSales;
    this.commission = 0.2 * annualSales;
 }//End of Constructor



//Overridden Method
@Override
public void annualSalary(double annualSalary){
this.annualSalary = commission + super.annualSalary;

}//End of Override Method

//Overriden toString Method
@Override
public String toString(){
    return "Employee Name: " + name + "\n" + "Monthly Salary: " + monthlySal  + "\n" + "Annual Sales: " +annualSales;
}

}

第二个子类

public class Executive extends Employee {

private double stockPrice;
private String name;
private double monthlySal;
private double annualSalary; 
private double bonus = 0;


 //Constructor
 public Executive(String name, double monthlySal, double annualSalary, double stockPrice) {
    super(name, monthlySal);
    this.stockPrice = stockPrice;
    this.annualSalary = annualSalary;

    if(stockPrice >= 50) {
        bonus = 30000;
    } else {
        bonus = 0;
    }//End of If Me
}//End of Constructor

//Override Method for annualSalary
@Override
public void annualSalary(double annualSalary){
    this.annualSalary = super.annualSalary + bonus;
}//End of Override Method

//toString Override Method
@Override
public String toString() {
return "Employee Name: " + name + "\n" + "Monthly Salary: " + monthlySal + "\n" + "Current Stock Price: " +stockPrice;
}

}

文字档案 - employee.txt

2014 Employee Clark, Sam 3000
2014 Salesman Samson, Jones 4000 40000
2014 Executive Brandon, Thurman 10000 70
2015 Executive Brandon, Thurman 11000 70
2015 Salesman Samson, Jones 4500 30000
2015 Employee Clark, Sam 3500


//4th Class
import java.io.File;
import java.FileNotFoundException;

public class TestEmployee {


//Start Main
   public static void main(String[] args) {

   private double yearOneAverSalary = 55000;
   private double yearTwoAverSalary = 45000;


   System.out.println("The average combined yearly salary for year 2014  employees is: " + yearOneAverSalary);
   System.out.println("The average combined yearly salary for 2015 employees is: " +yearTwoAverSalary);

} //结束主要

  public static void readFile(File "src/employee.txt") throws IOException {
    String line;
    try(BufferedReader) br = Files.newBufferedReader(file.toPath()) {
        while((line = br.readLine())1=null) {
        System.out.println(line)//prints every line in file
          }//End of While
       }//End of Try
    }//End of ReadFile

2 个答案:

答案 0 :(得分:0)

这是我用来读取文件的方法。

public static void readFile(File file) throws IOException {
    String line;
    try(BufferedReader br = Files.newBufferedReader(file.toPath())) {
        while((line = br.readLine())!=null) {
            System.out.println(line) // prints every line of your file in the console

        }
    }
}

让我们假设employee.txt与您的java类位于同一个文件夹中,并且您没有设置包。

用它调用它你会没事的

readFile(new File("src/employee.txt"));

答案 1 :(得分:0)

我想出了一个读取单行和整个txt文件的方法。它的作用是通过txt文件中的每一行并用“”空格分隔它。然后因为值的顺序是相同的,我只是根据它们在行中的位置来设置值。

我建议看一下的是构造函数。我不确定日期值是否应该在Employee类中进行。

// reads a file and returns a array of employees found in the file
public Employee[] readEmployeeFile(String loc){
    BufferedReader read = new BufferedReader(new FileReader(loc)); // creating reader
    ArrayList<Employee> people = new ArrayList<Employee>(); // arraylist to store values of employees   

    // read all the lines in the file
    String line = "";
    while ((line=read.readLine())!=null){
        people.add(getEmployeeFromLine(line));
    }

    // close the reader
    read.close();

    // convert arraylist to array
    Employee[] returnvalues = new Employee[people];
    for (int q = 0; q < returnvalues.length; q++){
        returnvalues[q] = people.get(q);
    }

    return people;
}

// reads each individual line for the file
public Employee getEmployeeFromLine(String line){
    // format: date + job + name(contains 1 space) + monthly earnings + salesperson:annual sales | executive:stock price

    Employee returnvalue = null; // this is what we will return

    String[] values = line.split(" "); // the txt file seems to split up the values by a space so we split up the line by spaces

    // get all the values from the line
    Integer date = Integer.parseInt(values[0]); // date is the first value
    String position = values[1];
    String name = value[2]+values[3]; // we need both because there is a space in between the last name and first
    double monearns = Double.parseDouble(values[4]);

    double lastvalue;
    if (position.equals("Salesman") || position.equals("Executive")){ // only set the value if it exists otherwise we will get a index out of bounds exception
        lastvalue = values[5];
    }

    // you can structure the constructors how ever you would like - currently there is no value for date in the employee class
    if (position.equals("Employee")){
        returnvalue = new Employee(name, monearns, date);
    }
    else if (position.equals("Salesman")){
        returnvalue = new Salesman(name, monearns, date, lastvalue);
    }
    else if (position.equals("Executive")){
        returnvalue = new Executive(name, monearns, date, lastvalue);
    }
    return returnvalue;
}

public static void main(String[] args){
    Employee[] employees = readEmployeeFile("file.txt"); // if the file location needs to be user input I recommend just using a scanner

    // print out data
}

对于这种混乱感到抱歉,这一切都会在我怀疑的第四节课中出现。我还认为它提到你应该根据日期打印出数据,这就是为什么我想知道你将如何处理这个值。