/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package spr17apr18grocsortcorrectedc; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** * * @author jcmtiernan */ public class Spr17Apr18GrocSortCorrectedC { /** * @param args the command line arguments * * Assume we want to create a program that will read in a set * of prices, quantities, units, and items. These prices will represent * the costs of ONE unit of the specified item that could be purchased * in a grocery store. The quantities will represent the number of units * of each item that will be purchased. * * This program should have a loop to read values from a file. The file * will have four values on each line, a double for price, an integer * for quantity, a word for unit, and a phrase for item name. The * program should ask the user to enter the name of * the file, then determine if the file exists. If the file exists, then * read all the values until the end of file. If the file does not exist, * the program should ask the user to enter a price and * a quantity, then determine if there are more inputs from the user. * If the user enters the value -1 for the price, your loop test should * fail and the program should stop reading in data. * * On each loop you should do the following actions: Verify that the price and * the quantity are positive values. If they are valid, print the inputs. * Using the valid inputs, multiply the * price times the quantity and then add this product to a running sum in a * variable called groceryBill. A running sum is a value that is summed up * over multiple loops. The initial value for the running sum is 0 and then * each price * quantity is added to this sum in each loop. Also on each loop, * your program should count how many different prices are entered using a * variable called itemCount. Your program should also look for the highest * individual price (highestPrice) and the largest quantity (largestQuantity) * of any particular item. [The highest price and largest quantity are probably * not connected to the same item.] ****Keep track of the item name for the * highest price item and keep track of the item name and units for the * largest quantity item. * * When the loop test fails, we are going to ask the user if they have any coupons. * A coupon has to have a value and an expiration date. A valid coupon has to be * a ****value less than $5 and an expiration date equal or greater than today. * Check the coupon validity and if it is valid, subtract its value from the * groceryBill. Let the user enter multiple coupons, keep a count of the * valid coupons, and keep a running sum of the values of the valid coupons. * * After processing coupons, print out a message stating the number of * prices and quantities that were entered and print the sum groceryBill, * the amount saved with coupons, the number of coupons, * the itemCount, the highestPrice with ****item name and the largestQuantity * with item name and unit. Make sure to label the output so that the user * understands what the pieces of information mean. * */ public static void main(String[] args) { // Declarations //double price = 0; //the costs of items in a grocery store. //int quantity; // amounts of each item that will be purchased double groceryBill = 0; // a running sum of price times the quantity int itemCount; // how many different prices are entered double highestPrice = -99; // highest individual price double largestQuantity = -99; // largest single quantity //String unit = ""; //String name = ""; String largestQuantityUnit = ""; String largestQuantityName = ""; String highestPriceName = ""; int ROWMAX = 30; int COLMAX = 10; int PRICE = 0; // column 0 in grocNumbers will hold the price int QUANT = 1; // column 1 in grocNumbers will hold the quantity int M = 2; int T = 3; int W = 4; int R = 5; int F = 6; int S = 7; int N = 8; int ITEMCOST = 9; int UNIT = 0; // column 0 in grocWords will hold the unit int NAME = 1; // column 1 in grocWords will hold the name double[][] grocNumbers = new double[ROWMAX][COLMAX]; String[][] grocWords = new String[ROWMAX][COLMAX]; File inFile = new File("groceries.txt"); Scanner input = new Scanner(System.in); // connect to file Scanner inData = new Scanner(System.in); // for user input from kybd PrintWriter screen = new PrintWriter(System.out); PrintWriter output; String fileOut = "groceryOut1.txt"; System.out.print("Please enter the name for your output file (with no " + "spaces)\n or enter N if you wish to use the default " + "file name of "+fileOut+": "); String tempFile = inData.next(); inData.nextLine(); boolean inFileFound = false; try { input = new Scanner(inFile); //input = new Scanner(new File("groceries.txt")); inFileFound = true; } catch (FileNotFoundException fnf) { System.out.println("Input file does not exist. Enter data manually."); System.out.println("Please enter a price and a quantity of the item to buy:"); System.out.println("Enter -1 to stop:"); input = new Scanner(System.in); } try { if (tempFile.equalsIgnoreCase("N")) { output = new PrintWriter(fileOut); } else { output = new PrintWriter(tempFile); } } catch (FileNotFoundException fnf) { System.out.println("Output file does not exist."); output = new PrintWriter(System.out); } itemCount = 0; // ****Introduce program to user // Loop needed for input. // **** If the user enters the value -1 for // the price, your loop test should fail //while ((inFileFound && input.hasNextDouble()) || //((!inFileFound )&& (price != -1))) while (inFileFound && input.hasNextDouble()) { // ****The program should ask the user to enter a price // if no file exists //if (!inFileFound) //{ System.out.println("Please enter a price in dollars and cents: "); } grocNumbers[itemCount][PRICE] = input.nextDouble(); // Verify the price if (grocNumbers[itemCount][PRICE] > 0) // ****Check for -1 if no file exists { // ****The program should ask the user to enter a quantity // if no file exists //if (!inFileFound) //{ System.out.println("Please enter a quantity as a whole number: "); } grocNumbers[itemCount][QUANT] = input.nextInt(); // Verify the quantity if (grocNumbers[itemCount][QUANT] > 0) { //****read string data from input file if there is a file //if (inFileFound) //{ grocWords[itemCount][UNIT] = input.next(); for (int day = M; day <= N; day++) { grocNumbers[itemCount][day] = input.nextInt(); } grocWords[itemCount][NAME] = removeBlanks(input.nextLine()); //} //****print item name, price, quantity, unit to screen and // output file grocNumbers[itemCount][ITEMCOST] = grocNumbers[itemCount][PRICE] * grocNumbers[itemCount][QUANT]; groceryBill = grocNumbers[itemCount][ITEMCOST] + groceryBill; //****Keep track of the item name for the // highest price item and keep track of the item name and // units for the largest quantity item. if (highestPrice < grocNumbers[itemCount][PRICE]) { highestPrice = grocNumbers[itemCount][PRICE]; if (inFileFound) { highestPriceName = grocWords[itemCount][NAME]; } } if (largestQuantity < grocNumbers[itemCount][QUANT]) { largestQuantity = grocNumbers[itemCount][QUANT]; if (inFileFound) { largestQuantityUnit = grocWords[itemCount][UNIT]; largestQuantityName = grocWords[itemCount][NAME]; } } itemCount++; } else // (quantity > 0) is false { System.out.println("Valid quantities must be positive."); output.println("Valid quantities must be positive."); input.nextLine(); } } else // (price > 0) is false { System.out.println("Valid prices must be positive."); output.println("Valid prices must be positive."); input.nextLine(); } } // while (input.hasNextDouble()) // while (price != -1) screen.println(""); screen.println("After input from file with itemCount of "+itemCount+" :"); output.println("After input from file with itemCount of "+itemCount+" :"); /* printGrocArray(grocNumbers, grocWords, itemCount, output); printGrocArray(grocNumbers, grocWords, itemCount, screen); */ //System.out.println(""); System.out.println("\n\nAfter reading values from file"); for (int z = 0; z < itemCount; z++) { System.out.print(grocNumbers[z][PRICE]+" per unit for " +grocNumbers[z][QUANT]); System.out.print(" "+grocWords[z][UNIT]+" was " + grocNumbers[z][ITEMCOST] +" for "+grocWords[z][NAME]); System.out.printf("\nnumber purchased on Monday was "+grocNumbers[z][M]+","); System.out.printf(" on Tuesday was "+grocNumbers[z][T]+","); System.out.printf(" on Wednesday was "+grocNumbers[z][W]+","); System.out.printf(" on Thursday was "+grocNumbers[z][R]+","); System.out.printf(" on Friday was "+grocNumbers[z][F]+","); System.out.printf(" on Saturday was "+grocNumbers[z][S]+","); System.out.printf(" and on Sunday was "+grocNumbers[z][N]+".\n"); printDaily(grocNumbers[z], screen); System.out.println(); output.print(grocNumbers[z][PRICE]+" per unit for " +grocNumbers[z][QUANT]); output.print(" "+grocWords[z][UNIT]+" was " + grocNumbers[z][ITEMCOST] +" for "+grocWords[z][NAME]); printDaily(grocNumbers[z], output); output.println(); } System.out.println(""); // Sorting groceries amounts within the week boolean didSwap = false; int swapCount = COLMAX; int compares = N; double temp = 0; for (int iCount = 0; iCount < itemCount; iCount++) { //System.out.println("Row loop of "+iCount); swapCount = ROWMAX; compares = N+1; for(int j=0; (j 0); j++) { swapCount = 0; // one pass through for(int i = M; i < compares ; i++) { System.out.print("Comparing grocNumbers["+iCount+"]["+i+"] "+grocNumbers[iCount][i]+ " with grocNumbers["+iCount+"]["+(i+1)+"] "+grocNumbers[iCount][(i+1)]); if ( grocNumbers[iCount][i] < grocNumbers[iCount][i+1] ) { //swap temp = grocNumbers[iCount][i]; grocNumbers[iCount][i] = grocNumbers[iCount][i+1]; grocNumbers[iCount][i+1] = temp; swapCount++; didSwap = true; } System.out.println(" and "+(didSwap?"did" :"did not do" ) +" a swap."); didSwap = false; } compares--; //System.out.println(); System.out.println("After pass "+(j+1)+" array is: "); for(int k = M; k <= N; k++) { System.out.print( grocNumbers[iCount][k]+"\t"); } System.out.println(); System.out.println("SwapCount is "+swapCount); } //System.out.println(""); } System.out.println("\n\nAfter sorting numbers in each row"); output.println("\n\nAfter sorting numbers in each row"); /* printGrocArray(grocNumbers, grocWords, itemCount, output); printGrocArray(grocNumbers, grocWords, itemCount, screen); */ System.out.println(""); for (int z = 0; z < itemCount; z++) { System.out.print(grocNumbers[z][PRICE]+" per unit for " +grocNumbers[z][QUANT]); System.out.print(" "+grocWords[z][UNIT]+" was " + grocNumbers[z][ITEMCOST] +" for "+grocWords[z][NAME]); System.out.printf("\nnumber purchased on Monday was "+grocNumbers[z][M]+","); System.out.printf(" on Tuesday was "+grocNumbers[z][T]+","); System.out.printf(" on Wednesday was "+grocNumbers[z][W]+","); System.out.printf(" on Thursday was "+grocNumbers[z][R]+","); System.out.printf(" on Friday was "+grocNumbers[z][F]+","); System.out.printf(" on Saturday was "+grocNumbers[z][S]+","); System.out.printf(" and on Sunday was "+grocNumbers[z][N]+".\n"); printDaily(grocNumbers[z], screen); System.out.println(); output.print(grocNumbers[z][PRICE]+" per unit for " +grocNumbers[z][QUANT]); output.print(" "+grocWords[z][UNIT]+" was " + grocNumbers[z][ITEMCOST] +" for "+grocWords[z][NAME]); printDaily(grocNumbers[z], output); output.println(); } /* Make a method to swap ROWS based on item name String[] tempWords = new String[COLMAX]; double[] tempNumbers = new double[COLMAX]; tempWords = grocWords[iCount]; tempNumbers = grocNumbers[iCount]; */ //System.out.println("\n\n\n"); didSwap = false; swapCount = COLMAX; compares = ROWMAX; double[] tempNumbers = new double[COLMAX]; String[] tempWords = new String[COLMAX]; swapCount = ROWMAX; System.out.println("**Item count is **"+itemCount+"\n"); int iCount = 0; compares = itemCount; for(int m=0; (m 0); m++) { System.out.println("On pass "+m+" itemCount is "+itemCount+"swapCount is "+swapCount); swapCount = 0; // one pass through for(iCount = 0; iCount < compares-1 ; iCount++) { System.out.println("In compare loop iCount is "+iCount+" and compares is "+compares); screen.println(">>In compare loop iCount is "+iCount+" and compares is "+compares); System.out.println("Comparing grocWords["+iCount+"][NAME] "+grocWords[iCount][NAME]+ "\n with grocWords["+(iCount+1)+"][NAME] "+grocWords[iCount+1][NAME]); if ( grocWords[iCount][NAME].compareTo( grocWords[iCount+1][NAME]) > 0) { //swap tempWords = grocWords[iCount]; tempNumbers = grocNumbers[iCount]; grocWords[iCount] = grocWords[iCount + 1]; grocNumbers[iCount] = grocNumbers[iCount+1]; grocWords[iCount + 1] = tempWords; grocNumbers[iCount+1] = tempNumbers; //temp = grocNumbers[iCount][i]; //grocNumbers[iCount][i] = grocNumbers[iCount][i+1]; //grocNumbers[iCount][i+1] = temp; swapCount++; didSwap = true; } System.out.println(" and "+(didSwap?"did" :"did not do" ) +" a swap."); System.out.println("After - grocWords["+iCount+"][NAME] "+grocWords[iCount][NAME]+ "\n with grocWords["+(iCount+1)+"][NAME] "+grocWords[iCount+1][NAME]); didSwap = false; } compares--; System.out.println(); System.out.println("After pass "+(m)+" array is: "); //printGrocArray(grocNumbers, grocWords, // itemCount, screen); System.out.println(); System.out.println("SwapCount is "+swapCount); } screen.println("\n\n"); screen.println("After sorting by name:"); output.println("\nAfter sorting by name:"); /* printGrocArray(grocNumbers, grocWords, itemCount, output); printGrocArray(grocNumbers, grocWords, itemCount, screen); */ screen.println("\n\n"); for (int z = 0; z < itemCount; z++) { System.out.print(grocNumbers[z][PRICE]+" per unit for " +grocNumbers[z][QUANT]); System.out.print(" "+grocWords[z][UNIT]+" was " + grocNumbers[z][ITEMCOST] +" for "+grocWords[z][NAME]); System.out.printf("\nnumber purchased on Monday was "+grocNumbers[z][M]+","); System.out.printf(" on Tuesday was "+grocNumbers[z][T]+","); System.out.printf(" on Wednesday was "+grocNumbers[z][W]+","); System.out.printf(" on Thursday was "+grocNumbers[z][R]+","); System.out.printf(" on Friday was "+grocNumbers[z][F]+","); System.out.printf(" on Saturday was "+grocNumbers[z][S]+","); System.out.printf(" and on Sunday was "+grocNumbers[z][N]+".\n"); printDaily(grocNumbers[z], screen); System.out.println(); output.print(grocNumbers[z][PRICE]+" per unit for " +grocNumbers[z][QUANT]); output.print(" "+grocWords[z][UNIT]+" was " + grocNumbers[z][ITEMCOST] +" for "+grocWords[z][NAME]); printDaily(grocNumbers[z], output); output.println(); } // coupon section // define how to enter coupon info - date then amount // date should be entered - dd mm yyyy // Example for Feb 23, 2017 please enter 23 02 2017 // Then enter amount as dollars and cents dd.cc System.out.print("Do you have any coupons? Y/N : "); String coupYN = inData.next(); System.out.println(""); boolean fileFound = false; double couponAmt = 0; if ((coupYN.equalsIgnoreCase("Y")) || (coupYN.equalsIgnoreCase("Yes"))) { try { inFile = new File("couponData.txt"); input = new Scanner(inFile); fileFound = true; } catch (FileNotFoundException fnf) { System.out.println("No coupon file found. No coupons will be entered."); } int dd = 0, mm = 0, yyyy = 0; double coupValue = 0; int d = 28, m = 2, y = 2017; if (fileFound) // found a file called couponData.txt { while (input.hasNextInt()) { dd = input.nextInt(); mm = input.nextInt(); yyyy = input.nextInt(); coupValue = input.nextDouble(); // **** Call a method to check expiration and value couponAmt = couponCalc(yyyy, mm, dd, y, m, d, couponAmt, coupValue); } } } //****Make output method to use for screen and for output file outputMsg(couponAmt, groceryBill, itemCount, highestPrice, largestQuantity, inFileFound, highestPriceName, largestQuantityUnit, largestQuantityName, screen); outputMsg(couponAmt, groceryBill, itemCount, highestPrice, largestQuantity, inFileFound, highestPriceName, largestQuantityUnit, largestQuantityName, output); //output.println("Grocery bill is :"+groceryBill); output.close(); screen.close(); } // end main public static double couponCalc( int yyyy, int mm, int dd, int y, int m, int d, double couponAmt, double coupValue) { boolean couponGood = false; if (yyyy > y) { couponGood = true; } else if ((yyyy == y) && (mm > m)) { couponGood = true; } else if ((yyyy == y) && (mm == m) && (dd >= d)) { couponGood = true; } if (couponGood) { couponAmt = couponAmt + coupValue; } else { System.out.println("The coupon date (DMY) "+dd+"/"+mm+"/"+yyyy+" is invalid"); } return couponAmt; } public static double inputCheck(boolean test, String item, double tempDouble, double defDouble) { if (test) { System.out.println("This is an invalid "+item+"."); System.out.println("Default "+item+" of "+defDouble+" will be used."); return defDouble; } else { return tempDouble; } } public static String removeBlanks(String inString) { while (inString.charAt(0) == ' ') { inString = inString.substring(1); //System.out.println("Removing blank in name["+i+"]"); } return inString; } public static void printDaily(double[] grocNumbers, PrintWriter outputDevice) { outputDevice.println("printDaily called"); int M = 2; int T = 3; int W = 4; int R = 5; int F = 6; int S = 7; int N = 8; outputDevice.printf("\nnumber purchased on Monday was "+grocNumbers[M]+","); outputDevice.printf(" on Tuesday was "+grocNumbers[T]+","); outputDevice.printf(" on Wednesday was "+grocNumbers[W]+","); outputDevice.printf(" on Thursday was "+grocNumbers[R]+","); outputDevice.printf(" on Friday was "+grocNumbers[F]+","); outputDevice.printf(" on Saturday was "+grocNumbers[S]+","); outputDevice.printf(" and on Sunday was "+grocNumbers[T]+".\n"); } public static void outputMsg(double couponAmt, double groceryBill, int itemCount, double highestPrice, double largestQuantity, boolean inFileFound, String highestPriceName, String largestQuantityUnit, String largestQuantityName, PrintWriter outputDevice) { //****Make output method to use for screen and for output file outputDevice.println(); outputDevice.printf("Coupon savings is : %.2f \n\n",couponAmt); outputDevice.printf("Original Grocery bill is : %.2f \n",groceryBill); outputDevice.printf("Final Grocery bill is : %.2f \n\n",(groceryBill -= couponAmt)); outputDevice.println("Item count is "+itemCount); // ****Print Highest price and Largest quantity with items names/units outputDevice.print("Highest price is "+highestPrice); if (inFileFound) { outputDevice.print(" for item "+highestPriceName); } outputDevice.println(); outputDevice.printf("Largest quantity is %d",(int)largestQuantity); if (inFileFound) { outputDevice.print(" "+largestQuantityUnit+" of "+largestQuantityName); } outputDevice.println(""); } }