2013-05-03 52 views
0

我有一个需要符合以下规范的程序: •销售人员每年将继续获得固定薪酬$ 50,000.00。每个销售人员目前的销售目标是每年120,000.00美元。 •只有达到销售目标的80%时,销售激励才会开始。目前的佣金是总销售额的7.5%。 •如果销售人员超过销售目标,佣金将根据加速因子增加。加速因子是1.25。 •应用程序应该要求用户输入年销售额,并且应显示年度总补偿。 •应用程序还应该显示销售人员可能获得的潜在年度总薪酬表,以高于销售员年销售额的5000美元为增量,直至达到销售员年销售额的50%以上。Java中的ArrayList输入和输出

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

一旦我找出这个问题,那么我需要弄清楚如何添加表格并进行比较。

+3

你的问题太长了。直接点,只粘贴相关的代码,并清楚地解释这个相关的代码应该做什么以及它做什么。 – 2013-05-03 19:47:38

+2

这显然是一个家庭作业问题 - 所以我不会直接回答。但检查你的getter和设置。 – 2013-05-03 19:50:05

+2

每个人都已经给了你很多答案,但正如@JerryAndrews指出的那样,问题在于你的获得者和制定者。这将是使用调试器的绝好机会;-) – 2013-05-03 19:51:49

回答

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; 
} 
+0

user1681360,谢谢,我甚至没有注意到这个简单的小错误,有时需要新鲜的眼睛。 – 2013-05-05 00:47:22

1

的问题是在你的setter方法:

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

你分配属性变量。解决这个问题是这样的:

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

为了避免这种问题,记得用this关键字在你的getter和对参数的属性分配。

+1

使参数最终也可以帮助检测这种类型的错误。 – 2013-05-03 19:56:27

+0

这工作,Luiggi门多萨,谢谢你。你能向我解释这个的意义吗? – 2013-05-05 00:46:22