Java中的ArrayList输入和输出

时间:2013-05-03 19:45:00

标签: java

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

• 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)

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

问题在于你的setter方法:

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

您正在为变量分配属性。修复如下:

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

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