2016-03-26 5 views
-3

Arbeiten an einem Java-Programm, das Gehälter für eine Sammlung von Mitarbeitern verschiedener Typen berechnet, die aus einer TXT-Datei importiert wird.Importieren Sie Daten in Datei und verwenden Sie, um Gehalt zu berechnen

Das Programm baut und erfolgreich läuft, wenn ich auf Kommentar:

System.out.println("\nAverage Salary for 2014: " + (totalSalary/indexYear2014)); 

System.out.println("\nAverage Salary for 2015: " + (totalSalary/indexYear2015)); 

aber es scheint nicht, dass die Daten importiert/überhaupt verwendet. Hier

ist der vollständige Code:

package computeemployeesalary; 

/** 
* 
* @author Jason Snook 
* Course name: Intermediate Programming (CMIS 242) 
* Assignment: Project 1 - Compute Employee Salary 
*/ 

/** 
* This program computes the salaries of a collection of employees of 
* different types: Employee, Salesman, Executive 
* 
* Salary for employee is calculated by multiplying monthly salary by 12 
* 
* Salary for salesman takes the base of employee, and then adds commission, 
* which has a cap of 20000 
* 
* Salary for executive takes the base of employee, and then adds a bonus 
* in the event that the current stock is more than 100 
*/ 

// Imports 
import java.util.Scanner; 
import java.io.*; 

/** 
* This class contains the employee name and monthly salary 
*/ 
// Employee class 
class Employee { 
    // Variable declaration 
    String employeeName; 
    int monthlySalary = 0; 

    // Employee constructor 
    public Employee(String employeeName, int monthlySalary) { 
     this.employeeName = employeeName; 
     this.monthlySalary = monthlySalary; 
    } // end Employee constructor method 

    // Calculate employee annual salary 
    public int annualSalary() { 
     return monthlySalary * 12; 
    } // end annualSalary method 

    // Return employee name and annual salary as String 
    @Override // takes advantage from compiler checking 
    public String toString() { 
     return "Employee Name: " + employeeName + 
       " Salary: $" + monthlySalary; 
    } // end toString method 
} // end employee class 

/** 
* This class contains the salesman annual sales and extends employee class 
*/ 
// Salesman subclass, extends Employee class 
class Salesman extends Employee { 
    // Variable declaration 
    int annualSales = 0; 

    // Salesman constructor 
    public Salesman(String employeeName, int monthlySalary, int annualSales) { 
     super(employeeName, monthlySalary); 
     this.annualSales = annualSales; 
    } // end Salesman constructor method 

    @Override // takes advantage from compiler checking 
    // Computes commission and annual salary 
    public int annualSalary() { 
     int commission = annualSales * 3/100; 

     // if commission is greater than 25000, then set commission to 25000 
     if (commission > 25000) { 
      commission = 25000; 
     } // emd comission if 

     return (monthlySalary * 12 + commission); 
    } // end annualSalary method 

    @Override // takes advantage from compiler checking 
    // Displays output details as String 
    public String toString(){ 
     return "Employee Name: " + employeeName + 
       " Salary: $" + monthlySalary; 
    } // end toString method 
} // end Salesman class 

/** 
* This class contains the current stock price and extends employee class 
*/ 
// Executive subclass, extends Employee class 
class Executive extends Employee { 
    // Variable declaration 
    int currentStockPrice = 0; 

    // Executive constructor 
    public Executive(String employeeName, 
      int monthlySalary, int currentStockPrice) { 
     super(employeeName, monthlySalary); 
     this.currentStockPrice = currentStockPrice; 
    } // end Executive constructor method 

    @Override // takes advantage from compiler checking 
    // Computes commission and annual salary 
    public int annualSalary() { 
     int executiveBonus = 0; 

     // Adds executive bonus if stock is greater than 100 
     if(currentStockPrice > 100) { 
      executiveBonus = 20000; 
     } // end executiveBonus if 

     return (monthlySalary * 12 + executiveBonus); 
    } // end annualSalary method 

    @Override // takes advantage from compiler checking 
    // Displays output details as String 
    public String toString() { 
     return "Employee Name: " + employeeName + 
       " Salary: $" + monthlySalary; 
    } // end toString method 
} // end Executive class 

/** 
* This class computes salary based on input file and reports results 
*/ 
public class ComputeEmployeeSalary { 
    // Main method 
    public static void main(String[] args) throws IOException { 
    // Import text file and compute salary increase 

     try { 
      // Create a File instance 
      java.io.File file = new java.io.File("employees.txt"); 

      // Create Scanner object 
      Scanner input = new Scanner(file); 

      // Create arrays for 2014 and 2015 
      Employee year2014[] = new Employee[20]; 
      Employee year2015[] = new Employee[20]; 

      // Create indexes for 2014 and 2015 arrays 
      int indexYear2014 = 0, indexYear2015 = 0; 

      // Read data from a file 
      while (input.hasNext()) { 
       String year = input.next(); 
       String employeeTitle = input.next(); 
       String employeeName = input.next(); 
       int monthlySalary = input.nextInt(); 

       // Action if employee is a regular employee. 
       if (employeeTitle.equalsIgnoreCase("Employee")) { 
        Employee regularEmployee = new Employee(employeeName, 
          monthlySalary); 

        if (year == "2014") { 
         year2014[indexYear2014++] = regularEmployee; 
        } // end 2014 if 

        if (year == "2015") { 
         year2015[indexYear2015++] = regularEmployee; 
        } // end 2015 if 
       } // end Employee if 

       // Action if employee is a salesman. 
       if (employeeTitle.equalsIgnoreCase("Salesman")){ 
        int annualSales = input.nextInt(); 

        Salesman salesEmployee = new Salesman(employeeName, 
          monthlySalary, annualSales); 

        if (year == "2014") { 
         year2014[indexYear2014++] = salesEmployee; 
        } // end 2014 if 

        if (year == "2015") { 
         year2015[indexYear2015++] = salesEmployee; 
        } // end 2015 if 
       } // end Salesman if 

       // Action if employee is an executive. 
       if (employeeTitle.equalsIgnoreCase("Executive")) { 
        int currentStockPrice = input.nextInt(); 

        Executive executiveEmployee = new Executive(employeeName, 
        monthlySalary, currentStockPrice); 

        if (year == "2014") { 
         year2014[indexYear2014++] = executiveEmployee; 
        } // end 2014 if 

        if (year == "2015") { 
         year2015[indexYear2015++] = executiveEmployee; 
        } // end 2015 if 
       } // end Executive if 

      } // end While 

      // Generate Report for 2014 
      int totalSalary = 0; 

      System.out.println("===== Salary Report for 2014 ====="); 

      for (int i = 0; i < indexYear2014; i++) { 
       System.out.print(year2014[i]); 
       System.out.println(" Annual Salary: " + 
         year2014[i].annualSalary()); 
      } // end annualSalary 2014 for 

      for (int i = 0; i < indexYear2014; i++) { 
       totalSalary+= year2014[i].annualSalary(); 
      } // end totalSalary 2014 for 

      System.out.println("\nAverage Salary for 2014: " + 
        (totalSalary/indexYear2014)); 

      // Generate Report for 2015 
      System.out.println("\n===== Salary Report for 2015 ====="); 

      for (int i = 0; i < indexYear2015; i++) { 
       System.out.print(year2015[i]); 
       System.out.println(" Annual Salary: " + 
         year2015[i].annualSalary()); 
      } // end annualSalary 2015 for 

      for (int i = 0; i < indexYear2015; i++) { 
       totalSalary+= year2015[i].annualSalary(); 
      } // end totalSalary 2015 for 

      System.out.println("\nAverage Salary for 2015: " + 
        (totalSalary/indexYear2015)); 

      // Close input file 
      input.close(); 
     } // end try 
     catch (IOException i) { 
      System.out.println("Error: File Not Found, error code: " + i); 
     } // end catch 
    } // end main method 
} // end ComputeEmployeeSalary class 

Jede Hilfe wäre sehr geschätzt.

+1

wow, viele Zeilen ... aber Sie sollten uns die Fehlermeldung geben , ein Stack-Trace oder etwas ähnliches, siehe http://stackoverflow.com/help/how-to-ask –

+0

Außerdem haben Sie nicht wirklich eine Frage gestellt. – mwm314

+0

Wie andere Benutzer gesagt haben, sollten Sie klar angeben, was das Programm tun soll und was es stattdessen tut. Außerdem sollten Sie Ihren Code auf einen [SSCCE] (http://www.sscce.org/) reduzieren. Sie werden viel eher auf diese Weise Hilfe bekommen. – elhefe

Antwort

1

Der Grund, warum es funktioniert, wenn Sie diese 2 Zeilen auskommentieren, ist, dass sie einen Fehler durch Division durch Null verursachen. Sowohl indexYear2014 und indexYear2015 0.

Der Grund dafür ist die Art und Weise Sie Strings sind zu vergleichen:

year == "2015"year.equals("2015") sollte

+0

Danke Adam, das hat mein Problem gelöst. –

Verwandte Themen