首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的代码中有错误吗?

我的代码中有错误吗?
EN

Stack Overflow用户
提问于 2016-05-29 17:54:42
回答 2查看 374关注 0票数 0

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

一旦读取了所有员工数据,就应该每年在控制台上显示一份报告。报告的每一行应包含为每个员工提供的所有原始数据以及该员工的年度年薪。对于这两年中的每一年,应计算和显示该年所有雇员的平均工资。

问:我的代码中有错误吗?或者我能修好的东西?

超类

代码语言:javascript
运行
复制
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

1子类

代码语言:javascript
运行
复制
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;
}

}

2子类

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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);

}//End Main

代码语言:javascript
运行
复制
  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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-29 22:41:57

我想出了一个读取单个行和整个txt文件的方法。它所做的就是遍历txt文件中的每一行,并将其拆分为“",这是一个空格。然后,由于值的顺序是相同的,所以我只是根据它们在行中的位置来设置值。

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

代码语言:javascript
运行
复制
// 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
}

抱歉,这一切都会出现在我怀疑的第四节课。我还以为它提到了你应该根据日期打印数据,所以我想知道你要用这个值做什么。

票数 0
EN

Stack Overflow用户

发布于 2016-05-29 18:25:33

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

代码语言:javascript
运行
复制
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类相同的文件夹中,并且您已经设置了一个包。

说出来,你就会没事的

代码语言:javascript
运行
复制
readFile(new File("src/employee.txt"));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37513100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档