Shopping cart and change

Assignment

INSTRUCTIONS
1.
There are two parts to this assignment – ShoppingCart and ChangeMaker. You need to submit both projects on Blackboard.
2.
You are shopping for computer supplies. Ask the user to enter the items he/she wants to purchase. You pay 5% sales tax (0.05) on these purchases.

Write a Java class called ShoppingCart that will handle the total price (before and after tax). Use the following UML diagram:
ShoppingCart
Attributes
private double taxRate;
private double preTaxTotalPrice;
private double totalPrice;
private Scanner scanner;
Operations

public ShoppingCart()
public double getTaxRate()
public void setTaxRate(double value)
public void printPreTaxTotalPrice()
public void printTotalPrice()
public void addItem (int quantity, double price)
public void getInput()
The names of the variables and methods indicate their functionality.
In a tester class that holds a main method, perform the following tasks:
• Create a ShoppingCart object
• Use the appropriate method to set the tax rate
• Use the getInput method to get the input from user. This method then calls the addItem method. The addItem method keeps track of the total price (pre-tax and final price)
• Use the appropriate method to print the pre-tax total price to the console. Hint: to format a double to 2 decimal places use System.out.printf(“%.2f”, preTaxTotalPrice);
• Use the appropriate method to print the total price after taxes to the console. Your output should be similar to this:

3.
Write a Java program called ChangeMaker that takes an amount of money in dollars and cents, computes the number of quarters, dimes, nickels and pennies equivalent to this amount, and prints the result. Here is the UML diagram:
ChangeMaker
Attributes
private double money;
private int numberOfQuarters;
private int numberOfDimes;
private int numberOfNickels;
private int numberOfPennies;
Operations
public ChangeMaker()
public double getMoney()
public void setMoney(double amount)
public void calculateChange()
public String toString()

In a tester class that holds a main method, perform the following tasks:
• Create a ChangeMaker object
• Ask the user to enter the amount of money and use the appropriate method to set money value entered
• Use the calculateChange method to compute the number of quarters, dimes, nickels and pennies equivalent to this amount
• Use the toString method to print the result. For example:
$1.39 equals 5 quarters, 1 dimes, 0 nickels, and 4 pennies

Use the minimum number of coins. $1.39 also equals 0 quarters, 0 dimes, 0 nickels and 139 pennies, but you’re not going to have a very successful career as a cashier if you make change in this way.
Also test with $1.15 to make sure you get the right answer. This number gets stored as 1.14999999, so it needs the rounding above to get it right.
4.
Pay attention to coding conventions such as alignment of braces, upper- and lower-case letters in names, indentation, and white space around operators.

Solution

ShoppingCart

ShoppingCart.java

package shoppingcart;

import java.util.Scanner;

public class ShoppingCart {

private double taxRate;
private double preTaxTotalPrice;
private double totalPrice;
private Scanner scanner;

public ShoppingCart() {
scanner = new Scanner(System.in);
taxRate = 0.00;
preTaxTotalPrice = 0.00;
totalPrice = 0.00;
}

public double getTaxRate() {
return taxRate;
}

public void setTaxRate(double value) {
taxRate = value;
}

public void printPreTaxTotalPrice() {
System.out.printf(“Pre tax total price: “);
System.out.printf(“%.2f”, preTaxTotalPrice);
}

public void printTotalPrice() {
System.out.printf(“Total price: “);
System.out.printf(“%.2f”, totalPrice);
}

public void addItem(int quantity, double price) {
preTaxTotalPrice += quantity * price;
totalPrice = preTaxTotalPrice * getTaxRate() + preTaxTotalPrice;
}

public void getInput() {
System.out.println(“Please enter quantity:”);
int quantity = scanner.nextInt();

System.out.println(“Please enter price:”);
double price = scanner.nextDouble();

addItem(quantity, price);
}

}

tester.java

package shoppingcart;

public class tester {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

ShoppingCart scart = new ShoppingCart();
scart.setTaxRate(0.05);
scart.getInput();
scart.printTotalPrice();
}
}

ChangeMaker

ChangeMaker.java

package changemaker;

public class ChangeMaker {

private double money;
private int numberOfQuarters;
private int numberOfDimes;
private int numberOfNickels;
private int numberOfPennies;

public ChangeMaker() {
money = 0.00;
numberOfQuarters = 0;
numberOfDimes = 0;
numberOfNickels = 0;
numberOfPennies = 0;
}

public double getMoney() {
return money;
}

public void setMoney(double amount) {
money = amount;
}

public void calculateChange() {
numberOfPennies = (int) Math.round(money * 100);

numberOfQuarters = numberOfPennies / 25;
numberOfDimes = (numberOfPennies – numberOfQuarters * 25) / 10;
numberOfNickels = (numberOfPennies – numberOfQuarters * 25 – numberOfDimes * 10) / 5;
numberOfPennies = numberOfPennies – numberOfQuarters * 25 – numberOfDimes * 10 – numberOfNickels * 5;

}

public String toString() {
return “$” + money + ” equals ” + numberOfQuarters + ” quarters, ” + numberOfDimes + ” dimes, ” + numberOfNickels + ” nickels, and ” + numberOfPennies + ” pennies”;
}

}

tester.java

package changemaker;

import java.util.Scanner;

public class tester {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

ChangeMaker cmaker = new ChangeMaker();
System.out.println(“Please enter amount of money:”);
double amount = scanner.nextDouble();
cmaker.setMoney(amount);
cmaker.calculateChange();
System.out.println(cmaker.toString());

}
}