Question

In: Computer Science

5.9 Online shopping cart (Java) Create a program using classes that does the following in the...

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:

  • 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

Solutions

Expert Solution

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())));
    }
}


Related Solutions

7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase struct to contain a new data member. (2 pt) char itemDescription[ ] - set to "none" in MakeItemBlank() Implement the following related functions for the ItemToPurchase struct. PrintItemDescription() Has an ItemToPurchase parameter. Ex. of PrintItemDescription() output: Bottled Water: Deer Park, 12 oz. (2) Create three new files: ShoppingCart.h - struct definition and...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) 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 the quantity, price, and subtotal PrintItemDescription() - Outputs the...
This week you will write a program that mimics an online shopping cart . You will...
This week you will write a program that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers for quantities -...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Your shopping cart should support...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Using the CLASSES BELOW Your...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). **PLEASE USE THE CLASSES BELOW***...
Python This week you will write a program in Python that mimics an online shopping cart...
Python This week you will write a program in Python that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT