我被要求编写一个小的在线销售应用程序。单个客户端中的多个产品(arrayList)
理论上听起来很简单(但对我来说这是一个地狱)。我只是应该有一个arrayList约5个产品,然后让客户购买1到5个产品并打印销售总额。
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String printInfo() {
return "Product: " + name + " Cost: " + price;
}
}
然后,我有一个客户端类:
public class Cliente {
private String name;
private int numPedido;
ArrayList<Producto> products = new ArrayList<Producto>();
public void listBuilder() {
Producto shirt = new Producto("Shirt", 30);
Producto tshirt = new Producto("T-Shirt", 40);
Producto sweater = new Producto("Sweater", 50);
}
public Cliente(String name, int numPedido) {
this.name = name;
this.numPedido = numPedido;
}
public Cliente() {
}
public String getName() {
return name;
}
public int getNumPedido() {
return (int) (Math.random() * 100);
}
public void addNewClient() {
name = JOptionPane.showInputDialog("Nombre: ");
}
public String printInfo() {
return "Nombre: " + name;
}
}
现在,我就如何使客户选择产品和获取连接到他卡住的思维。我正在考虑做一个arrayList的arrayList,但我相信这会让事情变得复杂。我知道可能有更简单的方法来连接它们,但我想不出任何。我想到的选项是一种显示1到3的数字(对应于每种产品)的方法,并且当用户选择一个时,它应该返回物品的价格。
仍然不确定如何以用户可以选择多个产品的方式实现它。
编辑:
我也有一个管理类,它是这样的:
public class Admin {
private Client[] clientList;
public AdminPedidos() {
clientList = new Client[2];
}
public void AddContact() {
clienteList[0] = addProduct();
clienteList[1] = addProduct();
fillList();
}
public Cliente addProduct() {
String contactoString = JOptionPane.showInputDialog("Are you a new client? Press 1 if yes.");
if (contactoString.equals("1")) {
return new Cliente();
} else {
return new Cliente(); //just for testing
}
}
private void fillList() {
for (Client i : clientList) {
i.addNewClient();
}
}
public void printContact() {
for (Client i : clientList) {
System.out.println(i.printInfo());
}
}
}
一是认为正常和分析,认为其中应listBuilder的方法以及如何访问使用ArrayList的 – Vinod