ShoppingCart.Java程序分配

时间:2013-11-17 08:36:45

标签: java arraylist

我正在完成一项任务,但我遇到了一些问题。这是作业问题以及我迄今为止所做的以及我的问题。

  1. 完成ShoppingCart.java,如下所示:

    • 声明并将变量cart实例化为可以容纳Product对象的空ArrayList。记得导入ArrayList。代码中的注释表明这些陈述的位置。

    • 填写所需的语句以编写一个循环,该循环从用户请求产品项目请求所需信息,使用输入信息创建Product对象,并将产品添加到ArrayList。确保用户可以根据用户是否继续购物继续工作来输入多个产品的信息。代码中的注释表明这些陈述的位置。

    • 通过打印存储在ArrayList中的每个产品项来打印购物车内容。请记住,Product对象具有toString方法。打印购物车内容后打印购物车的总价。请记住格式化总价格的输出,以便以当前格式打印。代码中的注释表明这些陈述的位置。

  2. 使用以下输入编译并运行程序(提供程序输出)

    • 第一个产品为Bicycle Horn,单价为7.19,数量为2,制造商为Klout
    • 第二个产品为Forerunner Watch,单价为140.89,数量为2,制造商为Garmin
  3. 以下是我写的代码:

      //***************************************************************
    //ShoppingCart.java
    //
    //Uses the Product class to create items and add them to a shopping
    //cart stored in an ArrayList.
    //***************************************************************
    
    // import statements
    import java.util.ArrayList;
    import java.text.NumberFormat;
    import java.util.Scanner;
    
    //Class header
    public class ShoppingCart {
        //Start of main method
        public static <Item> void main(String[] args){
    
            //Declare and instantiate a variable that is an ArrayList that can hold Product objects
            ArrayList<Product> item = new ArrayList<Product>();
            //Declare necessary local variables here
            String Name = null;
            double Price = 0;
            int Quantity = 0;
            String Seller = null;
            Scanner scan = new Scanner(System.in);
    
            // Write a print statement that welcome's the customer to our shop
    
    
            /**
             * create a do while that will be keep looping as long as user wants to continue shopping
             */
             String keepShopping = "Yes";
             Product item1  = new Product(Name, Price, Quantity, Seller);
            //do while loop start
                do
                     {
    
                //Ask user to enter product name and store it in appropriate local variable
                System.out.print("Please Enter the Product Name: ");
                Name = scan.next();
    
                //Ask user to enter product price and store it in appropriate local variable
                System.out.print("Please Enter the Price of the Product: ");
                Price = scan.nextDouble();
                //Ask user to enter quantity and store it in appropriate local variable
                System.out.print("Please enter the Quantity: ");
                Quantity = scan.nextInt();
                //Ask user to enter product manufacturer name and store it in appropriate local variable
                System.out.print("Please Enter the Manufacturer: ");
                Seller = scan.next();
    
                // create a new Product object using above inputed values
                Product newitem = new Product(Name, Price, Quantity, Seller); 
    
                //add above created Product to the ArrayList cart if Product has available stock
                // if stock not available inform user requested product is out of stock
    
                //Ask user whether wants to continue shopping 
                //set the do while loop to continue to loop if Yes option is selected
                     } while (keepShopping.equals("Yes"));
    
    
    
                      // do while loop end
    
            // header for shopping cart contents
    
    
            // print details of each product added to the ArrayList
            // calculate total price of the shopping cart
    
            // print the total price of the shopping cart
    
        }//end of main method
    }//end of Shop class
    

    教授提供的产品类:

    //***************************************************************
    //Product.java
    //Represents an item in a shopping cart.
    //***************************************************************
    import java.text.NumberFormat;
    
    public class Product
    {
        private String name;
        private double price;
        private int quantity;
        private double subtotal;
        private String manufacturer;
        private int inventory;
    
        // -------------------------------------------------------
        //  Create a new item with the given attributes.
        // -------------------------------------------------------
        public Product (String name, double price, int quantity, String manufacturer)
        {
            this.name = name;
            this.price = price;
            this.quantity = quantity;
            this.manufacturer = manufacturer;
    
            subtotal = price*quantity;
    
            inventory = 10;
    
        }
    
        public Product(String itemName, double itemPrice, int quantity2) {
            // TODO Auto-generated constructor stub
        }
    
        // -------------------------------------------------------
        //   Return a string with the information about the Product
        // -------------------------------------------------------
        public String toString ()
        {
            NumberFormat fmt = NumberFormat.getCurrencyInstance();
            return (manufacturer + " " + name + "\t\t    " + fmt.format(price) + "\t    " + quantity 
                    + "\t\t" + fmt.format(subtotal));
        }
    
        //   Returns the unit price of the Product
        public double getPrice()
        {
            return price;
        }
    
        //   Returns the name of the Product
        public String getName()
        {
            return name;
        }
    
        //   Returns the quantity of the Product
        public int getQuantity()
        {
            return quantity;
        }
    
        //   Returns the sub total of the Product
        public double getSubTotal()
        {
            return subtotal;
        }
    
        // Returns product manufacturer
        public String getManufacturer()
        {
            return manufacturer;
        }
    
        // Checks whether product is in stock
        // if after inventory get below stock after processing order, then
        // places order to replenish stock
        public boolean checkInventory()
        {
            boolean flag = false;
            if (inventory > quantity)
            {
                flag = true;
                inventory = inventory - quantity;
    
                if (inventory <= 0)
                    placeOrder(quantity);
            }
    
            return flag;
        }
    
        // Replenishes stock to 10 times the quantity of last order
        private void placeOrder(int orderQuantity)
        {
            inventory = orderQuantity * 10;
        }
    }//end of class Item  
    

1 个答案:

答案 0 :(得分:0)

我会写评论但由于某种原因它不给我选择。

我不明白这个问题?你只是想让我做任务吗?你不明白什么?

看起来你没有向你创建的arrayList添加任何东西。您创建了产品对象,但需要将其添加到列表中.....

此外,您似乎在产品类中创建了第二个我认为您没有使用的构造函数。