我应该将此命令放在我的代码中的什么位置?
Item item = new Item();
我应该把它放在我的循环do-while
中,以便它继续创建一个新Item
类,这取决于用户是否想要输入另一个产品多少次。
问题是每当我将该命令放入我do-while
的item
. 那么我应该把它放在哪里?
我的代码:
public static void main(String[] args) {
Scanner scan = new Scanner(System. in );
String prompt, id;
int z = 0, choice, x = 999;
int iprice;
String icode;
String iname;
class Item {
public int price;
public String code;
public String name;
}
ArrayList<Item> cart = new ArrayList<Item>(0);
do {
System.out.print("\nItem Code: ");
icode = scan.next();
System.out.print("Item Name: ");
iname = scan.next();
System.out.print("Item Price: ");
iprice = scan.nextInt();
item.code = icode;
item.name = iname;
item.price = iprice;
cart.add(item);
System.out.print("\tInput another? Y/N? ");
prompt = scan.next();
} while (prompt.equals("y"));
System.out.println("\nWhat do you want to do?");
System.out.println("\n1. Purchase Item ");
System.out.println("2. List of Products ");
System.out.println("3. Exit Program ");
System.out.print("\n\tChoice: ");
choice = scan.nextInt();
if (choice == 1) {
System.out.println("\nItem Code: ");
id = scan.next();
if (item.code.equals(id)) {
System.out.println("Item Name: " + item.name);
System.out.println("Item Price: " + item.price);
}
}
}