Question

In: Computer Science

HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...

HOMEWORK PROJECT #1 – SHOPPING CART

Part I.

Create two files to submit:

  • ItemToPurchase.java – Class Definition
  • ShoppingCartPrinter.java – Contains main() method
  1. Build the ItemToPurchase class with the following specifications:

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

  1. In main method of the driver (ShoppingCartPrinter.java) prompt the user for two items and create two objects of the ItemToPurchase class.

         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

  1. Add the costs of the two items together and output the total cost.

Output Example:

TOTAL COST

Chocolate Chips 1 @ $3 = $3

Bottled Water 10 @ $1 = $10

Total: $13

Solutions

Expert Solution

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

    }
}

==================================================================


Related Solutions

In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method Build the Payroll class with the following specifications: 4 private fields String name - Initialized in default constructor to "John Doe" int ID - Initialized in default constructor to 9999 doulbe payRate - Initialized in default constructor to 15.0 doulbe hrWorked - Initialized in default constructor to 40 2 constructors (public) Default constructor A constructor that accepts the employee’s name, ID, and pay rate...
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***...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(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 to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
I have a homework class, but I don't really understand anything and I have to submit...
I have a homework class, but I don't really understand anything and I have to submit my homework next week. Homework must be written in C ++ program language. Can someone help me please... Working with classes (everything written below is one task): Define a class Date that contains integer variables for day, month, and year. 1.1. Create the necessary methods for the class: set, get, default constructor, constructor with arguments. 1.2. Create a method that calculates the number of...
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...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT