【问题标题】:Salariedemployee employee受薪雇员雇员
【发布时间】:2015-09-12 03:04:38
【问题描述】:

大家好,第一次来。我开始学习 Java,我们的第一个任务是:

假设我们想要确定公司全职员工档案中薪酬最高的员工。对于受薪员工,文件中的每一行都包含姓名、薪水和可选的奖金。如果有奖金,总工资是工资加奖金; 0 表示没有奖金。例如,假设文件“salaried.txt”包含以下内容:

Agrawal,Harsh 450.00  0

Chiger,Steve  420.00 60.00

Cromer,Jason  460.00  0

Petkov,Yuli   430.00 40.00

Siddiqi,Amena 460.00 15.00

那么薪水最高的员工是 Chiger,Steve,总工资为 480.00 美元

每次我尝试运行程序时,它都不会读取文件名。我不知道我是否在 SalariedEmployee 或 SalariedCompany 中遗漏了什么,但我拥有的代码如下。我基本上被卡住了。

import java.text.DecimalFormat;
public class SalariedEmployee extends FullTimeEmployee 
{
     protected double grossPay,    
                 bonus,
                 salary;

public SalariedEmployee()
{
    grossPay=0.00;
    bonus=0.00;  
    salary=0.00;
}
public SalariedEmployee (String name, double salary, double bonus) 
{        
    this.name = name;
    this.salary=salary;
    this.bonus=bonus;
    if (bonus==0)
    {
        grossPay+=salary;
    }
    else 
    {
        grossPay=salary+bonus;
    }
}
public boolean setSalaryPay(double income)
{
    if(income<0)
    {
        return false;
    }
    this.salary=income;
    return true;
}
public double getSalary()
{
         return salary;
         }
public double getBonus()
{
    return bonus;
}
public String toString()
{       
    final String FULL_TIME_STATUS = "FULL TIME SALARIED";
    return super.toString() + FULL_TIME_STATUS;
} // meth

}


import java.util.*;
import java.io.*;

 public class SalariedCompany extends Company
 {
public static void main (String[ ] args) throws FileNotFoundException 
{
    new SalariedCompany().run();
} 
protected SalariedEmployee getNextEmployee (Scanner sc)
{
    Scanner lineScanner = new Scanner (sc.nextLine());

    String name = lineScanner.next();       

    double salary = lineScanner.nextDouble();        

    double bonus = lineScanner.nextDouble();  

    return new SalariedEmployee (name, salary, bonus);
} // method getNextEmployee     

} // class HourlyCompany 

SalariedEmployee 和 SalariedCompany 是我需要为我的家庭作业上交的文件。 其余的文件知道上面的类是关于什么的:

 public class FullTimeEmployee implements Employee
 {   
protected /*private*/ String name;

protected /*private*/ double grossPay; 
public FullTimeEmployee()
{
     final String EMPTY_STRING = "";

     name = EMPTY_STRING;
     grossPay = 0.00;
} 
public FullTimeEmployee (String name, double grossPay)
{  
    this.name = name;
    this.grossPay = grossPay;         
} // 2-parameter constructor

public String getName()
{
    return name;
} // method getName


public double getGrossPay()
{
    return grossPay;
} // method getGrossPay



public String toString()
{       
   final String EMPLOYMENT_STATUS = " FULL TIME";

     return name + MONEY.format (grossPay) + EMPLOYMENT_STATUS; 
     // the format method returns a String representation of grossPay.   
} // method toString

 } // class FullTimeE

import java.util.*; // for the Scanner class

import java.io.*;  // for the FileNotFoundException class � see Section 2.3

 public class Company
 {       

public static void main (String[ ] args) throws FileNotFoundException
{
    new Company().run();
} // method main

/**
 *  Determines and prints out the best paid of the full-time employees 
 *  scanned in from a specified file. 
 *
 */  
public void run() throws FileNotFoundException  // see Section 2.3            
{
    final String INPUT_PROMPT = "Please enter the path for the file of employees: ";

    final String BEST_PAID_MESSAGE = 
     "\n\nThe best-paid employee (and gross pay) is ";

   final String NO_INPUT_MESSAGE = 
    "\n\nError: There were no employees scanned in.";

   String fileName;

   System.out.print (INPUT_PROMPT);
   fileName = new Scanner (System.in).nextLine();
   Scanner sc = new Scanner (new File (fileName));

   FullTimeEmployee bestPaid = findBestPaid (sc);

   if (bestPaid == null)
       System.out.println (NO_INPUT_MESSAGE);
   else
       System.out.println (BEST_PAID_MESSAGE + bestPaid.toString());
} // method run


/**
 *  Returns the best paid of all the full-time employees scanned in. 
 *  
 *  @param sc � the Scanner object used to scan in the employees.
 *
 *  @return the best paid of all the full-time employees scanned in,
 *                 or null there were no employees scanned in.
 *
 */  
public FullTimeEmployee findBestPaid (Scanner sc)
{
    FullTimeEmployee full, 
                     bestPaid = new FullTimeEmployee();                            

    while (sc.hasNext())
    {                               
         full = getNextEmployee (sc);           
         if (full.getGrossPay() > bestPaid.getGrossPay())
            bestPaid = full; 
    } //while   
    if (bestPaid.getGrossPay() == 0.00)    
      return null;
    return bestPaid;
} // method findBestPaid



     protected /*private*/ FullTimeEmployee getNextEmployee (Scanner sc)
     {
        Scanner lineScanner = new Scanner (sc.nextLine());

        String name = lineScanner.next();

        double grossPay = lineScanner.nextDouble();

        return new FullTimeEmployee (name, grossPay);
     } // method getNextEmployee

 } // class Company




import java.text.DecimalFormat; 

public interface Employee
{

    final static DecimalFormat MONEY = new DecimalFormat (" $0.00");
    String getName();

    double getGrossPay();

    String toString();

 } // interface Employee

再次提前感谢你们。

【问题讨论】:

标签: java


【解决方案1】:

这里:

fileName = new Scanner (System.in).nextLine();
Scanner sc = new Scanner (new File (fileName));

...您从用户那里获取文件名。错误的基本可能性是给出的表单文件路径。尝试修改代码:

fileName = new Scanner (System.in).nextLine();
File file = new File (fileName);
// print actual file absolute path
System.out.printf("File absolute path: %s", file.getAbsolutePath());

Scanner sc = new Scanner (file);

像往常一样运行应用程序,并查看实际绝对文件路径的输出。如果它不符合您的期望(例如,您输入 salaries.txt,它会为您提供 C:\Users\user\...\folder-with-app\dist\salaries.txt),那么这意味着您将您的应用程序指向不同的 salaries.txt 文件。

如果是这种情况,请尝试输入真正的salaries.txt 文件的绝对路径,或将其移动到File absolute path:-s 输出中指定的文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多