Question
Pictures of inputs, outputs, and expected outputs from zybook. Coding used in zybooks: mport java.util.Scanner; public class ReceiptMaker { public static final String SENTINEL =
Pictures of inputs, outputs, and expected outputs from zybook.
Coding used in zybooks:
mport java.util.Scanner; public class ReceiptMaker { public static final String SENTINEL = "checkout";
public int MAX_NUM_ITEMS;
public double TAX_RATE;
private String [] itemNames;
private double [] itemPrices;
private int numItemsPurchased;
public ReceiptMaker()
{
MAX_NUM_ITEMS = 10;
TAX_RATE = .0825;
itemNames = new String[MAX_NUM_ITEMS];
itemPrices = new double[MAX_NUM_ITEMS];
numItemsPurchased = 0;
}
public ReceiptMaker(int maxNumItems, double taxRate) {
MAX_NUM_ITEMS = maxNumItems;
TAX_RATE = taxRate;
itemNames = new String[MAX_NUM_ITEMS];
itemPrices = new double[MAX_NUM_ITEMS];
numItemsPurchased = 0;
}
public void greetUser() {
System.out.println("Welcome to the " + MAX_NUM_ITEMS + " items or less checkout line");
}
public void promptUserForProductEntry() {
System.out.println("Enter item #" + (numItemsPurchased + 1)
+ "'s name and price separated by a space");
}
public void addNextPurchaseItemFromUser(String itemName, double itemPrice) {
itemNames[numItemsPurchased] = itemName;
itemPrices[numItemsPurchased] = itemPrice;
numItemsPurchased++;
}
public double getSubtotal() {
double subTotal = 0;
for (int i = 0; i
{
subTotal += itemPrices[i];
}
return subTotal;
}
public double getMinPrice() {
double minPrice = (numItemsPurchased > 0) ? Integer.MAX_VALUE : 0;
for (int i = 0; i
{
if (itemPrices[i]
{
minPrice = itemPrices[i];
}
}
return minPrice;
}
public int getIndexOfMinPrice() {
int indexOfMin = 0;
for (int i = 1; i
{
if (itemPrices[i]
{
indexOfMin = i;
}
}
return indexOfMin;
}
public double getMaxPrice() {
double maxPrice = (numItemsPurchased > 0) ? Integer.MIN_VALUE : 0;
for (int i = 0; i
{
if (itemPrices[i] > maxPrice)
{
maxPrice = itemPrices[i];
}
}
return maxPrice;
}
public int getIndexOfMaxPrice() {
int indexOfMax = 0;
for (int i = 1; i
{
if (itemPrices[i] > itemPrices[indexOfMax])
{
indexOfMax = i;
}
}
return indexOfMax;
}
public double getMeanPrice()
{
if (numItemsPurchased > 0)
{
return getSubtotal() / numItemsPurchased;
}
return 0;
}
public double getTaxOnSubtotal() {
return getSubtotal() * TAX_RATE;
}
public double getTotal() {
return getSubtotal() + getTaxOnSubtotal();
}
public void displayReceipt() {
System.out.println("-------------------------------------------------");
System.out.printf("Subtotal: $ %04.2f | # of Items %02d ", getSubtotal(), numItemsPurchased);
System.out.printf("Tax: $ %05.2f ", getTaxOnSubtotal());
System.out.printf("Total: $ %04.2f ", getTotal());
System.out.println("--------------------THANK YOU--------------------");
}
public void displayReceiptStats() {
System.out.println(" -----------------RECEIPT STATS-----------------");
System.out.printf("Min Item Name: %10s | Price: $ %04.2f ",
itemNames[getIndexOfMinPrice()], getMinPrice());
System.out.printf("Max Item Name: %10s | Price: $ %04.2f ", itemNames[getIndexOfMaxPrice()],
getMaxPrice());
System.out.printf("Mean price of %02d items purchased: $ %04.2f ",
numItemsPurchased, getMeanPrice());
}
public void displayAllItemsWithPrices() {
System.out.println(" ---------------RECEIPT BREAKDOWN---------------");
for (int i = 0; i
{
System.out.printf("Item #%02d Name: %10s | Price: $ %04.2f ", (i + 1),
itemNames[i], itemPrices[i]);
}
}
private double getItemPriceFromUser(Scanner scanner) {
double price = -1;
while ((price = scanner.nextDouble())
{
System.out.println("Price \"" + price + "\" cannot be negative. Reenter price");
}
return price;
}
public void scanCartItems(Scanner scanner) {
while (numItemsPurchased
{
promptUserForProductEntry();
String token1;
double token2 = -1;
if (!(token1 = scanner.next()).equalsIgnoreCase(SENTINEL))
{
token2 = getItemPriceFromUser(scanner);
addNextPurchaseItemFromUser(token1, token2);
}
else
{
break;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ReceiptMaker rm = new ReceiptMaker();
rm.greetUser();
rm.scanCartItems(scanner);
rm.displayReceipt();
rm.displayReceiptStats();
rm.displayAllItemsWithPrices();
scanner.close();
}
}
apples 5.32 bananas 3.78 cantaloupe 2.99 donuts 6.55 eggs 4.87 flour 3.21 grapes 5.89 hummus 7.63 ice 1.99 jello 2.75 Welcome to the 10 items or less checkout line Enter item \#1's name and price separated by a space Enter item \#2's name and price separated by a space Enter item \# 3,3 name and price separated by a space Enter item \#4's name and price separated by a space Enter item \# 5s name and price separated by a space Enter item \#6's name and price separated by a space Enter item #7s name and price separated by a space Enter item \#8's name and price separated by a space Enter item \#9's name and price separated by a space Enter item \#10's name and price separated by a space Subtotal: $44.98 \# of Items 10 Tax: \$03.71 Total: $48.69 Min Item Name: ice | Price: $1.99 Max Item Name: humus | Price: $7.63 Mean price of 10 items purchased: $4.50 Expected output Expected output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started