我有一個項目詢問以下內容: 在本練習中,您將完成一個將購物車實現爲一系列項目的類。 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.
在這裏發佈確切的錯誤信息。 – csmckelvey 2014-11-06 03:46:33
^^那個和它指向的確切線 – 2014-11-06 03:47:06
另外,請注意,您有兩個具有相同名稱的變量。你有'ArrayList- 購物車'和'ShoppingCart購物車'這將導致你的問題。 –
csmckelvey
2014-11-06 03:48:44