In: Computer Science
HOMEWORK PROJECT #1 – SHOPPING CART
Part I.
Create two files to submit:
Specifications |
Description |
ItemToPurchase(itemName) |
itemName – The name will be a String datatype and Initialized in default constructor to “none”. |
ItemToPurchase(itemPrice) |
itemPrice – The price will be integer datatype and Initialized in default constructor to 0. |
ItemToPurchase(itemQuantity) |
itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0. |
Public Member methods |
Use of Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() |
Your output should look like this:
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
Output Example:
TOTAL COST
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10
Total: $13
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== public class ItemToPurchase { private String iteName; private int itemPrice; private int itemQuantity; public ItemToPurchase() { itemPrice = 0; itemQuantity = 0; iteName = "none"; } public String getName() { return iteName; } public void setName(String iteName) { this.iteName = iteName; } public int getPrice() { return itemPrice; } public void setPrice(int itemPrice) { if (itemPrice >= 0) this.itemPrice = itemPrice; } public int getQuantity() { return itemQuantity; } public void setItemQuantity(int itemQuantity) { if (itemQuantity > 0) this.itemQuantity = itemQuantity; } }
==================================================================
import java.util.Scanner; public class ShoppingCartPrinter { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); System.out.println("Item 1"); System.out.println("Enter the item name: "); String itemName = scnr.nextLine(); System.out.println("Enter the item price: "); int price = scnr.nextInt(); System.out.println("Enter the item quantity: "); int quantity = scnr.nextInt(); ItemToPurchase itemOne = new ItemToPurchase(); itemOne.setName(itemName); itemOne.setPrice(price); itemOne.setItemQuantity(quantity); scnr.nextLine(); System.out.println("\nItem 2"); System.out.println("Enter the item name: "); itemName = scnr.nextLine(); System.out.println("Enter the item price: "); price = scnr.nextInt(); System.out.println("Enter the item quantity: "); quantity = scnr.nextInt(); ItemToPurchase itemTwo = new ItemToPurchase(); itemTwo.setName(itemName); itemTwo.setPrice(price); itemTwo.setItemQuantity(quantity); System.out.println(); System.out.println("TOTAL COST"); System.out.println(itemOne.getName() + " " + itemOne.getQuantity() + " @ $" + itemOne.getPrice() + " = $" + itemOne.getQuantity() * itemOne.getPrice()); System.out.println(itemTwo.getName() + " " + itemTwo.getQuantity() + " @ $" + itemTwo.getPrice() + " = $" + itemTwo.getQuantity() * itemTwo.getPrice()); int total = itemOne.getPrice() * itemOne.getQuantity() + itemTwo.getQuantity() * itemTwo.getPrice(); System.out.println(); System.out.println("Total: $" + total); } }
==================================================================