0

我正在处理的问题是我无法弄清楚如何使我的类中的某些值出现,它们只返回为零。我是初学者,所以我对 Java 了解不多,但我必须制作 4 个类或 3 类员工和一个类来输出他们的值。但我无法得到委员会和工会所雇用的价值观。这是代码:

import java.util.Scanner;

// Base or Superclass
class Employee
{
    String name;
    String department;
    double pay;
    double hours;
    double money;
    double mission;
    double rate;
    double withheld;
    double moneyc;
    double moneyu;
    double sales;

    public void inputs()
    {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter your name: ");
        name = in.nextLine();

        System.out.println("Enter your department: ");
        department = in.nextLine();

        System.out.println("Enter your pay per hour: ");
        pay = in.nextDouble();

        System.out.println("Enter how many hours you worked: ");
        hours = in.nextDouble();

        System.out.println("Enter your rate of commission(0.00): ");
        rate = in.nextDouble();

        System.out.println("Enter your withheld amount: ");
        withheld = in.nextDouble();

        System.out.println("Enter your sales amount: ");
        sales = in.nextDouble();

        money = pay * hours;
    }

    // Accessor Methods
    public String getName()
    {
        return this.name;
    }

    public String getDepartment()
    {
        return this.department;
    }

    public Double getPay()
    {
        return this.pay;
    }

    public Double getHours()
    {
        return this.hours;
    }

    public Double getMoney()
    {
        return this.money;
    }

    public Double getMission()
    {
        return this.mission;
    }

    public Double getRate()
    {
        return this.rate;
    }

    public Double getWithheld()
    {
        return this.withheld;
    }

    public Double getMoneyc()
    {
        return this.moneyc;
    }

    public Double getMoneyu()
    {
        return this.moneyu;
    }

    public Double getSales()
    {
        return this.sales;
    }

    // Mutator Methods
    public void setName(String n)
    {
        name = n;
    }

    public void setDepartment(String d)
    {
        department = d;
    }

    public void setPay(double p)
    {
        pay = p;
    }

    public void setHours(double h)
    {
        hours = h;
    }

    public void setMoney(double m)
    {
        money = m;
    }

    public void setMission(double mi)
    {
        mission = mi;
    }

    public void setRate(double r)
    {
        rate = r;
    }

    public void setWithheld(double w)
    {
        withheld = w;
    }

    public void setMoneyc(double mc)
    {
        moneyc = mc;
    }

    public void setMoneyu(double mu)
    {
        moneyu = mu;
    }

    public void setSales(double s)
    {
        sales = s;
    }
}

class Last extends Employee
{
    public void dinero()
    {
        Employee one = new Employee();

        one.inputs();

        // Union Employee
        UnionEmployee three = new UnionEmployee();
        three.Syndicate();

        // Commission Employee
        Commissioned two = new Commissioned();
        two.sales();

        System.out.println("\n"+ "Name: "+ one.getName());
        System.out.println( "Department: "+ one.getDepartment());
        System.out.println( "Hours: "+ one.getHours());
        System.out.println( "Pay Rate: "+ one.getPay());
        System.out.println("Your money is: "+ one.getMoney());

        // Commissioned Employee
        System.out.println( "\n"+ "Commissioned Employee");
        System.out.println("Your money is: "+ one.getMoneyc());
        System.out.println( "Your commission is: "+ one.getMission());
        System.out.println("Your rate: "+ one.getRate());

        // Union employee
        System.out.println("\n"+"Union Employee");
        System.out.println("Your money is: "+ one.getMoneyu());
        System.out.println( "Your withheld is: "+ one.getWithheld());
    }
}

// Derived or Subclass
class Commissioned extends Employee
{
    public void sales()
    {
        moneyc = hours * pay;
        // Commission
        mission = sales * rate;
    }
}

// Derived or Subclass
class UnionEmployee extends Employee
{
    public void Syndicate()
    {
        if (hours <= 40) {
            moneyu = (hours * pay) - withheld;
        } else  {
            moneyu = (pay * hours * ((hours - 40) * 1.5)) - withheld;
        }
    }
}

public class WeiTry extends Last
{
    public static void main(String[] args)
    {
        // Output
        Last show = new Last();
        show.dinero();
    }
}
4

1 回答 1

1

我看到你Employee的字段设置的唯一地方是在一个名为的方法中inputs()

one的值会被填充,因为您调用了该inputs方法,one但对于您的其他员工类型,例如您的UnionEmployee threeinputs从未调用过,并且它们的字段永远不会被设置。

如果您希望设置其他员工的值,看起来您必须调用他们的输入方法。

UnionEmployee three = new UnionEmployee();
three.inputs();
three.Syndicate();

或者你可以把它们复制过来

three.setName(one.getName());
于 2013-07-18T19:01:50.227 回答