0% found this document useful (0 votes)
65 views4 pages

Name Price Quantity: Import Public Class Private Private Double Private Int

This document contains code for a ShoppingCart class that allows adding items to a shopping cart, represented as an array. The class tracks the total price and has methods to add items, return the cart contents as a string, and increase the capacity of the cart array when needed. It also includes a ShoppingDriver class to simulate a shopping experience by prompting the user for item details and adding them to a cart, then printing the cart and total.

Uploaded by

Jumma Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Name Price Quantity: Import Public Class Private Private Double Private Int

This document contains code for a ShoppingCart class that allows adding items to a shopping cart, represented as an array. The class tracks the total price and has methods to add items, return the cart contents as a string, and increase the capacity of the cart array when needed. It also includes a ShoppingDriver class to simulate a shopping experience by prompting the user for item details and adding them to a cart, then printing the cart and total.

Uploaded by

Jumma Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//

//
//
//
//

***********************************************************
Item.java
Represents an item in a shopping cart.
***********************************************************

import java.text.NumberFormat;
public class Item
{
private String name;
private double price;
private int quantity;
// ------------------------------------------------------// Create a new item with the given attributes.
// ------------------------------------------------------public Item (String itemName, double itemPrice, int numPurchased)
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}
// ------------------------------------------------------// Return a string with the information about the item.
// ------------------------------------------------------public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();

return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"


+ fmt.format(price*quantity));

// ------------------------------------------------------// Returns the unit price of the item.


// ------------------------------------------------------public double getPrice()
{
return price;
}
// ------------------------------------------------------// Returns the name of the item.
// ------------------------------------------------------public String getName()
{
return name;
}

// ------------------------------------------------------// Returns the quantity of the item.


// ------------------------------------------------------public int getQuantity()
{
return quantity;
}

//
//
//
//
//

***********************************************************
ShoppingCart.java
Represents a shopping cart as an array of items.
***********************************************************

//import Item
import java.text.NumberFormat;
public class ShoppingCart
{
private int itemCount;
private double totalPrice;
private int capacity;

// Total number of items in the cart.


// Total price of items in the cart.
// Current cart capacity

// Instance variable cart is an array of type Item.


private Item[] cart, temp;
// Change Request 1a, 1b
// ------------------------------------------------------// Creates an empty shopping cart with a capacity of 5 items.
// ------------------------------------------------------public ShoppingCart()
{
capacity = 5;
itemCount = 0;
totalPrice = 0.0;
cart = new Item[capacity]; // CR 1a
}
// ------------------------------------------------------// Adds an item to the shopping cart. Change Request 1c
// ------------------------------------------------------public void addToCart(String itemName, double price, int quantity)
{
if (itemCount == cart.length)
// Already maximum length of array?
increaseSize();
// Yes -- need to increase
size of array
// No -- add to
shopping cart
Item myItem = new Item(itemName, price, quantity);
// Instantiate the Item
cart[itemCount] = myItem;
// Add to the end of the array
itemCount++;
// increment count of
elements

// update total price of all items in the shopping cart


totalPrice += myItem.getPrice() * (double) myItem.getQuantity();

// ------------------------------------------------------// Returns the contents of the cart together with


// summary information.
// ------------------------------------------------------public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String contents = "\nShopping Cart\n";
contents += "\nItem\tPrice\t\tQuantity\tTotal\n";
for (int i = 0; i < itemCount; i++)

contents += cart[i].toString() + "\n";


contents += "\nTotal Price: " + fmt.format(totalPrice);
contents += "\n";
}

return contents;

// ------------------------------------------------------// Increase the capacity of the shopping cart by 3. CR 1b


// ------------------------------------------------------private void increaseSize()
{
temp = new Item[cart.length + 3];
// CR 1b
for (int i = 0; i < cart.length; i++) // load current cart into temp
temp[i] = cart[i];
}

cart = temp;

// now rebuild cart from temp

// ***********************************************************
// ShoppingDriver.java

Author: Mrs. Logan

//
// Simulate a shopping experience by continuing to shop as long
// as the user wants to. Read in the name, price, and quantity
// of the item the user wants to add to the cart. After the
// item is added to the cart, the cart contents should be printed.
// When the user is finished shopping print a "Please pay ..."
// message with the total price of the items in the cart.
// ***********************************************************

import java.text.NumberFormat;
import java.util.Scanner;

public class ShoppingDriver


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
ShoppingCart myCart = new ShoppingCart();
NumberFormat fmt = NumberFormat.getCurrencyInstance();

String tmpName;
double tmpPrice, myCartTotal = 0.0;
int tmpQuantity;

String keepShopping = "Y";

while (keepShopping.equalsIgnoreCase("Y"))
{
// obtain information about item purchased
System.out.println("Please enter the name of the item you wish to purchase:
");
tmpName = scan.next();
System.out.println("Please enter the price of the item: ");
tmpPrice = scan.nextDouble();
System.out.println("Please enter the quantity purchased: ");
tmpQuantity = scan.nextInt();

// add item purchased to shopping cart


myCart.addToCart(tmpName, tmpPrice, tmpQuantity);
myCartTotal += tmpPrice * (double) tmpQuantity;

// print out current contents of shopping cart


System.out.println(myCart.toString());

System.out.println("\nDo you want to continue shopping? (y/n)");


keepShopping = scan.next();
}

System.out.println("Please pay ..." + fmt.format(myCartTotal));


}

You might also like