Java 了解阵列项目

Java 了解阵列项目,java,arrays,list,class,Java,Arrays,List,Class,我有一个项目要求如下: 在本练习中,您将完成一个将购物车实现为项目数组的类。java文件包含一个名为Item的类的定义,该类为一个人将购买的物品建模。项目有名称、价格和数量(购买的数量)。文件ShoppingCart.java将购物车实现为一个项目对象数组 我总共收到了三个代码,我已经完成了它们: 我相信这些课程是: // *************************************************************** // Item.java // Rep

我有一个项目要求如下: 在本练习中,您将完成一个将购物车实现为项目数组的类。java文件包含一个名为Item的类的定义,该类为一个人将购买的物品建模。项目有名称、价格和数量(购买的数量)。文件ShoppingCart.java将购物车实现为一个项目对象数组

我总共收到了三个代码,我已经完成了它们: 我相信这些课程是:

// ***************************************************************
//   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;
  }

}
//***************************************************************
//Item.java
//表示购物车中的项目。
// ***************************************************************
导入java.text.NumberFormat;
公共类项目
{
私有字符串名称;
私人双价;
私人整数数量;
// -------------------------------------------------------
//创建具有给定属性的新项。
// -------------------------------------------------------
公共项目(字符串itemName、双项目价格、整数numPurchased)
{
名称=项目名称;
价格=项目价格;
数量=购买的数量;
}
// -------------------------------------------------------
//返回包含项目信息的字符串
// -------------------------------------------------------
公共字符串toString()
{
NumberFormat fmt=NumberFormat.getCurrencyInstance();
退货(名称+“\t”+fmt.格式(价格)+“\t”+数量+“\t”+fmt.格式(价格*数量));
} 
// -------------------------------------------------
//返回项目的单价
// -------------------------------------------------
公开双价
{
退货价格;
}
// -------------------------------------------------
//返回项的名称
// -------------------------------------------------
公共字符串getName()
{
返回名称;
}
// -------------------------------------------------
//返回项目的数量
// -------------------------------------------------
公共整数getQuantity()
{
退货数量;
}
// **********************************************************************
//ShoppingCart.java
//将购物车表示为项目数组
// **********************************************************************
公共类购物车
{
private int itemCount;//购物车中的项目总数
private double totalPrice;//购物车中物品的总私有权
私人物品[]购物车;
私人int能力;
// -----------------------------------------------------------
//创建一个可容纳5件物品的空购物车。
// -----------------------------------------------------------
公共购物车()
{
购物车=新项目[容量];
容量=5;
itemCount=0;
总价=0.0;
{
// -------------------------------------------------------
//将项目添加到购物车。
//首先检查确保推车中有空间
// -------------------------------------------------------
public void addToCart(字符串itemName、双倍价格、整数数量)
{
项目温度=新项目(项目名称、价格、数量);
总价+=(价格*数量);
itemCount+=1;
购物车[项目计数]=临时;
如果(itemCount==容量)
{
增加大小();
}
}
// -------------------------------------------------------
//返回购物车的内容以及
//摘要信息。
// -------------------------------------------------------
公共字符串toString()
{
NumberFormat fmt=NumberFormat.getCurrencyInstance();
String contents=“\n购物车\n”;
contents+=“\nItem\t\Unit Price\tQuantity\tTotal\n”;
对于(int i=0;i
这是主要的:

//***************************************************************
//   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. 
   }
}
//***************************************************************
//Shop.java
//使用Item类创建项目并将其添加到购物列表中
//存储在ShoppingCart类中的购物车
// ***************************************************************
导入java.util.Scanner;
导入java.util.ArrayList;
公营商店
{    
公共静态void main(字符串[]args)
{
ArrayList购物车=新建ArrayList();
项目;
字符串itemName;
双倍价格;
整数;
扫描仪扫描=新扫描仪(System.in);
字符串keepShopping=“y”;
ShoppingCart=new ShoppingCart();
做
{
System.out.print(“输入项目名称:”);
itemName=scan.next();
系统输出打印(“输入单价:”);
itemPrice=scan.nextDouble();
系统输出打印(“输入数量:”);
数量=scan.nextInt();
//***通过向addToCart方法传递itemName、itemPrce和quantity,将此项目添加到购物车。
cart.addToCart(项目名称、项目价格、数量);
//***使用ShoppingCart类的toString方法打印cart对象的内容。
系统输出打印项次(购物车);
系统输出打印(“是否继续购物”);
keepShopping=scan.next();
}
而(keepShopping.equals(“y”));
//***在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.
   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;
   }