购物车计划

时间:2014-01-25 20:59:10

标签: java cart

我一直在处理我的购物车计划,但我一直遇到输入itemTax的问题​​以及在购物车中添加新商品时我不知道我做错了什么。

班级项目:

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;
}

}

类购物车:

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");
}



}

类商店:

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;
        }

    }
}

}

2 个答案:

答案 0 :(得分:1)

这段代码看起来......可疑......

private static Item[] cart = new Item[capacity];

public Cart(){
    itemCount = 10;
    totalPrice = 0.0;
    capacity = 0;
}

你没有提供实例化Cart的其他方法,所以每次你新推出时,你都有一个大小为0的购物车。没有任何东西可以加入。

我愿意打赌,在语义上,你的意思是做这样的事情:

private Item[] cart;

public Cart(){
    itemCount = 0;
    totalPrice = 0.0;
    capacity = 10;
    cart = new Item[capacity];
}

您现在有10个项目的容量,并且itemCount应该用capacity代替add方法将元素移动到适当的位置。

答案 1 :(得分:-1)

尝试:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();

获得购物狂的输入