gpt4 book ai didi

Java ArrayList 类

转载 作者:太空宇宙 更新时间:2023-11-04 14:54:52 27 4
gpt4 key购买 nike

我开始这段代码,但我不知道如何完成它。我需要为销售人员及其数据的集合创建类和测试驱动程序。我创建了销售人员类和构造函数。我创建了访问器和修改器。我需要帮助将其合并到测试驱动程序的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com