import java.util.Scanner; import java.text.DecimalFormat; public class Calculator { Scanner scan = new Scanner(System.in); //Creates array to hold fifteen references of type Register //or of any type that implements Register. Register[] items = new Register[15]; //Variable keeps track of the array index. int count = 0; double rTotal = 0; public static void main(String[] args) { DecimalFormat fmt = new DecimalFormat("0.##"); Calculator run = new Calculator(); run.enterItems(); //The total cost of all items is printed out with the proper formatting. if(run.count != 0) { System.out.println("The final total is " + fmt.format(run.calcTotal())); System.out.println("The total calculated recursivly is " + fmt.format(run.recTotal(run.count-1))); } else { System.out.println("No items entered."); run.enterItems(); } } //This method asks the user for input about the items they request, creates objects //of type General or Produce and stores their references in the Register array. public void enterItems() { Produce greens; General merchandise; int type, sale; double price, pound; System.out.println("Enter 1 for general merchandise and 2 for produce. Enter zero if you have no more items."); type = scan.nextInt(); //While statement allows user to stop entering items when they enter the sentinel, 0. while (type != 0) { //Both types of items require the percent off so this question is asked every time the //while loop executes. System.out.println("Is the item on sale? Please enter the percent off. If it is not " + "on sale please enter zero."); sale = scan.nextInt(); //This if-else statement allows the user to chose which type of //object they would like to create the asks them specific questions //to obtain information required for the constructor of an object of //that type. if(type == 1) { System.out.println("Now please enter the price of the item."); price = scan.nextDouble(); //General object is created and assigned to an a array reference variable of //type Register. Polymorphism makes this possible. items[count] = new General(price,sale); //The array index is incremented. count++; } else { System.out.println("Now please enter the price per pound of the item."); price = scan.nextDouble(); System.out.println("What is the item weight(lbs)?"); pound = scan.nextDouble(); //Produce object is created and assigned to an a array reference variable of //type Register. Polymorphism makes this possible. items[count] = new Produce(price, sale, pound); //The array index is incremented. count++; } System.out.println("Enter 1 for general merchandise and 2 for produce. Enter zero if you have no more items."); //Reassigns the variable "type" to be checked by the next iteration of the while loop //and the enclosed if-else statement. type = scan.nextInt(); } } //Runs through each item stored in the array and calls its corresponding total() //method. The value returned from each method call is added to the variable "total" //which represents the total cost of all items in the array. public double calcTotal() { double total = 0; for(int i = 0;i