2015-09-04 141 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

相關問題