Advertisement
ThanosIsCool

6.20 Warm up: Online shopping cart (Java)

Aug 5th, 2019
2,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. /*package shoppingcartprinter;*/
  2. /*COMMENT OUT PACKAGE NAME IN MAIN METHOD AND OTHER CLASS */
  3. public class ItemToPurchase {
  4.  
  5.     private String itemName;
  6.  
  7.     private int itemPrice;
  8.  
  9.     private int itemQuantity;
  10.  
  11.     public ItemToPurchase() {
  12.  
  13.         itemName = "none";
  14.  
  15.         itemPrice = 0;
  16.  
  17.         itemQuantity = 0;
  18.  
  19.     }
  20.  
  21.     public String getName() {
  22.  
  23.         return itemName;
  24.  
  25.     }
  26.  
  27.     public void setName(String itemName) {
  28.  
  29.         this.itemName = itemName;
  30.  
  31.     }
  32.  
  33.     public int getPrice() {
  34.  
  35.         return itemPrice;
  36.  
  37.     }
  38.  
  39.     public void setPrice(int itemPrice) {
  40.  
  41.         this.itemPrice = itemPrice;
  42.  
  43.     }
  44.  
  45.     public int getQuantity() {
  46.  
  47.         return itemQuantity;
  48.  
  49.     }
  50.  
  51.     public void setQuantity(int itemQuantity) {
  52.  
  53.         this.itemQuantity = itemQuantity;
  54.  
  55.     }
  56. }
  57.  
  58. public class ShoppingCartPrinter {
  59.  
  60.     public static void main(String[] args) {
  61.         Scanner scan = new Scanner(System.in);
  62.  
  63.         ItemToPurchase item1 = new ItemToPurchase();
  64.  
  65.         ItemToPurchase item2 = new ItemToPurchase();
  66.  
  67.         System.out.println("Item 1");
  68.  
  69.         System.out.println("Enter the item name: ");
  70.  
  71.         String name1 = scan.nextLine();
  72.  
  73.         System.out.println("Enter the item price: ");
  74.  
  75.         int price1 = scan.nextInt();
  76.  
  77.         System.out.println("Enter the item quantity: \n");
  78.  
  79.         int quantity1 = scan.nextInt();
  80.  
  81.         item1.setName(name1);
  82.  
  83.         item1.setPrice(price1);
  84.  
  85.         item1.setQuantity(quantity1);
  86.  
  87.         scan.nextLine();
  88.  
  89.         System.out.println("Item 2");
  90.  
  91.         System.out.println("Enter the item name: ");
  92.  
  93.         String name2 = scan.nextLine();
  94.  
  95.         System.out.println("Enter the item price: ");
  96.  
  97.         int price2 = scan.nextInt();
  98.  
  99.         System.out.println("Enter the item quantity: \n");
  100.  
  101.         int quantity2 = scan.nextInt();
  102.  
  103.         item2.setName(name2);
  104.  
  105.         item2.setPrice(price2);
  106.  
  107.         item2.setQuantity(quantity2);
  108.  
  109.         System.out.println("TOTAL COST");
  110.  
  111.         int item1Total = item1.getPrice() * item1.getQuantity();
  112.  
  113.         int item2Total = item2.getPrice() * item2.getQuantity();
  114.  
  115.         System.out.println(
  116.                 item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Total);
  117.  
  118.         System.out.println(
  119.                 item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Total);
  120.  
  121.         System.out.println();
  122.  
  123.         System.out.println("Total: $" + (item1Total + item2Total));
  124.  
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement