Я начал этот код, и я не знаю, как его закончить. Мне нужно сделать класс и тестовый драйвер для коллекции SalesPeople и их данных. Я создал класс продавцов и конструктор. Я создал аксессоры и мутаторы. Мне нужна помощь, расчесывающая это в класс 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 и добавьте в salesPeople в конце цикла while. Для получения большей ясности проверьте код ниже.
.....
SalesPerson tmp = new SalesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice);
salesPeople.add(tmp);
}while (newRecord == true)
....
добавьте ниже 2 строки (создайте sp-объект и добавьте его в список) в конце do, которое находится перед строкой ниже
System.out.print("Would you like to enter more data?(y/n)");
SalesPerson sp = new SalesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice);
salesPeople.add(sp);
Поместите это внутри цикла do-while после того, как у вас есть все данные. Вам нужно создать каждый SalesPerson после того, как у вас есть данные, иначе значения (id, name...) будут просто потеряны и перезаписаны в следующей циклической итерации
salesPeople.add(new SalesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice));
Поскольку arraylist salesPeople принимает объекты salesPerson, вы сначала захотите создать объект salesPerson, а затем добавить salesPerson в salesPeople arraylist.
salesPerson sp = new salesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice);
salesPeople.add(sp);
Положите это в конце своего действия, пока вы не спросите их, хотите ли вы создать нового продавца.
salesPerson
и добавьте его в массив