Java 购物车程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21356182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shopping cart program
提问by user3023250
I've been working on my shopping cart program, but I keep having problems with entering itemTax in and with adding new items into cart and I don't know what I've done wrong.
我一直在研究我的购物车程序,但在输入 itemTax 和将新商品添加到购物车时一直遇到问题,我不知道我做错了什么。
class Item:
类项目:
public class Item {
private int id;
private String name;
private double price;
private String description;
private int quantity;
private double tax;
public Item (int itemID, String itemName, double itemPrice, String itemDescription, int itemQuantity, double itemTax){
id = itemID;
name = itemName;
price = itemPrice;
description = itemDescription;
quantity = itemQuantity;
tax = itemTax;
}
public int getID(){
return id;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public String getDescription(){
return description;
}
public int getQuantity(){
return quantity;
}
public double getTax(){
return tax;
}
}
class Cart:
类购物车:
import java.util.Scanner;
public class Cart {
private int itemCount;
private double totalPrice;
private static int capacity;
private static Item[] cart = new Item[capacity];
public Cart(){
itemCount = 10;
totalPrice = 0.0;
capacity = 0;
}
public void add(int itemID, String itemName, double itemPrice, String itemDescription, int itemQuantity, double itemTax){
Item item = new Item(itemID, itemName, itemPrice, itemDescription, itemQuantity, itemTax);
totalPrice += (itemPrice * itemQuantity);
cart[itemCount] = item;
itemCount += 1;
if(itemCount==capacity)
{
increaseSize();
}
}
public static void remove(String itemName){
Scanner s = new Scanner(System.in);
for (int i = 0; i < cart.length; i++) {
Item remove = (Item) cart.get(i);
if (itemName.equals(remove.getName())) {
cart.remove(i);
}
}
System.out.println("\n" + "Item " + itemName + " wasn't found.");
}
private void increaseSize()
{
Item[] item = new Item[capacity+5];
for(int i=0; i < capacity; i++)
{
item[i] = cart[i];
}
cart = item;
item = null;
capacity = cart.length;
}
public static void prLine (int itemID, String itemName, int itemQuantity, double itemPrice, double total, double itemTax) {
System.out.printf("\n%-10.10d %30s %10.2f %10d %10.2f", itemID, itemName, itemPrice, itemQuantity, itemTax, total);
}
public static void prTitles () {
System.out.printf("\n%-10s 30% %10s %10s %10s %10s", "ID", "Item", "Price", "Quantity", "Tax", "Total");
}
}
class Shop:
班级店铺:
import java.util.ArrayList;
import java.util.Scanner;
public class Shop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Item> cart = new ArrayList<Item>();
Item item;
int itemID;
String itemName;
double itemPrice;
String itemDescription;
int itemQuantity;
double itemTax;
int ch;
String choice;
Cart shoppingCart = new Cart();
while (true) {
System.out.println("Menu:");
System.out.println("0) Exit " + "\n"
+ "1) Add item in shopping cart" + "\n"
+ "2) Remove item from shpping cart");
ch = sc.nextInt();
switch (ch) {
case 0:
System.out.println("\n" + "Good bye!");
System.exit(0);
case 1:
System.out.println("Enter item ID: ");
itemID = sc.nextInt();
System.out.println("Enter item name: ");
itemName = sc.next();
System.out.println("Enter item price: ");
itemPrice = sc.nextDouble();
System.out.println("Enter short description of item: ");
itemDescription = sc.next();
System.out.println("Enter quantity: ");
itemQuantity = sc.nextInt();
System.out.println("Enter tax rate:");
itemTax = sc.nextDouble();
shoppingCart.add(itemID, itemName, itemPrice, itemDescription, itemQuantity, itemTax);
break;
case 2:
System.out.println("Enter name of the item that you would like to remove: ");
choice = sc.next();
shoppingCart.remove(choice);
break;
}
}
}
}
回答by user3192295
try:
尝试:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
to get input of the shopaholic
获得购物狂的意见
回答by Makoto
This block of code looks...suspect...
这段代码看起来......怀疑......
private static Item[] cart = new Item[capacity];
public Cart(){
itemCount = 10;
totalPrice = 0.0;
capacity = 0;
}
You provide no other way to instantiate the Cart
, so every time you new one up, you have a cart of size 0. Nothing could be added to that.
您没有提供其他方法来实例化Cart
,因此每次您新建一个时,您都会拥有一个大小为 0 的购物车。无法添加任何内容。
I'm willing to bet that, semantically, you mean to do something like this:
我敢打赌,从语义上讲,你的意思是做这样的事情:
private Item[] cart;
public Cart(){
itemCount = 0;
totalPrice = 0.0;
capacity = 10;
cart = new Item[capacity];
}
You have a capacity of ten items now, and itemCount
should be used in place of capacity
in your add
method to move the elements into their appropriate spot.
您现在有十个项目的容量,itemCount
应该capacity
在您的add
方法中使用 代替将元素移动到适当的位置。