This assignment is to be completed in JAVA specifically NetBeans. I have tried using the aolutions already given and I keep getting an error code.
Create a program using classes that does the following using your NetBeans IDE and upload it here:
(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:
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
(3) Add the costs of the two items together and output the total cost. (2 pts)
Ex:
TOTAL COST Chocolate Chips 1 @ $3 = $3 Bottled Water 10 @ $1 = $10 Total: $13
Expert Answer
Java Code:
ItemToPurchase class:
package shopping_cart_printer;
public class ItemToPurchase {
private String itemName;
private int itemPrice;
private int itemQuantity;
public ItemToPurchase(){
this.itemName = “none”;
this.itemPrice = 0;
this.itemQuantity =0;
}
public void setName(String itemName){
this.itemName = itemName;
}
public String getName(){
return this.itemName;
}
public void setPrice(int itemPrice){
this.itemPrice = itemPrice;
}
public int getPrice(){
return this.itemPrice;
}
public void setQuantity(int itemQuantity){
this.itemQuantity = itemQuantity;
}
public int getQuantity(){
return this.itemQuantity;
}
}
ShoppingCartPrinter class:
package shopping_cart_printer;
import java.util.Scanner;
public class ShoppingCartPrinter {
// fuction to print shopping cart
public static void totalCost(ItemToPurchase item1, ItemToPurchase item2){
System.out.println(“TOTAL COST”);
System.out.println(item1.getName()+” “+item1.getQuantity()+” @ $”+item1.getPrice()+” = $”+item1.getQuantity()*item1.getPrice());
System.out.println(item2.getName()+” “+item2.getQuantity()+” @ $”+item2.getPrice()+” = $”+item2.getQuantity()*item2.getPrice());
System.out.println();
System.out.println(“Total: $”+(item1.getQuantity()*item1.getPrice()+item2.getQuantity()*item2.getPrice()));
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String itemName1, itemName2;
int itemPrice1, itemQuantity1, itemPrice2, itemQuantity2;
ItemToPurchase item1, item2;
// Input item-1 details
System.out.println(“Item 1”);
System.out.print(“Enter the item name: “);
itemName1 = scnr.nextLine();
System.out.print(“Enter the item price: “);
itemPrice1 = Integer.valueOf(scnr.nextLine());
System.out.print(“Enter the item quantity: “);
itemQuantity1 = Integer.valueOf(scnr.nextLine());
item1 = new ItemToPurchase();
// set item1 details
item1.setName(itemName1);
item1.setPrice(itemPrice1);
item1.setQuantity(itemQuantity1);
System.out.println();
// Input item-2 details
System.out.println(“Item 2”);
System.out.print(“Enter the item name: “);
itemName2 = scnr.nextLine();
System.out.print(“Enter the item price: “);
itemPrice2 = Integer.valueOf(scnr.nextLine());
System.out.print(“Enter the item quantity: “);
itemQuantity2 = Integer.valueOf(scnr.nextLine());
item2 = new ItemToPurchase();
// set item2 details
item2.setName(itemName2);
item2.setPrice(itemPrice2);
item2.setQuantity(itemQuantity2);
System.out.println();
// Print Shopping cart
totalCost(item1, item2);
scnr.close();
}
}
Output screenshot: