Java 如何在ArrayList中添加10个具有不同数据类型的特定产品(对象)?
我是一名java新手,我正在尝试创建一台自动售货机,但我似乎无法添加我需要的10种产品。(我正在按照指导老师的要求使用BlueJ进行编码)。我需要的机器能够显示可用的产品只,让用户购买产品,显示总金额在机器上,并显示总项目出售 这是我的目标代码:Java 如何在ArrayList中添加10个具有不同数据类型的特定产品(对象)?,java,object,Java,Object,我是一名java新手,我正在尝试创建一台自动售货机,但我似乎无法添加我需要的10种产品。(我正在按照指导老师的要求使用BlueJ进行编码)。我需要的机器能够显示可用的产品只,让用户购买产品,显示总金额在机器上,并显示总项目出售 这是我的目标代码: import java.util.*; public class VendingMachineObject { public String productName = ""; public double unitPric
import java.util.*;
public class VendingMachineObject {
public String productName = "";
public double unitPrice;
public int initialQuantity;
public int availableQuantity;
public VendingMachineObject(String newProductName, double newUnitPrice, int newInitialQuantity, int newAvailableQuantity) {
this.productName = newProductName;
this.unitPrice = newUnitPrice;
this.initialQuantity = newInitialQuantity;
this.availableQuantity = newAvailableQuantity;
}
public String getProductName() {
return this.productName;
}
public double getUnitPrice() {
return this.unitPrice;
}
public int getInitialQuantity() {
return this.initialQuantity;
}
public int getAvailableQuantity() {
return this.availableQuantity;
}
public void setProductName(String newProductName) {
this.productName = newProductName;
}
public void setUnitPrice(double newUnitPrice) {
this.unitPrice = newUnitPrice;
}
public void setInitialQuantity(int newInitialQuantity) {
this.initialQuantity = newInitialQuantity;
}
public void setAvailableQuantity(int newAvailableQuantity) {
this.availableQuantity = newAvailableQuantity;
}
}
和主应用程序代码:
public class VendingMachine {
static Scanner input = new Scanner(System.in);
static ArrayList<VendingMachineObject> myVendor = new ArrayList<VendingMachineObject>(10);
static public int soldProductsCounter = 0;
static public double totalMoneyCounter = 0;
public static void main (String args[]) {
int choice;
do {
displayMenu();
choice = input.nextInt();
switch (choice) {
case 1: displayAvailableProducts(myVendor);
break;
case 2: buyProduct(myVendor);
break;
case 3: displayTotalProductsSold(myVendor);
break;
case 4: displayTotalMoney(myVendor);
break;
case 5: System.out.println("End of program");
break;
default: System.out.println("Invalid option. Try again.");
}
} while (choice != 5);
System.exit(0);
}
public static void displayMenu() {
System.out.println("1. Display available products");
System.out.println("2. Buy a product");
System.out.println("3. Display the total products sold");
System.out.println("4. Display the total money in the machine");
System.out.println("5. Exit");
System.out.println("Choice ?");
}
public static void displayAvailableProducts(ArrayList<VendingMachineObject> myVendor) {
int availableQuantity;
for (VendingMachineObject x: myVendor) {
if(x.getAvailableQuantity() > 0) {
System.out.println(x.getProductName() + " " + x.getUnitPrice() + " " + x.getAvailableQuantity());
}
}
}
public static void buyProduct(ArrayList<VendingMachineObject> myVendor) {
String productName;
int availableQuantity;
int choice;
System.out.println("Which product do you want to buy?");
productName = input.next();
for (VendingMachineObject x: myVendor) {
if (x.getProductName() == productName) {
System.out.println("The cost of this product is:" + x.getUnitPrice());
System.out.println("Items in stock:" + x.getAvailableQuantity());
System.out.println("How many do you want to buy?");
choice = input.nextInt();
if (choice > x.getAvailableQuantity()) {
System.out.println("You can not buy so many products");
System.out.println("You can buy only:" + x.getAvailableQuantity());
choice = x.getAvailableQuantity();
}
System.out.println("The total cost is:" + (choice * x.getUnitPrice()));
x.setAvailableQuantity(x.getAvailableQuantity()-choice);
soldProductsCounter = soldProductsCounter + choice;
totalMoneyCounter = totalMoneyCounter + x.getUnitPrice();
}
}
}
public static void displayTotalProductsSold(ArrayList<VendingMachineObject> myVendor) {
System.out.println("Items sold:" + " " + soldProductsCounter);
}
public static void displayTotalMoney(ArrayList<VendingMachineObject> myVendor) {
System.out.println("Total money in the machine:" + " "+ totalMoneyCounter);
}
}
公共类自动售货机{
静态扫描仪输入=新扫描仪(System.in);
静态ArrayList myVendor=新ArrayList(10);
静态公共int-soldProductsCenter=0;
静态公共双totalMoneyCounter=0;
公共静态void main(字符串参数[]){
智力选择;
做{
显示菜单();
choice=input.nextInt();
开关(选择){
案例1:displayAvailableProducts(我的供应商);
打破
案例2:购买产品(我的供应商);
打破
案例3:displayTotalProductsSold(我的供应商);
打破
案例4:displayTotalMoney(我的供应商);
打破
案例5:System.out.println(“程序结束”);
打破
默认值:System.out.println(“无效选项。请重试”);
}
}while(选项!=5);
系统出口(0);
}
公共静态无效显示菜单(){
System.out.println(“1.显示可用产品”);
System.out.println(“2.购买产品”);
System.out.println(“3.显示销售的总产品”);
System.out.println(“4.显示机器中的总金额”);
System.out.println(“5.Exit”);
System.out.println(“选择?”);
}
公共静态无效显示可用产品(ArrayList myVendor){
国际有效对等;
对于(VendingMachineObject x:myVendor){
如果(x.getAvailableEquality()>0){
System.out.println(x.getProductName()+“”+x.getUnitPrice()+“”+x.getAvailableEquality());
}
}
}
公共静态无效购买产品(ArrayList myVendor){
字符串名称;
国际有效对等;
智力选择;
System.out.println(“您想买哪种产品?”);
productName=input.next();
对于(VendingMachineObject x:myVendor){
如果(x.getProductName()==productName){
System.out.println(“此产品的成本为:+x.getUnitPrice());
System.out.println(“库存项目:+x.getAvailableEquality());
System.out.println(“你想买多少?”;
choice=input.nextInt();
如果(选项>x.GetAvailableEquality()){
System.out.println(“你不能买这么多产品”);
System.out.println(“您只能购买:+x.getAvailableQuantity());
choice=x.getAvailableQuantity();
}
System.out.println(“总成本为:+(choice*x.getUnitPrice()));
x、 setAvailableEquantity(x.getAvailableEquantity()-choice);
SoldProductsCenter=SoldProductsCenter+选项;
totalMoneyCounter=totalMoneyCounter+x.getUnitPrice();
}
}
}
公共静态无效显示TotalProductsSold(ArrayList myVendor){
System.out.println(“售出物品:“+”+SoldProductsCenter);
}
公共静态无效显示TotalMoney(ArrayList myVendor){
System.out.println(“机器中的总货币:“+”+totalMoneyCounter);
}
}
产品(对象)必须从一个超类继承
假设您有类食品
,饮料
,和零食
,这样饮料
和零食
继承自食品
您可以这样做:
ArrayList<Foods> menu = new ArrayList<Foods>(10);
menu.add(new Drinks("Coke", 1.99, 10, 10));
menu.add(new Snacks("Candy", 0.99, 10, 10));
ArrayList菜单=新建ArrayList(10);
菜单。添加(新饮料(“可乐”,1.99,10,10));
菜单。添加(新零食(“糖果”,0.99,10,10));
上面的内容叫做多态性,这里有一个很棒的教程EDIT:列表的正确声明是:ArrayList myVendor=new ArrayList(10);问题是什么?您已经有了适合从机器购买对象的类型。您只需要使用它们的数据(例如,名称、价格等)创建它们的新实例。。。如何在一个数组中输入名称、价格、初始数量和可用数量的10个自定义产品(对象)?其他类似的文章对我没有多大帮助。只需为
VendingMachine
创建一个构造函数,并在那里填写该列表即可。我仍然不知道问题出在哪里。这实际上是我无法完成整个阵列和添加产品的事情。我不确定使用数组还是数组列表更好。谢谢!我正在尝试这个!“Foods”类是主右键?只要你只有一个Main
方法,你想把Main
方法放在哪里并不重要。但是,我看到您的自动售货机
类中已经有了main
,所以就这样吧。只需为食品
、饮料
和零食创建单独的类
我已经创建了一个VendingMachineObject类,其中我有变量类型,如上所示,但我一直在“.add”之后获得预期的标识符错误(VendingMachineObject[]myVendor=new VendingMachineObject[10];VendingMachineObject product1=新的VendingMachineObject(“Coke”,1.50,10,10);myVendor[0]=product1;即使我这样尝试,也会在myVendor[(括号后面的零之前)之后出现错误“]”。您使用的是ArrayList还是Array?ArrayList是一个类,您无法访问myVendor[0]就像这样!看看这里的可用方法这是家庭作业吗?如果是的话,如果你想通过这门课,你肯定需要学习这些东西