2014-11-06 57 views
0

我有一個項目詢問以下內容: 在本練習中,您將完成一個將購物車實現爲一系列項目的類。 Item.java文件包含一個名爲Item的類的定義,該類用於對要購買的項目建模。一個項目有一個名稱,價格和數量(購買的數量)。 ShoppingCart.java文件將購物車作爲Item對象的數組實現。瞭解數組項目

我有一個總的都給了我們,我已經完成了他們三個碼: 我相信這些都是類:

// *************************************************************** 
// Item.java 
// Represents an item in a shopping cart. 
// *************************************************************** 

import java.text.NumberFormat; 

public class Item 
{ 
    private String name; 
    private double price; 
    private int quantity; 


    // ------------------------------------------------------- 

    // Create a new item with the given attributes. 

    // ------------------------------------------------------- 

    public Item (String itemName, double itemPrice, int numPurchased) 
    { 
     name = itemName; 
     price = itemPrice; 
     quantity = numPurchased; 
    } 

    // ------------------------------------------------------- 
    // Return a string with the information about the item 
    // ------------------------------------------------------- 

    public String toString() 
    { 
     NumberFormat fmt = NumberFormat.getCurrencyInstance(); 

    return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"+ fmt.format(price*quantity)); 
    } 

    // ------------------------------------------------- 
    // Returns the unit price of the item 
    // ------------------------------------------------- 
    public double getPrice() 
    { 
     return price; 
    } 


    // ------------------------------------------------- 
    // Returns the name of the item 
    // ------------------------------------------------- 
    public String getName() 
    { 
     return name; 
    } 

    // ------------------------------------------------- 
    // Returns the quantity of the item 
    // ------------------------------------------------- 
    public int getQuantity() 
    { 
     return quantity; 
    } 

    // ********************************************************************** 
    // ShoppingCart.java 
    // Represents a shopping cart as an array of items 
    // ********************************************************************** 


    public class ShoppingCart 
{ 
    private int itemCount; //total number of items in the cart 
    private double totalPrice; //total prive of items in the cart 
    private Item[] cart; 
    private int capacity; 

    // ----------------------------------------------------------- 
    // Creates an empty shopping cart with a capacity of 5 items. 
    // ----------------------------------------------------------- 
    public ShoppingCart() 
    { 
     cart = new Item[capacity]; 
     capacity = 5; 
     itemCount = 0; 
     totalPrice = 0.0; 

    { 

    // ------------------------------------------------------- 
    // Adds an item to the shopping cart. 

    // Check to make sure there is room in the cart first 

    // ------------------------------------------------------- 

    public void addToCart(String itemName, double price, int quantity) 
    { 
     Item temp = new Item(itemName, price, quantity); 
     totalPrice += (price * quantity); 
     itemCount += 1; 
     cart[itemCount] = temp; 
    if (itemCount==capacity) 
    { 
    increaseSize(); 
    } 
    } 

    // ------------------------------------------------------- 
    // Returns the contents of the cart together with 
    // summary information. 
    // ------------------------------------------------------- 
    public String toString() 
    { 
    NumberFormat fmt = NumberFormat.getCurrencyInstance(); 

     String contents = "\nShopping Cart\n"; 
     contents += "\nItem\t\Unit Price\tQuantity\tTotal\n"; 

     for (int i = 0; i < itemCount; i++) 
     contents += cart[i] + "\n"; 
     contents += "\nTotal Price: " + fmt.format(totalPrice); 
     contents += "\n"; 

     return contents; 
    } 

    // --------------------------------------------------------- 
    // Increases the capacity of the shopping cart by 3 
    // --------------------------------------------------------- 
    private void increaseSize() 
    { 
    Item[] temp = new Item[capacity+3]; 
    for(int i=0; i < capacity; i++) 
    { 
     tempt[i] = cart[i]; 
    } 
    cart = temp; 
    temp = null; 
    capacity = cart.length; 
    } 

} 

這是主要的:

//*************************************************************** 
// Shop.java 
// Uses the Item class to create items and add them to a shopping 
// cart stored in a ShoppingCart class 
// *************************************************************** 
import java.util.Scanner; 


import java.util.ArrayList; 
public class Shop 
{  

public static void main (String[] args) 

{ 

    ArrayList<Item> cart = new ArrayList<Item>(); 


    Item item; 

    String itemName; 

    double itemPrice; 

    int quantity; 



    Scanner scan = new Scanner(System.in); 

    String keepShopping = "y"; 
    ShoppingCart cart = new ShoppingCart(); 

    do 

    { 

    System.out.print("Enter the name of the item: "); 
    itemName = scan.next(); 

    System.out.print("Enter the unit price: "); 
    itemPrice = scan.nextDouble(); 

    System.out.print("Enter the quantity: "); 
    quantity = scan.nextInt(); 
     // *** add this item to the cart by passing itemName, itemPrce, and quantity to the addToCart method. 
    cart.addToCart(itemName, itemPrice, quantity); 

     // *** print the contents of the cart object using the toString method of the ShoppingCart class. 

    System.out.println(cart); 

    System.out.print("Continue shopping (y/n)?"); 
    keepShopping = scan.next(); 


    } 

     while (keepShopping.equals("y")); 
     // *** print the final total of the grocery list with a 「Please pay ...」 in front of the toalPrice. 
    } 
} 

我的問題是我正確安排了代碼嗎?由於該類不編譯我得到非法啓動錯誤,我在主要麻煩。 任何幫助表示讚賞。

類錯誤:

----jGRASP exec: javac -g Item.java 
Item.java:95: error: illegal start of expression 
public void addToCart(String itemName, double price, int quantity) 
    ^
Item.java:95: error: illegal start of expression 
public void addToCart(String itemName, double price, int quantity) 
    ^
Item.java:95: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
        ^
Item.java:95: error: <identifier> expected 
public void addToCart(String itemName, double price, int quantity) 
            ^
Item.java:95: error: not a statement 
public void addToCart(String itemName, double price, int quantity) 
              ^
Item.java:95: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
               ^
Item.java:95: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
                   ^
Item.java:111: error: illegal start of expression 
public String toString() 
^ 
Item.java:111: error: ';' expected 
public String toString() 
        ^
Item.java:116: error: illegal escape character 
    contents += "\nItem\t\Unit Price\tQuantity\tTotal\n"; 
         ^
Item.java:130: error: illegal start of expression 
private void increaseSize() 
^ 
Item.java:130: error: illegal start of expression 
private void increaseSize() 
     ^
Item.java:130: error: ';' expected 
private void increaseSize() 
         ^
Item.java:147: error: reached end of file while parsing 
} 
^ 
14 errors 

----jGRASP wedge: exit code for process is 1. 
----jGRASP: operation complete. 

主要方法錯誤:

----jGRASP exec: javac -g Shop.java 
Item.java:95: error: illegal start of expression 
public void addToCart(String itemName, double price, int quantity) 
^ 
Item.java:95: error: illegal start of expression 
public void addToCart(String itemName, double price, int quantity) 
    ^
Item.java:95: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
        ^
Item.java:95: error: <identifier> expected 
public void addToCart(String itemName, double price, int quantity) 
            ^
Item.java:95: error: not a statement 
public void addToCart(String itemName, double price, int quantity) 
              ^
Item.java:95: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
               ^
Item.java:95: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
                   ^
Item.java:111: error: illegal start of expression 
public String toString() 
^ 
Item.java:111: error: ';' expected 
public String toString() 
        ^
Item.java:116: error: illegal escape character 
    contents += "\nItem\t\Unit Price\tQuantity\tTotal\n"; 
         ^
Item.java:130: error: illegal start of expression 
private void increaseSize() 
^ 
Item.java:130: error: illegal start of expression 
private void increaseSize() 
     ^
Item.java:130: error: ';' expected 
private void increaseSize() 
         ^
Item.java:147: error: reached end of file while parsing 
} 
^
ShoppingCart.java:34: error: illegal start of expression 
public void addToCart(String itemName, double price, int quantity) 
^ 
ShoppingCart.java:34: error: illegal start of expression 
public void addToCart(String itemName, double price, int quantity) 
    ^
ShoppingCart.java:34: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
        ^
ShoppingCart.java:34: error: <identifier> expected 
public void addToCart(String itemName, double price, int quantity) 
            ^
ShoppingCart.java:34: error: not a statement 
public void addToCart(String itemName, double price, int quantity) 
              ^
ShoppingCart.java:34: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
               ^
ShoppingCart.java:34: error: ';' expected 
public void addToCart(String itemName, double price, int quantity) 
                   ^
ShoppingCart.java:50: error: illegal start of expression 
public String toString() 
^ 
ShoppingCart.java:50: error: ';' expected 
public String toString() 
        ^
ShoppingCart.java:55: error: illegal escape character 
    contents += "\nItem\t\Unit Price\tQuantity\tTotal\n"; 
         ^
ShoppingCart.java:69: error: illegal start of expression 
private void increaseSize() 
^ 
ShoppingCart.java:69: error: illegal start of expression 
private void increaseSize() 
     ^
ShoppingCart.java:69: error: ';' expected 
private void increaseSize() 
         ^
ShoppingCart.java:82: error: reached end of file while parsing 
} 
^ 
Shop.java:33: error: variable cart is already defined in method main(String[]) 
    ShoppingCart cart = new ShoppingCart(); 
      ^
Shop.java:48: error: cannot find symbol 
    cart.addToCart(itemName, itemPrice, quantity); 
     ^
    symbol: method addToCart(String,double,int) 
    location: variable cart of type ArrayList<Item> 
    Item.java:103: error: cannot find symbol 
    increaseSize(); 
    ^
    symbol: method increaseSize() 
    location: class Item.ShoppingCart 
    Item.java:124: error: incompatible types: unexpected return value 
    return contents; 
     ^
    Item.java:135: error: cannot find symbol 
     tempt[i] = cart[i]; 
     ^
    symbol: variable tempt 
    location: class Item.ShoppingCart 
    ShoppingCart.java:36: error: cannot find symbol 
     Item temp = new Item(itemName, price, quantity); 
           ^
    symbol: variable price 
    location: class ShoppingCart 
    ShoppingCart.java:37: error: cannot find symbol 
    totalPrice += (price * quantity); 
       ^
    symbol: variable price 
    location: class ShoppingCart 
    ShoppingCart.java:42: error: cannot find symbol 
    increaseSize(); 
    ^
    symbol: method increaseSize() 
    location: class ShoppingCart 
    ShoppingCart.java:63: error: incompatible types: unexpected return value 
    return contents; 
     ^
    ShoppingCart.java:74: error: cannot find symbol 
     tempt[i] = cart[i]; 
     ^
    symbol: variable tempt 
    location: class ShoppingCart 
    38 errors 

----jGRASP wedge: exit code for process is 1. 
----jGRASP: operation complete. 
+0

在這裏發佈確切的錯誤信息。 – csmckelvey 2014-11-06 03:46:33

+0

^^那個和它指向的確切線 – 2014-11-06 03:47:06

+0

另外,請注意,您有兩個具有相同名稱的變量。你有'ArrayList 購物車'和'ShoppingCart購物車'這將導致你的問題。 – csmckelvey 2014-11-06 03:48:44

回答

0

在我看來,很少有建議:

在我的購物應該持有該項目陣列object.Iterate項目數組對象來獲取總價格,數量和容量。構造函數只能保存爲接受項目數組。

將項目添加到購物車後,可以調用購物車中定義的方法作爲示例totalPrice,totalQuantity以獲取項目的所需詳細信息。

您可以參考以下鏈接: http://www.journaldev.com/1754/strategy-design-pattern-in-java-example-tutorial

0

作爲一個評論說,那我看到的最大的問題之一是,你用「購物車」兩次:一次是作爲一個ArrayList,一旦作爲一個ShoppingCart。這樣做會使應用程序混淆,因爲它不知道從哪裏撤出。

當您說'cart.addToCart(itemName,itemPrice,quantity);'時,應用程序不會立即知道您是要將信息添加到ArrayList還是添加到ShoppingCart。

此外,我看到你的方法有幾個問題。

在你的「toString()」方法中,我會重命名它。 「toString()」是一個代碼,允許您將某些變量轉換爲字符串,並且它是一個已經在Java中定義的代碼。這就是爲什麼它告訴你一個';'是期待;因爲通常它會是「thisVariable.toString();」。那有意義嗎?

同時,仍然在你的 「的toString()」 類,我看到你鍵入:

「內容+ = 」\ nItem域\ t \價格\ tQuantity \ TTOTAL \ n「;」。

這應改爲:

「內容+ = 」\ nItem域\ TUNIT價格\ tQuantity \ TTOTAL \ n「;」。

你有一個額外的反斜槓。我認爲應該解決這個問題。

0
public ShoppingCart() 
    { 
     cart = new Item[capacity]; 
     capacity = 5; 
     itemCount = 0; 
     totalPrice = 0.0; 

您在購物卡的構造函數中缺少右括號'}'。它應該是以下內容。

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