0

我有一个程序需要满足这些规范: • 销售人员将继续获得每年 50,000.00 美元的固定工资。每个销售人员的当前销售目标是每年 120,000.00 美元。• 销售激励只有在销售目标达到80% 时才会开始。目前的佣金是总销售额的 7.5%。• 如果销售人员超过销售目标,佣金将根据加速因素增加。加速因子为 1.25。• 应用程序应要求用户输入年度销售额,并应显示年度总薪酬。• 应用程序还应显示销售人员本可以赚取的潜在年度总薪酬表,以销售人员年销售额以上 5000 美元为增量,

• The application will now compare the total annual compensation of at least two salespersons.
 
• It will calculate the additional amount of sales that each salesperson must achieve to match or exceed the higher of the two earners.
 
• The application should ask for the name of each salesperson being compared. 
• The application should have at least one class, in addition to the application’s controlling class.
• The source code must demonstrate the use of conditional and looping structures.
• The source code must demonstrate the use of Array or ArrayList.

源代码中应该有适当的文档

我能够用 while 循环构造表。我现在的问题是数组或 ArrayList。我用 Salesperson 类构造了一个 ArrayList,但它没有输出 getName。我究竟做错了什么?以下是我的代码。

/*
 * Program: Salesperson.java
 * Written by: Amy Morin
 * This program will calculate total annual compensation for a salesperson.
 * Business requirements include:
 * Fixed salary = $50,000, sales target = $120,000,
 * sales incentive at 80% of sales,
 * Commission 7.5% of sales, if sales target is exceeded 1.25% increased 
 * accelorated factor.
 * This program will also be the foundation to
 * compare two or more salespersons.
 */

public class Salesperson
{   
    //Declares and initalizes fixed salary.
    private final double Fix_Sal = 50000;
    //Declares and initalizes commission.
    private final double Comm = 7.5;
    //Declares and initalizes acceleration factor.
    private final double Accel_Factor = 1.25;
    //Declares and initializes sales target.
    double target = 120000;         
    //Declares and initializes sales incentive threshold.
    double thresh = .80;

    String spName;    //holds the salesperson's name
    double annSales;   // Holds value for annual sales
    double commRate;  //holds calculated commission rate.  
    double commEarned;  //holds calculated commission earned.   
    double totalAnnComp; //Holds calculated total annual commission

    //Default Constructor
    public Salesperson()
    {
        spName = "Unknown";
        annSales = 0.0;
    }

    ////parameterized constructor
    public Salesperson(String name, double sales)
    {
        spName = name;
        annSales = sales;
    }

    //The setName method will set the name of salesperson
    public void setName(String name)
    {
        name = spName;
    }

    //The getName method will ruturn the name of salesperson
    public String getName()
    {
        return spName;
    }

    //The setSales method will set the annual sales
    public void setSales(double sales)
    {
        annSales = sales;
    }

    //The getSales method returns the value stored in annualSales
    public double getSales()
    {
        return annSales;
    }

    //The getComm method will calculate and return commission earned
    public double getComm()
    {
    //Check if sale are greater than or equal to 80% of target. 
    if (annSales >= (target * thresh))
    {
            if (annSales > target) //Checks if annual sales exceed target.
            {   
            //Gets commission rate.
                commRate = (Comm * Accel_Factor)/100;
        commEarned = commRate * annSales;
            }
            else
            {
        commRate = Comm/100;
        commEarned = commRate * annSales;
            }
    }
    else
        {
        commRate = 0;
        commEarned = 0;
    }
    return commEarned;
    }

    /*
     * The getAnnComp method will calculate and return the total 
     * annual compensation.
     */ 
    public double getAnnComp ()
    {
    totalAnnComp = Fix_Sal + commEarned;
    return totalAnnComp;
    }
}


/*
 * Program: SalespersonComparison
 * Written by: Amy Morin
 * This program will compare the total annual compensation 
 * of at least two salespersons. It will also calculate the additional 
 * amount of sales that each salesperson must achieve to match or 
 * exceed the higher of the two earners.
 */

import java.util.ArrayList; //Needed for ArrayList
import java.util.Scanner;  //Needed for Scanner class
import java.text.DecimalFormat; //Needed for Decimal formatting

public class SalespersonComparison
{
   public static void main(String[] args)
   {         
       final double Fix_Sal = 50000; //Declares and initiates fixed salary
       double sales; // hold annual sales

       //Create new ArrayList
       ArrayList<Salesperson> cArray = new ArrayList<>();

        // Create a Scanner object to read input.
       Scanner keyboard = new Scanner(System.in); 

       //Lets user know how to end loop
       System.out.println("Press \'Enter\' to continue or type \'done\'"
               + " when finished.");

       //blank line
       System.out.println();

       //Loop for setting name and annual sales of salesperson
       do
       {   
           //Create an Salesperson object
           Salesperson sPerson = new Salesperson();

           //Set salesperson's name
           System.out.println("Enter salesperson name");
           String name = keyboard.nextLine();
           sPerson.setName(name);

           //End while loop
           if (name.equalsIgnoreCase("Done"))
               break;                     

           //Set annual sales for salesperson
           System.out.println("Enter annual sales for salesperson");
           sales = keyboard.nextDouble();
           sPerson.setSales(sales);          

           //To add Salesperson object to ArrayList
           cArray.add(sPerson);

           //Consume line
           keyboard.nextLine();     
       }
       while (true);

       //Display ArrayList

       DecimalFormat arrayL = new DecimalFormat("#,##0.00");
       for (int index = 0; index < cArray.size(); index++)
       {
           Salesperson sPerson = (Salesperson)cArray.get(index);
           System.out.println();
           System.out.print("Salesperson " + (index + 1) +
                            "\n Name: " + sPerson.getName() +
                            "\n Sales: " + (arrayL.format(sPerson.getSales())) +
                            "\n Commission Earned: " + 
                            (arrayL.format(sPerson.getComm())) +
                            "\n Total Annual Compensation: " 
                            + (arrayL.format(sPerson.getAnnComp())) + "\n\n");



       }
   }
}      

Output
Press 'Enter' to continue or type 'done' when finished.

Enter salesperson name
amy
Enter annual sales for salesperson
100000
Enter salesperson name
marty
Enter annual sales for salesperson
80000
Enter salesperson name
done

Salesperson 1
 Name: Unknown
 Sales: 100,000.00
 Commission Earned: 7,500.00
 Total Annual Compensation: 57,500.00


Salesperson 2
 Name: Unknown
 Sales: 80,000.00
 Commission Earned: 0.00
 Total Annual Compensation: 50,000.00

BUILD SUCCESSFUL (total time: 20 seconds)

一旦我解决了这个问题,我就需要弄清楚如何添加表格并进行比较。

4

2 回答 2

1

您以相反的顺序分配值。

//The setName method will set the name of salesperson
public void setName(String name)
{
    name = spName;
}

应该

//The setName method will set the name of salesperson
public void setName(String name)
{
    spName = name;
}
于 2013-05-03T19:48:12.557 回答
1

问题出在您的 setter 方法中:

public void setName(String name) {
    name = spName;
}

您正在将属性分配给变量。像这样修复它:

public void setName(String name) {
    this.spName = name;
}

为避免此类问题,请记住this在 getter 中使用关键字并将参数分配给属性。

于 2013-05-03T19:48:21.737 回答