Answer:
import java.util.Scanner;
public class ReceiptMaker {
public static final String SENTINEL = "checkout";
public final double MIN_NUM_ITEMS;
public final double TAX_RATE;
private String [] itemNames;
private double [] itemPrices;
private int numItemsPurchased;
public ReceiptMaker(){
MIN_NUM_ITEMS = 10;
TAX_RATE = .0825;
itemNames = new String[(int)MIN_NUM_ITEMS];
itemPrices = new double[(int)MIN_NUM_ITEMS];
numItemsPurchased = 0;
}
public ReceiptMaker(int maxNumItems, double taxRate){
MIN_NUM_ITEMS = maxNumItems;
TAX_RATE = taxRate;
itemNames = new String[(int)MIN_NUM_ITEMS];
itemPrices = new double[(int)MIN_NUM_ITEMS];
numItemsPurchased = 0;
}
public void greetUser(){
System.out.println("Welcome to the "+MIN_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<numItemsPurchased; i++){
subTotal += itemPrices[i];
}
return subTotal;
}
public double getMinPrice(){
double minPrice = (numItemsPurchased > 0) ? Integer.MAX_VALUE : 0;
for(int i=0; i<numItemsPurchased; i++){
minPrice = Math.min(minPrice, itemPrices[i]);
}
return minPrice;
}
public double getMaxPrice(){
double maxPrice = (numItemsPurchased > 0) ? Integer.MIN_VALUE : 0;
for(int i=0; i<numItemsPurchased; i++){
maxPrice = Math.max(maxPrice, itemPrices[i]);
}
return maxPrice;
}
public double getMeanPrice(){
if(numItemsPurchased == 0) return 0;
return getSubtotal() / numItemsPurchased;
}
public void printReceipt()
System.out.println("Subtotal: $"+getSubtotal()+"
public void printReceiptStats(){
System.out.println("-----------------RECEIPT STATS-----------------");
String minItemName = "";
double minPrice = getMinPrice();
for(int i=0; i<numItemsPurchased; i++){
if(itemPrices[i] == minPrice){
minItemName = itemNames[i];
break;
}
}
System.out.println("Min Item Name: "+minItemName+" | Price: $"+minPrice);
String maxItemName = "";
double maxPrice = getMaxPrice();
for(int i=0; i<numItemsPurchased; i++){
if(itemPrices[i] == maxPrice){
maxItemName = itemNames[i];
break;
}
}
System.out.println("Max Item Name: "+maxItemName+" | Price: $"+maxPrice);
System.out.println("Mean price of "+numItemsPurchased+" items purchased: $"+getMeanPrice());
}
public void printReceiptBreakdown(){
System.out.println("---------------RECEIPT BREAKDOWN---------------");
for(int i=0; i<numItemsPurchased; i++)
System.out.println("Item #"+String.format("%02d", i+1)+" Name: "+itemNames[i]+"
}
public static void main(String[] args){
ReceiptMaker receipt = new ReceiptMaker();
Scanner input = new Scanner(System.in);
receipt.greetUser();
while(receipt.numItemsPurchased < receipt.MIN_NUM_ITEMS){
receipt.promptUserForProductEntry();
String itemName = input.next();
if(itemName.equals(SENTINEL)) break;
double itemPrice = input.nextDouble();
while(itemPrice < 0){
System.out.println("Price "+itemPrice+" cannot be negative.");
System.out.println("Reenter price");
itemPrice = input.nextDouble();
}
receipt.addNextPurchaseItemFromUser(itemName, itemPrice);
}
receipt.printReceipt();
receipt.printReceiptStats();
receipt.printReceiptBreakdown();
}
}
Step-by-step explanation: