Question

Warm up: Online shopping cart (Part 1)

8.6 LAB*: Warm up: Online shopping cart (Part 1)

(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


1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
import java.util.Scanner;

public class ShoppingCartPrinter {
   
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      ItemToPurchase item1 = new ItemToPurchase();
      ItemToPurchase item2 = new ItemToPurchase();
      
      String itemName1;
      int itemPrice1;
      int itemQuantity1;
      int itemCost1;
      
      System.out.println("Item 1");
      
      System.out.println("Enter the item name:");
      itemName1 = scnr.nextLine();
      item1.setName(itemName1);
//      System.out.println(itemName1); // debug
      
      System.out.println("Enter the item price:");
      itemPrice1 = scnr.nextInt();
      item1.setPrice(itemPrice1);
//      System.out.println(itemPrice1); // debug
      
      System.out.println("Enter the item quantity:");
      itemQuantity1 = scnr.nextInt();
      item1.setQuantity(itemQuantity1);
//      System.out.println(itemQuantity1); // debug
      
      itemName1 = item1.getName();
      itemQuantity1 = item1.getQuantity();
      itemPrice1 = item1.getPrice();
      itemCost1 = itemPrice1 * itemQuantity1;
      
      //-----------------------------------------------------------------------------------------------------//
      
      String itemName2;
      int itemPrice2;
      int itemQuantity2;
      int itemCost2;
      
      System.out.println("\nItem 2");
      
      System.out.println("Enter the item name:");
      itemName2 = scnr.next() + scnr.nextLine();
      item2.setName(itemName2);
//      System.out.println(itemName2); // debug
      
      System.out.println("Enter the item price:");
      itemPrice2 = scnr.nextInt();
      item2.setPrice(itemPrice2);
//      System.out.println(itemPrice2); // debug
      
      System.out.println("Enter the item quantity:");
      itemQuantity2 = scnr.nextInt();
      item2.setQuantity(itemQuantity2);
//      System.out.println(itemQuantity2); // debug
      
      itemName2 = item2.getName();
      itemQuantity2 = item2.getQuantity();
      itemPrice2 = item2.getPrice();
      itemCost2 = itemPrice2 * itemQuantity2;
      
      //-----------------------------------------------------------------------------------------------------//
      
      int totalCost;
      
      totalCost = itemCost1 + itemCost2;
      
      System.out.println("\nTOTAL COST");
      
      System.out.println(itemName1 + " " + itemQuantity1 +  " @ $" + itemPrice1 + " = $" + itemCost1);
      System.out.println(itemName2 + " " + itemQuantity2 +  " @ $" + itemPrice2 + " = $" + itemCost2);
      System.out.println("\nTotal: $" + totalCost);
      
   }
   
}
public class ItemToPurchase {
   
   private String itemName;
   private int itemPrice;
   private int itemQuantity;
   
   public void setName(String name) {
      
      itemName = name;
      
   }
   
   public String getName() {
      
      return itemName;
      
   }
   
   public void setPrice(int price) {
      
      itemPrice = price;
      
   }
   
   public int getPrice() {
      
      return itemPrice;
      
   }
   
   public void setQuantity(int quantity) {
      
      itemQuantity = quantity;
      
   }
   
   public int getQuantity() {
      
      return itemQuantity;
      
   }

}


Add a comment
Know the answer?
Add Answer to:
Warm up: Online shopping cart (Part 1)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 20.1 LAB*: Warm up: Online shopping cart (Part 1) in C++ Please: The answers are already...

    20.1 LAB*: Warm up: Online shopping cart (Part 1) in C++ Please: The answers are already on chegg are not working. (1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor...

  • 4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping...

    4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (solution from previous lab assignment is provided in Canvas). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() -...

  • Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1)...

    Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1) (1) Create three files to submit: • Item ToPurchase.h - Struct definition and related function declarations • Item ToPurchase.c-Related function definitions • main.c-main function Build the ItemToPurchase struct with the following specifications: • Data members (3 pts) • char itemName • int itemPrice • int itemQuantity • Related functions • MakeltemBlank0 (2 pts) Has a pointer to an item To Purchase parameter. Sets item's...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • 11.11 LAB: Warm up: Online shopping cart (1) Build the ItemToPurchase class with the following specifications:...

    11.11 LAB: Warm up: Online shopping cart (1) Build the ItemToPurchase class with the following specifications: Attributes (6 pts) item_name (string) item_price (float) item_quantity (int) Default constructor (2 pt) Initializes item's name = "none", item's price = 0, item's quantity = 0 Method print_item_cost() Ex. of print_item_cost() output: Bottled Water 10 @ $1 = $10 (2) In the main section of your code, prompt the user for two items and create two objects of the ItemToPurchase class. (4 pts) Ex:...

  • Need three seperate files. ShoppingCartManager.java ShoppingCart.java ItemsToPurchase.java These are from part 1 what do you mean...

    Need three seperate files. ShoppingCartManager.java ShoppingCart.java ItemsToPurchase.java These are from part 1 what do you mean by deep study 7.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input This program extends the earlier Online shopping cart program (Consider...

  • I need help with this assignment, can someone HELP ? This is the assignment: Online shopping...

    I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public...

    I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public class ItemToPurchase {    // instance variables    private String itemName;    private String itemDescription;    private int itemPrice;    private int itemQuantity;    // default constructor    public ItemToPurchase() {        this.itemName = "none";        this.itemDescription = "none";        this.itemPrice = 0;        this.itemQuantity = 0;    }    public ItemToPurchase(String itemName, int itemPrice, int itemQuantity,String itemDescription) {   ...

  • 8.7 LAB*: Program: Online shopping cart (Part 2)

    8.7 LAB*: Program: Online shopping cart (Part 2)Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT