2015-09-04 145 views
0

我对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的车,库存,产品,用户。用户必须能够选择多个多个量子的多个项目。 (对于物料已创建清单,数量为map,其中物料对象为键,数量为值)。用户选择类别,项目,然后选择quatity。然后他可以继续结帐。所以当他决定结账。他必须能够看到所购买的各种物品,例如(itemName,SIxe,UnitPrice,unitprice *数量)以及最终的总价格。 我如何做到这一点?我有点失落!

+0

您可以随时与映射一对多的CartItems用户计算总价。 CartItem使用Inventory映射到manyToOne。我建议将用户的cartItems保存在数据库中,这样如果发生电力损失或用户注销,购物车仍会保留。 –

+1

除了你的问题,试着将你的方法分成更小的方法来增加可读性 –

回答

0

一些指针:

  • 您应该使用proper naming conventions(即变量和方法的名称以小写字母开头)
  • ItemCart是多余的,你可以将其删除,并使用Quantity
  • a Map提供了一个entrySet方法,使用Quantity将帮助您列出购物车中的物品(钥匙 ),使用项目()价格和数量(
+0

嗨,先生,请给我一个相同的例子代码。 ? – Shruthi

相关问题