计算购物车中的商品数量

时间:2015-09-04 09:28:40

标签: java list dictionary cart shopping

我对Java的概念很陌生。我使用核心java设计了一个购物车应用程序。但我无法获得所需的输出。 这是我的pojo类item.java

package com.shop.data.*;
public class Item
{
    private int Itemid;
    private String category;
    private String name;
    private double price;
      private String size;

    /**
     * @return the category
     */
    public String getCategory() {
        return category;
    }
    public Item(int itemid) {
        super();
        Itemid = itemid;
    }
    /**
     * @param category the category to set
     */
    public void setCategory(String category) {
        this.category = category;
    }

    // -------------------------------------------------------
    //  Create a new item with the given attributes.
    // -------------------------------------------------------
    public Item (String itemcategory ,String itemName, double itemPrice,String itemSize)
    {
        name = itemName;
        price = itemPrice;

        size = itemSize;
        category = itemcategory;
    }
    public Item(String itemName, int itemPrice, String size) {
        // TODO Auto-generated constructor stub
    }
    /**
     * @return the size
     */
    public String getSize() {
        return size;
    }
    /**
     * @param size the size to set
     */
    public void setSize(String size) {
        this.size = size;
    }

    // -------------------------------------------------
    //   Returns the unit price of the item
    // -------------------------------------------------
    public double getPrice()
    {
        return price;
    }
    // -------------------------------------------------
    //   Returns the name of the item
    // -------------------------------------------------
    public String getName()
    {
        return name;
    }
    /**
     * @return the itemid
     */
    public int getItemid() {
        return Itemid;
    }
    /**
     * @param itemid the itemid to set
     */
    public void setItemid(int itemid) {
        Itemid = itemid;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @param price the price to set
     */
    public void setPrice(double price) {
        this.price = price;
    }
    public String toString ()
    {

        return (name + "\t" + price +  "\t"+ size +  "\t");
    }


}

主要类是ShopCartTest.java

package com.shop.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.shop.data.*;

public class ShoppingCartTest {
    private static Scanner scan;

    public static void main(String[] args) {
        // *** declare and instantiate a variable cart to be an empty ArrayList
        shopping();
    }

    public static void shopping() {
        ArrayList<Item> ItemCart = new ArrayList<Item>();
        Map<Item, Integer> Quantity = new HashMap<Item, Integer>();
        Item item = null;
        Inventory inventory = null;
        String category = null;
        String itemName;
        String itemSize;
        double itemPrice = 0;
        double totalPrice = 0.0;
        double sum = 0.0;
        int itemquantity;
        scan = new Scanner(System.in);
        String keepShopping = "y";
        System.out.println("****Shopping Cart****");
        do {
            System.out.println("Select Category: ");
            category = scan.nextLine();
            if (category.equals("men") || category.equals("Men")) {

                System.out
                        .println("Available Items: \n1Shirt : 900 \n2.T-Shirt : 700 \n3.Jean: 1500.\n");
                System.out.println("Select Item to Add to your Bag: ");
                itemName = scan.nextLine();
                if (itemName.equals("Shirt") || (itemName.equals("shirt"))) {
                    itemPrice = 900;
                } else if (itemName.equals("T-Shirt")
                        || (itemName.equals("t-shirt"))) {
                    itemPrice = 700;
                } else
                    itemPrice = 1500;
            } else {
                System.out
                        .println("Available Items: \n1.Dress : 250 \n2.Top : 350 \n3.Jean: 1500.\n");
                System.out.println("Select Item to Add to your Bag: ");
                itemName = scan.nextLine();
                // *** create a new item and add it to the cart
                if (itemName.equals("Dress") || (itemName.equals("dress"))) {
                    itemPrice = 250;
                } else if (itemName.equals("Top") || (itemName.equals("top"))) {
                    itemPrice = 350;
                } else
                    itemPrice = 1500;
            }
            System.out.print("Available Sizes \nS \tM \tL: ");
            itemSize = scan.nextLine();
            System.out.print("Enter the quantity: ");
            itemquantity = scan.nextInt();
            item = new Item(category, itemName, itemPrice, itemSize);
            ItemCart.add(item);
            Quantity.put(item, itemquantity); 

            // *** print the contents of the cart object using println
            for (int i = 0; i < ItemCart.size(); i++) {
                Item itm = ItemCart.get(i);
                System.out.println(itm);
            }

            // Print out the results
            System.out.print("Continue shopping (y/n)? ");
            scan.nextLine();
            keepShopping = scan.nextLine();
        } while (keepShopping.equals("y"));

        for (int i = 0; i < ItemCart.size(); i++) {
            Quantity.put(item, itemquantity);
            Item itm = ItemCart.get(i);
            System.out.println(itm);
            totalPrice = itemquantity * itm.getPrice();
            for (int j = 0; i < ItemCart.size(); i++)
                sum += totalPrice;

        }

        System.out.println("The total price is: " + sum);
        System.out.println("Do you wan to proceed to checkout (y/n): ");
        if (scan.nextLine().equals("y")) {
            System.out.println("Thank you for shopping with us!");

        } else {
            shopping();
        }

    }

}

有4个pojo&s购物车,库存,物品,用户。用户必须能够选择多个量子的多个项目。 (对于已创建列表的项目和数量:map。其中,项目对象是关键,数量是值)。用户调整类别,项目,然后是quatity。然后他可以继续结账。所以当他决定结账时。他必须能够看到所购买的各种物品,即(itemName,SIxe,UnitPrice,unitprice *数量),最后是总价。 我如何实现这一目标?我有点失落!

1 个答案:

答案 0 :(得分:0)

一些指示:

  • 您应该使用proper naming conventions(即变量和方法名称以小写字母开头)
  • ItemCart是多余的,您可以删除它并使用Quantity密钥
  • Map提供entrySet方法,使用Quantity上的方法将帮助您列出购物车中的商品()并计算总价使用项目(密钥)价格和数量(