Java ArrayList 类

标签 java arrays object arraylist

我开始这段代码,但我不知道如何完成它。我需要为销售人员及其数据的集合创建类和测试驱动程序。我创建了销售人员类和构造函数。我创建了访问器和修改器。我需要帮助将其合并到测试驱动程序的 arrayList 类中。请帮忙 (:

import java.util.*;
 public class salesPerson {

   //salesPerson fields
   private int salespersonID;
   private String salespersonName;
   private String productType;
   private int unitsSold = 0;
   private double unitPrice;

   //Constructor method
   public salesPerson(int salespersonID, String salespersonName, String productType, int unitsSold, double unitPrice)
   {
     this.salespersonID = salespersonID;
     this.salespersonName = salespersonName;
     this.productType = productType;
     this.unitsSold = unitsSold;
     this.unitPrice = unitPrice;
   }


   //Accessor for salesPerson
   public int getSalesPersonID(){
       return salespersonID;
    }

   public String getSalesPersonName(){
       return salespersonName;
   }

   public String getProductType(){
       return productType;
    }

   public int getUnitsSold(){
       return unitsSold;
    }

   public double getUnitPrice(){
       return unitPrice;
    }

   public double getTotalSold(){
        return unitsSold * unitPrice;
    }

   //Mutoators for salesPerson
   public void setSalesPersonID(int salespersonID){
       this.salespersonID = salespersonID;
   }

   public void setSalesPersonName(String salespersonName) {
       this.salespersonName = salespersonName;
    }

   public void setProductType(String productType){
       this.productType = productType;
    }

   public void setUnitsSold(int unitsSold){
       this.unitsSold += unitsSold;
    }

   public void setUnitProce(double unitPrice){
       this.unitPrice = unitPrice;
    }

        public static void main(String[] args)
    {
        ArrayList<salesPerson> salesPeople = new ArrayList<salesPerson>();
        Scanner userInput = new Scanner(System.in);
        boolean newRecord = true;
        int salespersonID;
        String salespersonName;
        String productType;
        int unitsSold = 0;
        double unitPrice;

        do{
            System.out.println("Please enter the Salesperson Inoformation.");
            System.out.print("Salesperson ID: ");
            salespersonID = userInput.nextInt();

            System.out.print("Salesperson Name: ");
            salespersonName = userInput.next();
            System.out.print("Product Type: ");
            productType = userInput.next();
            System.out.print("Units Sold: ");
            unitsSold = userInput.nextInt();
            System.out.print("Unit Price: ");
            unitPrice = userInput.nextDouble();



            System.out.print("Would you like to enter more data?(y/n)");
            String askNew = userInput.next();
            newRecord = (askNew.toLowerCase().equals("y")) ? true : false;

        }while(newRecord == true);

    }

 }

最佳答案

您已经创建了一个名为 salesPeople 的变量。 获取用户的所有输入后,创建一个新的 SalesPerson 对象,并在 while 循环末尾添加到 salesPeople 中。 检查下面的代码以获得更清晰的信息。

.....
SalesPerson tmp = new SalesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice);
salesPeople.add(tmp);

}while (newRecord == true)
....

关于Java ArrayList 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23328003/

相关文章:

java - 我应该如何处理J2ME中的关键事件?

Java,使用点数组

javascript - hasOwnProperty 具有多个属性

javascript - 比较 typescript 中的 2 个字符串

javascript - 找到所有的字符串值和相应的键

javascript - 从数组中的 JSON 对象中删除属性

java - 如何在 Java 中复制对象?

java - 警报对话框中的 HTTP 链接不起作用

java - 如何在 Jar 和项目之间创建依赖关系?

java - 如何制作自定义 View 的隐藏和显示动画