繁体   English   中英

购物车使用两个文件(类和主要方法)Java

[英]Shopping Cart using two files (class and main method) Java

创建两个要提交的文件:ItemToPurchase.java - 类定义ShoppingCartPrinter.java - 包含main()方法

〜使用以下规范构建ItemToPurchase类:

私人领域
-String itemName - 在默认构造函数中初始化为“none”
-int itemPrice - 在默认构造函数中初始化为0
-int itemQuantity - 在默认构造函数中初始化为0
默认构造函数
公共成员方法(mutators和accessors)
-setName()&getName()
-setPrice()&getPrice()
-setQuantity()&getQuantity()

- 在main()中,提示用户输入两个项目并创建ItemToPurchase类的两个对象。 在提示输入第二个项目之前,请调用scnr.nextLine(); 允许用户输入新字符串。

- 将两个项目的成本加在一起并输出总成本。

以下是我的ShoppingCartPrinter代码:

    public static void main(String[] args) { 
      Scanner scnr = new Scanner(System.in);
      ItemToPurchase item1 = new ItemToPurchase();
      ItemToPurchase item2 = new ItemToPurchase();

      //String inputName = "";
      //int inputPrice = 0;
      //int inputQuantity = 0;

      item1.setName("testName");
      item1.setQuantity(1);
      item1.setPrice(1);

      String itemName = item1.getName();
      int itemQuantity = item1.getQuantity();
      int itemPrice = item1.getPrice();

      System.out.println(itemName);
      System.out.println(itemPrice);
      System.out.println(itemQuantity);

    }
    //Add the costs of the two items together and output the total cost.
}

以下是我的ItemToPurchase代码:

    public class ItemToPurchase {

   // The class' private internal fields
   private String itemName;
   private int itemPrice;
   private int itemQuantity;

    // The class' public methods

   public void setName(String inputName) {
       itemName = inputName;
   }
   public String getName() {
      return itemName;
   }
   public void setPrice(int inputPrice) {
       itemPrice = inputPrice;
   }
   public int getPrice() {
      return itemPrice;
   }
   public void setQuantity(int inputQuantity) {
       itemQuantity = inputQuantity;
   }
   public int getQuantity() {
       return itemQuantity;
   }
}

当我寻找资源来帮助我并且没有从我的教授那里听到回复时,我变得越来越困惑。 代码构建没有错误但不复制。 这是我得到的错误: 在此输入图像描述

对于ShoppingCartPrinter ,这适用于:

import java.util.Scanner;

class ShoppingCartPrinter
{
   public static void main(String[] args)
   {
      Scanner scnr = new Scanner(System.in);
      String item1 = "";
      String item2 = "";
      int price1      = 0;
      int price2      = 0;
      int quantity1   = 0;
      int quantity2   = 0;

      ItemToPurchase itemInfo1 = new ItemToPurchase();
      ItemToPurchase itemInfo2 = new ItemToPurchase();

      System.out.println("Item 1");
      System.out.println("Enter the item name:");
      itemInfo1.setName(scnr.nextLine());

      System.out.println("Enter the item price:");
      itemInfo1.setPrice(scnr.nextInt());

      System.out.println("Enter the item quantity:");
      itemInfo1.setQuantity(scnr.nextInt());
      System.out.println();

      System.out.println("Item 2");
      itemInfo2.setName(scnr.nextLine());
      System.out.println("Enter the item name:");
      itemInfo2.setName(scnr.nextLine());

      System.out.println("Enter the item price:");
      itemInfo2.setPrice(scnr.nextInt());

      System.out.println("Enter the item quantity:");
      itemInfo2.setQuantity(scnr.nextInt());
      System.out.println();

      System.out.println("TOTAL COST");

      System.out.println(itemInfo1.getName() + " " + itemInfo1.getQuantity() + " @ $" + itemInfo1.getPrice() + " = $" + (itemInfo1.getPrice() * itemInfo1.getQuantity()));

      System.out.println(itemInfo2.getName() + " " + itemInfo2.getQuantity() + " @ $" + itemInfo2.getPrice() + " = $" + (itemInfo2.getPrice() * itemInfo2.getQuantity()));

      System.out.println();

      System.out.println("Total: $" + ((itemInfo1.getPrice() * itemInfo1.getQuantity()) + (itemInfo2.getPrice() * itemInfo2.getQuantity())));

   }
 }

然后对于ItemToPrint ,这与它匹配:

public class ItemToPurchase
{
    private String itemName;
    private int itemPrice;
    private int itemQuantity;

    public ItemToPurchase()
    {
        this.itemName = "none";
        this.itemPrice = 0;
        this.itemQuantity = 0;
    }

    public ItemToPurchase(String itemName, int itemPrice, int itemQuantity)
    {
        this.itemName = itemName;
        this.itemPrice = itemPrice;
        this.itemQuantity = itemQuantity;
    }

    public void setName(String itemName)
    {
        this.itemName = itemName;
    }

    public String getName()
    {
        return itemName;
    }

    public void setPrice(int itemPrice)
    {
        this.itemPrice = itemPrice;
    } 

    public int getPrice()
    {
        return itemPrice;
    }

    public void setQuantity(int itemQuantity)
    {
        this.itemQuantity = itemQuantity;
    }

    public int getQuantity()
    {
        return itemQuantity;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM