/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package spr17apr18grocsortcorrectedb; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** * * @author jcmtiernan */ public class Spr17Apr18GrocSortCorrectedB { /** * @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 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 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); 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()) { grocNumbers[itemCount][PRICE] = input.nextDouble(); // Verify the price if (grocNumbers[itemCount][PRICE] > 0) { grocNumbers[itemCount][QUANT] = input.nextInt(); // Verify the quantity if (grocNumbers[itemCount][QUANT] > 0) { grocWords[itemCount][UNIT] = input.next(); for (int day = M; day <= N; day++) { grocNumbers[itemCount][day] = input.nextInt(); } grocWords[itemCount][NAME] = removeBlanks(input.nextLine()); 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(""); //****print item name, price, quantity, unit to screen and // output file screen.println("\nAfter input from file with itemCount of "+itemCount+" :"); output.println("\nAfter input from file with itemCount of "+itemCount+" :"); printGrocArray(grocNumbers, grocWords, itemCount, output); printGrocArray(grocNumbers, grocWords, itemCount, screen); // 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++) { swapCount = ROWMAX; compares = N; for(int j=0; (j 0); j++) { swapCount = 0; // one pass through for(int i = M; i < compares ; i++) { 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++; } } compares--; } } screen.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(""); /* 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]; */ didSwap = false; swapCount = COLMAX; compares = ROWMAX; double[] tempNumbers = new double[COLMAX]; String[] tempWords = new String[COLMAX]; swapCount = ROWMAX; int iCount = 0; compares = itemCount; for(int m=0; (m 0); m++) { swapCount = 0; // one pass through for(iCount = 0; iCount < compares-1 ; iCount++) { 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; swapCount++; } } compares--; } 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"); // 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.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 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(""); } 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 %.0f,",grocNumbers[M]); outputDevice.printf(" on Tuesday was %.0f,",grocNumbers[T]); outputDevice.printf(" Wednesday %.0f,",grocNumbers[W]); outputDevice.printf(" Thurs. %.0f,",grocNumbers[R]); outputDevice.printf(" Fri. %.0f,",grocNumbers[F]); outputDevice.printf(" Sat. %.0f,",grocNumbers[S]); outputDevice.printf(" and Sun. %.0f.\n",grocNumbers[N]); } public static void printGrocArray(double[][] grocNumbers, String[][] grocWords, int itemCount, PrintWriter outputDevice) { //System.out.println("In printGrocArray method"); 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 for (int z = 0; z < itemCount; z++) { //System.out.println("In loop"); //outputDevice.println("In loop"); outputDevice.printf("%.2f per unit for %.0f",grocNumbers[z][PRICE], +grocNumbers[z][QUANT]); outputDevice.printf(" %s was %.2f for %s",grocWords[z][UNIT], grocNumbers[z][ITEMCOST], grocWords[z][NAME]); printDaily(grocNumbers[z], outputDevice); outputDevice.println(); } } }