2016-05-29 15 views
0

問題:最後に、mainメソッドを含む第4のクラスが存在するはずです。従業員情報をテキストファイルから読み込む必要があります。テキストファイルの各行は、1人の従業員の情報を1年間表示します。テキストファイルの表示例を以下に示します。コードにエラーがありますか?

すべての従業員データが読み込まれると、レポートはコンソールに2年ごとに表示されます。レポートの各行には、各従業員に提供された元のデータとその年の従業員の年間給与がすべて含まれている必要があります。 2年間ごとに、その年の全従業員の全給与の平均を計算して表示する必要があります。

質問:コードにエラーがありますか?あるいは私が修正できるものは?

スーパークラス

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; 
} 

} 

テキストファイル -

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 
+1

コードの問題点は何ですか? –

+0

セールスマンの手数料が20,000ドルに達していない可能性があります。 – Spidey

答えて

0

大丈夫です、私は個々のラインと全体のtxtファイルを読み込む方法を思い付きました。それは、txtファイルの各行を通り、スペースで区切られています。次に、値の順序が同じであるため、私は値がライン内のどこに基づいて設定されるのですか。

私が見てみることの1つは、コンストラクタです。私は、日付の値が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 
} 

混乱して申し訳ありませんが、これはすべて私が疑っている第4クラスに入ります。また、日付に基づいてデータを印刷する必要があると言われたので、その価値をどうしたらいいのか不思議でした。

+0

日付の価値はどういう意味ですか?また、このコードは実際にどこに行きますか?私はこのコードを第4回最終クラスまたは別のエリアに追加しますか?私はまた、私の4番目のクラスにpublic static mainメソッドを持っていると仮定しています。どのように私はこれを私の主な方法と一緒に入れるだろうか? –

+0

私は今あなたの日の意味を見ます。いいえ、私は日付価値を持っているとは思っていませんでしたが、傷つくことはありません。 –

+0

私は主な方法を追加し、答えに変更を加えた – Arthur

0

employee.txtこれは私がファイルを読み込むために使用されている方法です。

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")); 
+0

恐ろしい!ニコ助けてくれてありがとう。私はそれを試してみます –

+0

私のコードはどのように見えるのですか?あなたは何かエラーや何か良いことができますか? –

関連する問題