(1) Create two files to submit: ItemToPurchase.java - Class definition ShoppingC
ID: 3796917 • Letter: #
Question
(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() (2 pts)
setPrice() & getPrice() (2 pts)
setQuantity() & getQuantity() (2 pts)
(2) 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. (2 pts)
Ex:
(3) Add the costs of the two items together and output the total cost. (2 pts)
Ex:
Here is my code below I have created for the ShoppingCartPrinter.java, However when ever I try to compile my code it is not compiling, it's giving me errors for which I don't understand or maybe which I don't know how to fix. Can you go over through my code and find where I am making misatakes.
import java.util.Scanner;
public class ShoppingCartPrinter
{
public static void main(String[] args) {
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
Scanner scnr = new Scanner(System.in);
System.out.println("Item 1");
System.out.print("Enter the item name: ");
String name1 = scnr.nextLine();
System.out.print("Enter the item price: ");
int price1 = scnr.nextInt();
System.out.print("Enter the item quantity: ");
int quantity1 = scnr.nextInt();
item1.setName(name1);
item1.setPrice(price1);
item1.setQuantity(quantity1);
scnr.nextLine();
System.out.println("Item 2");
System.out.print("Enter the item name: ");
String name2 = scnr.nextLine();
System.out.print("Enter the item price: ");
int price2 = scnr.nextInt();
System.out.print("Enter the item quantity: ");
int quantity2 = scnr.nextInt();
item2.setName(name2);
item2.setPrice(price2);
item2.setQuantity(quantity2);
System.out.println("TOTAL COST");
int item1Total = item1.getPrice() * item1.getQuantity();
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item1.getName()+" "+item1.getQuantity()+" for $"+item1.getPrice()+" = $"+item1Total);
System.out.println(item2.getName()+" "+item2.getQuantity()+" for $"+item2.getPrice()+" = $"+item2Total);
System.out.println();
System.out.println("Total: $"+(item1Total + item2Total));
}
}
Explanation / Answer
ShoppingCartPrinter.java
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.print("Enter the item name: ");
String name1 = scan.nextLine();
System.out.print("Enter the item price: ");
int price1 = scan.nextInt();
System.out.print("Enter the item quantity: ");
int quantity1 = scan.nextInt();
item1.setName(name1);
item1.setPrice(price1);
item1.setQuantity(quantity1);
scan.nextLine();
System.out.println("Item 2");
System.out.print("Enter the item name: ");
String name2 = scan.nextLine();
System.out.print("Enter the item price: ");
int price2 = scan.nextInt();
System.out.print("Enter the item quantity: ");
int quantity2 = scan.nextInt();
item2.setName(name2);
item2.setPrice(price2);
item2.setQuantity(quantity2);
System.out.println("TOTAL COST");
int item1Total = item1.getPrice() * item1.getQuantity();
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item1.getName()+" "+item1.getQuantity()+" for $"+item1.getPrice()+" = $"+item1Total);
System.out.println(item2.getName()+" "+item2.getQuantity()+" for $"+item2.getPrice()+" = $"+item2Total);
System.out.println();
System.out.println("Total: $"+(item1Total + item2Total));
}
}
ItemToPurchase.java
public class ItemToPurchase {
private String itemName ;
private int itemPrice;
private int itemQuantity;
public ItemToPurchase(){
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
public String getName() {
return itemName;
}
public void setName(String itemName) {
this.itemName = itemName;
}
public int getPrice() {
return itemPrice;
}
public void setPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getQuantity() {
return itemQuantity;
}
public void setQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
}
Output:
Item 1
Enter the item name: Chocolate Chips
Enter the item price: 3
Enter the item quantity: 1
Item 2
Enter the item name: Bottled Water
Enter the item price: 1
Enter the item quantity: 10
TOTAL COST
Chocolate Chips 1 for $3 = $3
Bottled Water 10 for $1 = $10
Total: $13
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.