当前位置: 首页 > 知识库问答 >
问题:

每个员工的calculateWeeklypay方法有问题

盛嘉
2023-03-14

好吧,我现在有四种类型的员工。经理(每周领取固定工资)、设计工人(在工作的前40小时内获得固定的小时工资,以及“一个半时间”,即加班时间的1.5倍于他们的小时工资)、销售人员(他们获得250美元加上每周总销售额的5.7%)和制造业(他们生产的每件物品每件物品获得固定金额的钱 - 该公司的每个制造商只从事一种类型的项目)。这是我的类:员工类的问题是我得到这个输出:

You chose to open this file: Employees.txt
John Smith Manufacturing 6.75 120 444
0.0
Betty White Manager 1200.0 0 111
0.0
Stan Slimy Sales 10000.0 0 332
250.0
Betty Boop Design 12.5 50 244
0.0

这意味着它为我的所有计算都得到0,并导致这个每周报告:

WEEKLY PAY REPORT FOR Wacky Widgets COMPANY
Employee
0
    WEEKLY PAY 
250
Total Payroll: $0.0
Total number of managers paid:1
Total number of Design Employees paid:1
Total number of Sales Employees paid:1
Total number of Manufacturing Employees paid:1

我需要帮助来修复我的方法,这样它们就可以接受文件的整数并在计算中使用它们,因为我现在得到的都是0值。如果需要,我可以提供我读文件方法。谢谢

这里是抽象类,所有其他employees类型都是从[code]扩展的

public abstract class Employee {

    public abstract double calculateWeeklyPay();

    private String fName;
    private String lName;

    private int EmpId;
    private Position p; 
    /*
       public Employee()
       {

       }*/

    /**
     * default constructor
     */

    public Employee() {
        // TODO Auto-generated constructor stub
        /*this.getFName();
        this.getLName();
        this.getEmpId();*/
        getFName();
        getLName();
        this.getEmpId();
    }


    /**
     * @param string  first name
     * @param string2 last name
     * @param y       position
     */
    public Employee(String string, String string2, String y) {


        fName = string;
        lName = string2;
        // EmpId=string3;
        if (y.equals("Design")) {
            p = Position.Design;
        }
        if (y.equals("Sales")) {
            p = Position.Sales;
        }
        if (y.equals("Manufacturing")) {
            p = Position.Manufacturing;
        }
        if (y.equals("Manager")) {
            p = Position.Manager;
        }
        //  StringTokenizer str=new StringTokenizer(fName+ lName+y,": .");

    }


    /**
     * @return first name
     */
    public String getFName() {
        return fName;
    }

    /**
     * @param firstName sets the first name
     */
    public void setFName(String firstName) {
        this.fName = firstName;
    }

    /**
     * @return last name
     */
    public String getLName() {
        return lName;
    }

    /**
     * @param lastName
     */
    public void setLName(String lastName) {
        this.lName = lastName;
    }

    /*public String toString()
    {

    //  String str =  firstName+" "+lastName+" "+"Position: "+getPosition();
        return firstName;
        }*/

    /**
     * @return p position
     */
    public Position getPosition() {

        //  String str =  firstName+" "+lastName+" "+"Position: "+getPosition();
        return p;
    }

    public String toString() {
        String str = fName + " " + lName + " " + "Position: " + getPosition();
        return str;

    }//https://www.udemy.com/blog/java-abstract-class/


    public int getEmpId() {
        return EmpId;
    }

    public void setEmpId(int empId) {
        this.EmpId = empId;
    }

}

员工类的设计

public class Design extends Employee {
    public double rate;//rate for each hours
    public double h;//hours worked

    public Design(String fName, String lName, String pos) {
        // TODO Auto-generated constructor stub
        //double firstParam, int secondParam, int empNum
        super(fName, lName, pos);
        //toString();

        calculateWeeklyPay();
    }

    @Override
    public double calculateWeeklyPay() {
        // TODO Auto-generated method stub
        double weeklyPay = 0;
        if (h <= 40) {
            weeklyPay = h * rate;
        }
        if (h > 40) {
            h = h * (3 / 2);
            weeklyPay = h * rate;
        }
        //System.out.println(weeklyPay);
        return weeklyPay;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

}

销售人员

 public class Sales extends Employee {

    private double weekSales;
    private final double pay = 250;
    private final double percent = .057;


    public Sales(String fName, String lName, String pos) {
        // TODO Auto-generated constructor stub
        super(fName, lName, pos);


        this.calculateWeeklyPay();
    }

    @Override
    public double calculateWeeklyPay() {
        // TODO Auto-generated method stub
        double weekPay;
        weekPay = pay + percent * this.getWeekSales();
        return weekPay;
    }

    public double getWeekSales() {
        return weekSales;
    }

    public void setWeekSales(double weekSales) {
        this.weekSales = weekSales;
    }

}

我还有另外两个雇员类型的类,但是这里不需要它们,因为如果我解决了这个类中的问题,我就可以解决其他的。这是我的公司类,它读取一个文件,计算所有雇员类的周薪和总周薪

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JOptionPane;


public class Company implements CompanyInterface {
    //private Employee addEmp = new Employee();
    //  private Position addPosition = new 
    private ArrayList<Employee> list;
    private static int numberOfCompanies;
    private String CompanyName;
    private int numDesign;
    private int numEmployees;
    private int numManufacturing;
    private int numManager;
    private int numSales;
    /*String fName="";
    String lName="";
    String position="";
    String first="";
    String second="";
    String empID="";
    Scanner File;*/
    //private ArrayList<Employee> list2 ;
    private Employee addEmp;

    /**
     * @param cn is the company name
     */
    public Company(String cn) {

        cn = "Wacky Widgets";
        list = new ArrayList<Employee>();
        numDesign = 0;
        numEmployees = 0;
        numManufacturing = 0;
        numSales = 0;

        numberOfCompanies++;
        setCompanyName(cn);

    }

    @Override
    /**
     * @param fName first name
     * @param lName last name
     * @param pos position
     * @return null if everything is right
     */


    public String addEmployee(String fName, String lName, String pos,
                              double firstParam, int secondParam, int empNum) {
        String message = null;
        //  if (pos.equals("DESIGN")&&       

        numDesign == 1 && numSales != 2 && numManufacturing != 4)
        if (pos.equals("Design")) {

            if (numDesign == 2)//p2=Position.DESIGN;
            {

                message = "There are already 2 design persons\nEmployee not          
                added ";

            } else {
                addEmp = new Design(fName, lName, pos);
                numDesign++;
                numEmployees++;
                list.add(addEmp);
            }


        }//else return message;
        //if (pos.equals("SALES")&&numDesign<1&&numSales<2&&numManufacturing<4)
        else if (pos.equals("Sales")) {
            if (numSales == 1) {
                message = "There is already a sales person\nEmployee not    
                added ";


            } else {
                //p2=Position.SALES;
                addEmp = new Sales(fName, lName, pos);
                numSales++;
                numEmployees++;
                list.add(addEmp);
            }

        }//else return message;
        else if (pos.equals("Manufacturing")) {
            if (numManufacturing == 4) {
                message = "There are already four manufacturing persons     
                \nEmployee not added ";
            } else {
                //p2=Position.MANUFACTURING;
                addEmp = new Manufacturing(fName, lName, pos);
                numManufacturing++;
                numEmployees++;
                list.add(addEmp);
            }
        } else if (pos.equals("Manager")) {
            if (numManager == 1) {
                message = "There is already a manager person\nEmployee not 
                added ";
            } else {
                //p2=Position.MANUFACTURING;
                addEmp = new Manufacturing(fName, lName, pos);
                numManager++;
                numEmployees++;
                list.add(addEmp);
            }
        }
        //String str=fName+" "+lName+" "+pos+" "+firstParam+" "+empNum;
        System.out.println(fName + " " + lName + " " + pos + " " + firstParam + " " + secondParam + " 
                "+empNum);
                //firstParam=Employee.
                System.out.println(calculateTotalWeeklyPay());

        return message;

    }

    /**
     * Returns number of companies
     *
     * @return number of companies
     */
    public static int getNumCompanies() {
        return numberOfCompanies;
    }

    public void setNumCompanies(int nc) {
        numberOfCompanies = nc;
    }

    /**
     * Sets the number of employees
     *
     * @param ne number of employees
     */
    public void setNumemployees(int ne) {
        numEmployees = ne;
    }

    /**
     * Returns the number of employees
     *
     * @return the number of employees
     */
    public int getNumEmployees() {
        return numEmployees;
    }

    @Override
    public int getNumManager() {

        return numManager;
    }

    /**
     * sets the number of designer
     *
     * @param nd number of designer
     */
    public void setNumDesign(int num) {
        numDesign = num;
    }

    @Override
    public int getNumDesign() {

        return numDesign;
    }

    public void setNumsales(int num) {
        numSales = num;
    }

    @Override
    public int getNumSales() {

        return numSales;
    }

    /**
     * Sets the number of manufacturer
     *
     * @param nm the number of manufacturer
     */
    public void setNumManufacturing(int num) {
        numManufacturing = num;
    }

    @Override

    public int getNumManufacturing() {

        return numManufacturing;
    }

    public void setNumManager(int num) {
        numManager = num;
    }

    @Override
    public String generateWeeklyReport() {

        int empID = 0;
        String title = "WEEKLY PAY REPORT FOR " + getCompanyName() + " COMPANY" + "\n";
        String label = "Employee\n";
        String label2 = "WEEKLY PAY \n";
        int payWeek = 0;

        double total = 0.0;

        for (Employee index : list) {

            empID += index.getEmpId();
            payWeek += index.calculateWeeklyPay();

        }

        return title + label + empID + "\n" + "\t" + label2 + payWeek + "\n" + "Total Payroll: 


        $ "+total+"\n "+
        "Total number of managers paid:" + getNumManager() + "\n" +
                "Total number of Design Employees paid:" + getNumDesign() + "\n" +
                "Total number of Sales Employees paid:" + getNumSales() + "\n" +
                "Total number of Manufacturing Employees paid:" + getNumManufacturing();
    }

    @Override
    public double calculateTotalWeeklyPay() {

        double total = 0;

        for (int index = 0; index < list.size(); index++) {
            total = list.get(index).calculateWeeklyPay();
            //total.add(list.get(index).calculateWeeklyPay());


        }
        return total;
    }


    /**
     * @return str the arraylist of employees and positions
     */

    public String printCompany() {
        // TODO Auto-generated method stub
        String str = "";
        for (int index = 0; index < list.size(); index++) {
            str += list.get(index).getFName() + " " + list.get(index).getLName() + " 
            "+" Position:
            "+list.get(index).getPosition()+"\n ";

        }//System.out.println(str);
        return str;


    }

    /**
     * @return company name
     */
    public String getCompanyName() {
        return CompanyName;
    }

    public void setCompanyName(String companyName) {
        //companyName="Wacky Widgets";
        this.CompanyName = companyName;
    }


    public String toString() {
        //  int i =0;
        String str = CompanyName + "";
        //String str = "";
        for (int i = 0; i < list.size(); i++) {
            str += "\n" + list.get(i).getFName() + " " + list.get(i).getLName() + " 
            "+" Position:
            "+list.get(i).getPosition()+"\n ";
        }


        return str;
    }

}

共有1个答案

公孙高轩
2023-03-14

这一点很突出:

h = h * (3 / 2);

你在这里做整数除法,所以你总是乘以1,有效地。

将其更改为使用浮点值。

h = h * (3.0 / 2);

或者,明确你乘以的是什么——你知道总是一个半的时间,所以我看没有理由不这样做:

h = h * 1.5;
 类似资料:
  • Hybris:1905.9(也用1905.12测试) 我创建了一个,密码为,使用下面的impex。我将配置为拥有创建员工和客户的用户访问权限,以及查看用户组的权限。 通过Backoffice,这个可以创建一个客户,但在尝试创建员工时会导致错误。 我错过了什么?我是否也需要向其他类型添加UAC权限? 注意事项: 一个,属于组不能创建一个雇员或一个客户 OOTB用户可以创建一个员工 属于的员工可以创建

  • 问题内容: 我必须对一个大型Java项目做一个一般性的说明,但是我对它的了解很少,我想知道是否有确定以下内容的准则: 每个包有多少个类可以被认为是正确的,低或高的(这个项目每个包有3.89个类,对我来说似乎太小了), 每个类有多少种方法?(该项目每个类有6.54个方法… 每个方法的行数?(此项目每种方法大约有7行(对我来说似乎不错,也许有点低)) 我应该指出,这个问题仅涉及体积。我有很多来自质量工

  • 我想要工资的百分比栏为(工资/总工资)*100。 我试过这个,但它总是给0。 我甚至不会应用乘法运算。查询给出了应用乘以100时的错误。

  • 问题内容: 从另一个问题中,我了解到在Java中 可以为Enum的每个实例定义特定的方法 : 让我感到惊讶的是,这甚至是可能的,每个实例专用的“专有方法”是否都有 名称 来查找文档? 另外, 应该如何使用 ?由于下一个未编译: 我应该如何使用这些“专有”方法? 问题答案: 您不能引用这些方法,因为您正在有效地为每个枚举创建匿名(*)类。由于它是匿名的,因此只能从匿名类本身内部或通过反射引用此类方法

  • 问题内容: 不太确定如何获得这个。我有一个职员表,我需要找到平均工资。我知道我可以使用use 。但是,诀窍是我需要找到拥有5名以上职员的部门的平均值。我不确定是否应使用分组依据或使用方式。谢谢! 问题答案:

  • 好的,我有一个课堂作业,为老师写的一个石头,纸,剪刀程序创建3个预先确定的方法。但是,当我运行程序时,它会连续多次运行这些方法。我把代码看了好几遍,还是弄不明白问题所在。 下面是老师提供的部分课程: 以下是该程序的示例会话: 欢迎来到石头,纸,剪刀! 你想打几局?1输入你的选择:岩石电脑选择纸张输入你的选择:岩石输入你的选择:岩石电脑赢!