1

Create two files to submit: ItemToPurchase.java - Class definition ShoppingCartPrinter.java - Contains main() method

~Build the ItemToPurchase class with the following specifications:

Private fields
-String itemName - Initialized in default constructor to "none"
-int itemPrice - Initialized in default constructor to 0
-int itemQuantity - Initialized in default constructor to 0
Default constructor
Public member methods (mutators & accessors)
-setName() & getName()
-setPrice() & getPrice()
-setQuantity() & getQuantity()

-In main(), prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnr.nextLine(); to allow the user to input a new string.

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

Below is my code for 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.
}

Below is my code for 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;
   }
}

I getting more and more confused as I look for resources to help me and haven't heard back from my Professor. The code builds without errors but doesn't copy. This is he error I am getting: enter image description here

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Casey Vanecek
  • 43
  • 1
  • 1
  • 4
  • 1
    I have a hard time understanding the core of your question. Your code looks fine so far; but I am not exactly sure what ANT is supposed to do here. Step number one would be: you run *javac* manually, and then try to run your main method manually using *java*. And: you got why to many tags on your question. – GhostCat Sep 30 '16 at 19:21
  • I am confused on why it won't print out the values for Name, Price, and Quantity. When I build in NetBeans no errors come up but there is nothing to copying. Up until this point my class has only has us writing code in the main but now they want us to use multiple classes. – Casey Vanecek Sep 30 '16 at 19:38
  • As said: try to step away from your IDE. Run the command line tools to avoid that additional complexity of netbeans. – GhostCat Sep 30 '16 at 19:39
  • 2
    Your screengrab shows you compiling your code, but I don't see where it actually tried to start running it. – Joe C Sep 30 '16 at 20:12
  • When I run the code I am getting an error when it is asking for item two's price. It jumps from name to price so I cannot put the name in.. – Casey Vanecek Sep 30 '16 at 20:27
  • You don't show any code in your question that asks for an items name or price. Please show the relevant code that is giving you issue. Also, if you are using a `Scanner` then this will likely be your issue: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – sorifiend Jul 04 '22 at 01:08

1 Answers1

1

For the ShoppingCartPrinter, this works:

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())));

   }
 }

And then for the ItemToPrint, this matches up with it:

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

}
Unheilig
  • 16,196
  • 193
  • 68
  • 98