Hi,
I am new to programming. I was asked to make a program that uses a method to sum the grandtotal and a method for sorting.
So far the output of the program is perfect, however, i have not met the requirement of having a method sum the totals and a method to sort the collections. I have wrecked my brain trying to figure this out for two days. I will post my code and any help will be appreciated. By the way I have also tried building arrays to hold the (price*cases) amount, but there is no method to add a double to a regular array.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package farmersmarket2; import java.lang.reflect.Array; import java.util.Scanner; //uses scanner import java.text.NumberFormat;//needed to format currency import java.util.Locale; //used to get local format for currency import java.util.ArrayList;//to create arrays import java.util.Arrays;//to sort array import java.util.Collections;//sort arraylist sisnce arrays will not sort lists /** * * @author David */ public class FarmersMarket2 { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input = new Scanner(System.in); // new scanner named input NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US); ReceiptBuilder2 RB2; //Storage object //Declare Variables String Produce;//variable for produce name String produce;//store lower case name to change to upper String SKU ;//will hold SKU of 8 digits int cases; //will hold number of cases ordered double price;//price of produce boolean moreItems = true; //while control variable double ggtt = 0;//holds grandtotal //create Arrays ArrayList<String> receipt = new ArrayList<>();//10 element array //this is where i tried to create an array double[] runtotal while (moreItems){ //Ask for produce name System.out.println("Enter produce name to continue or" +"\"end\" to get total: "); produce = input.next();//set variable with name input Produce = produce.toUpperCase(); //test for name to change moreItems if (!Produce.equalsIgnoreCase("end")) { System.out.println(); //blank line for readability // Ask for 8 digit minimum SKU System.out.println("Please enter the 8 digit SKU number: ");//Get SKU SKU = input.next();//set variable SKU //validation of input while (SKU.length() != 8 ) // check lenght of SKU { System.out.println("SKU must be exactly 8 digits. Please re-enter: "); SKU = input.next(); } // end while System.out.println(); //Blank line for readibility //Ask for case quantity System.out.println("How many cases of this produce do you want? "); cases = input.nextInt();//set cases variable System.out.println();//blank line //Ask for produce price System.out.println("What is the price of each case? "); price = input.nextDouble();//set variable price //line break System.out.println("\n\n******************************************\n\n"); //change the ggtt variable to a running total ggtt = ggtt+(price*cases); //at this point i would need to add the total to the array //so far failed miserably //send all info to new constructor //new constructor with 4 variables passed to it RB2 = new ReceiptBuilder2(Produce,SKU,cases,price); receipt.add(RB2.toString());//add strings to the receipt array //Print receipt System.out.println("Thanks for shopping at the Farmer's Market!!\n"); System.out.print("Product: "+ RB2.getName()+"\n"); System.out.print("SKU number: "+RB2.getBarcode()+"\n"); System.out.print("Price per case: "+format.format(RB2.getCost())+"\n"); System.out.print("Cases ordered: "+RB2.getAmount()+"\n"); System.out.print("SubTotal price: "+format.format(RB2.getTotal()) +"\n"); //line break System.out.println("\n\n******************************************\n\n"); }//end if portion else//begin else portion of if { moreItems = false; System.out.println("\n\n******************************************\n\n" + "\n\nThanks for shopping at the Farmer's Market!!\n"); //display elements in array receipt System.out.println("Unsorted version of receipt"); for (String item : receipt) {//display items in array receipt System.out.printf("%s\n\n", item); }//end for try { System.out.print("Order Total: "+format.format(ggtt)); } catch (NullPointerException nullPointerException ) { System.out.print("You did not purchase any items.\nTotal: $0.00"); } //line break System.out.println("\n\n******************************************\n\n" + "\n\nThanks for shopping at the Farmer's Market!!\n"); }//end else area }//end while //method to sort arraylist Collections.sort(receipt);//method to sort list System.out.println("Sorted version of the reciept."); for (String temp2 : receipt) { System.out.print(temp2+"\n\n"); } System.out.print("Order Total: "+format.format(ggtt));//sho total again System.out.println("\n\n*************\n\n"); //here I would like to print out a test to be sure that the sum of the array //is correct before I alter my program. }//end main //I had created the method to sum the array here but it did not work either }//end class