In: Computer Science
5.9 Online shopping cart (Java)
Create a program using
classes that does the following in the zyLabs developer below. For
this lab, you will be working with two different class files. To
switch files, look for where it says "Current File" at the top of
the developer window. Click the current file name, then select the
file you need.
(1) Create two files to submit:
Build the ItemToPurchase class with the following specifications:
(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
public class ItemToPurchase { private String itemName; private String itemDescription; private int itemPrice; private int itemQuantity; public ItemToPurchase() { this.itemName = "none"; this.itemDescription = "none"; this.itemPrice = 0; this.itemQuantity = 0; } public ItemToPurchase(String itemName, String itemDescription, int itemPrice, int itemQuantity) { this.itemName = itemName; this.itemDescription = itemDescription; this.itemPrice = itemPrice; this.itemQuantity = itemQuantity; } public void setName(String name) { itemName = name; } public void setPrice(int price) { itemPrice = price; } public void setQuantity(int quantity) { itemQuantity = quantity; } public void setDescription(String description) { itemDescription = description; } public String getName() { return itemName; } public int getPrice() { return itemPrice; } public int getQuantity() { return itemQuantity; } public void printItemPurchase() { System.out.println(itemName + " " + itemQuantity + " @ $" + itemPrice + " = $" + (itemPrice * itemQuantity)); } public void printItemDescription() { System.out.println(itemName + ": " + itemDescription + ", " + itemQuantity + " oz."); } }
import java.util.Scanner; public class ShoppingCartPrinter { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String productName; int productPrice; int productQuantity; ItemToPurchase item1 = new ItemToPurchase(); ItemToPurchase item2 = new ItemToPurchase(); System.out.println("Item 1"); System.out.println("Enter the item name:"); productName = scnr.nextLine(); item1.setName(productName); System.out.println("Enter the item price:"); productPrice = scnr.nextInt(); item1.setPrice(productPrice); System.out.println("Enter the item quantity:"); productQuantity = scnr.nextInt(); item1.setQuantity(productQuantity); System.out.println(); scnr.nextLine(); System.out.println("Item 2"); System.out.println("Enter the item name:"); productName = scnr.nextLine(); item2.setName(productName); System.out.println("Enter the item price:"); productPrice = scnr.nextInt(); item2.setPrice(productPrice); System.out.println("Enter the item quantity:"); productQuantity = scnr.nextInt(); item2.setQuantity(productQuantity); System.out.println(); System.out.println("TOTAL COST"); item1.printItemPurchase(); item2.printItemPurchase(); System.out.println(); System.out.println("Total: $" + ((item1.getPrice() * item1.getQuantity()) + (item2.getPrice() * item2.getQuantity()))); } }