/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package spr17apr6; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Scanner; /** * * @author jcmtiernan */ public class Spr17Apr6 { /** * @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 = 0; // 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 = ""; /* double price1, price2, price3, price4, price5, price6, price7, price8; double quan1, quan2, quan3, quan4, quan5, quan6, quan7, quan8; String unit1, unit2, unit3, unit4, unit5, unit6, unit7, unit8; String name1, name2, name3, name4, name5, name6, name7, name8; */ int MAX = 100; double prices[] = new double[MAX]; double[] quantities = new double[MAX]; String[] units = new String[MAX]; String[] names = new String[MAX]; String[] ads = {"Big Sale!","Buy 1 Get 1 Free!", "10 for 10!"}; int employeeStarts[] = {800, 400, 1500, 1900}; /* price1 = price2 = price3 = price4 = price5 = price6 = price7 = price8 = 0; quan1 = quan2 = quan3 = quan4 = quan5 = quan6 = quan7 = quan8 = 0; unit1 = unit2 = unit3 = unit4 = unit5 = unit6 = unit7 = unit8 = ""; name1 = name2 = name3 = name4 = name5 = name6 = name7 = name8 = ""; */ for( int c = 0; c < MAX; c++) { units[c] = ""; names[c] = ""; } 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 )&& (prices[itemCount] != -1))) { // ****The program should ask the user to enter a price // if no file exists if ((!inFileFound) && (itemCount < MAX)) { System.out.println("Please enter a price in dollars and cents: "); } prices[itemCount] = input.nextDouble(); // Verify the price if (prices[itemCount] > 0) // ****Check for -1 if no file exists { // ****The program should ask the user to enter a quantity // if no file exists if ((!inFileFound) && (itemCount < MAX)) { System.out.println("Please enter a quantity as a whole number: "); } quantities[itemCount] = input.nextDouble(); // Verify the quantity if (quantities[itemCount] > 0) { //****read string data from input file if there is a file if ((inFileFound) && (itemCount < MAX)) { units[itemCount] = input.next(); names[itemCount] = removeBlanks(input.nextLine()); } groceryBill = prices[itemCount] * quantities[itemCount] + 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 < prices[itemCount]) { highestPrice = prices[itemCount]; if (inFileFound) { highestPriceName = names[itemCount]; } } if (largestQuantity < quantities[itemCount]) { largestQuantity = quantities[itemCount]; if (inFileFound) { largestQuantityUnit = units[itemCount]; largestQuantityName = names[itemCount]; } } /* if (itemCount < MAX) { prices[itemCount] = price; quantities[itemCount] = quantity; if (inFileFound) { units[itemCount] = new String(unit); names[itemCount] = new String(name); } } */ itemCount++; /* switch(itemCount) { case 1: price1 = price; quan1 = quantity; if (inFileFound) { unit1 = new String(unit); name1 = new String(name); } break; case 2: price2 = price; quan2 = quantity; if (inFileFound) { unit2 = new String(unit); name2 = new String(name); } break; case 3: price3 = price; quan3 = quantity; if (inFileFound) { unit3 = new String(unit); name3 = new String(name); } break; case 4: price4 = price; quan4 = quantity; if (inFileFound) { unit4 = new String(unit); name4 = new String(name); } break; case 5: price5 = price; quan5 = quantity; if (inFileFound) { unit5 = new String(unit); name5 = new String(name); } break; case 6: price6 = price; quan6 = quantity; if (inFileFound) { unit6 = new String(unit); name6 = new String(name); } break; case 7: price7 = price; quan7 = quantity; if (inFileFound) { unit7 = new String(unit); name7 = new String(name); } break; case 8: price8 = price; quan8 = quantity; if (inFileFound) { unit8 = new String(unit); name8 = new String(name); } break; default: break; } */ } else // (quantity > 0) is false { System.out.println("Valid quantities must be positive, not "+quantities[itemCount]+"."); output.println("Valid quantities must be positive."); input.nextLine(); } } else // (price > 0) is false { System.out.println("Valid prices must be positive, not "+prices[itemCount]+"."); output.println("Valid prices must be positive."); input.nextLine(); } } // while (input.hasNextDouble()) // while (price != -1) // 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 couponMAX = 5.00; double couponAmt = 0; int couponCount = 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; GregorianCalendar now = new GregorianCalendar(); int d = now.get(Calendar.DAY_OF_MONTH); int m = now.get(Calendar.MONTH); int y = now.get(Calendar.YEAR); 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 // check expiration date against current date d, m, y // and a valid coupon has to be a value less than $5 (max) couponAmt += couponCalc(yyyy, mm, dd, y, m, d, couponMAX, coupValue); } } } //****print item name, price, quantity, unit to screen and // output file System.out.println("\nThe "+itemCount+" groceries were:"); for(int i = 0; i < itemCount; i++) { printGrocery(output, prices[i], quantities[i], units[i], names[i], inFileFound); printGrocery(screen, prices[i], quantities[i], units[i], names[i], inFileFound); } /* printGrocery(output, price1, quan1, unit1, name1, inFileFound); printGrocery(screen, price1, quan1, unit1, name1, inFileFound); printGrocery(output, price2, quan2, unit2, name2, inFileFound); printGrocery(screen, price2, quan2, unit2, name2, inFileFound); printGrocery(output, price3, quan3, unit3, name3, inFileFound); printGrocery(screen, price3, quan3, unit3, name3, inFileFound); printGrocery(output, price4, quan4, unit4, name4, inFileFound); printGrocery(screen, price4, quan4, unit4, name4, inFileFound); printGrocery(output, price5, quan5, unit5, name5, inFileFound); printGrocery(screen, price5, quan5, unit5, name5, inFileFound); printGrocery(output, price6, quan6, unit6, name6, inFileFound); printGrocery(screen, price6, quan6, unit6, name6, inFileFound); printGrocery(output, price7, quan7, unit7, name7, inFileFound); printGrocery(screen, price7, quan7, unit7, name7, inFileFound); printGrocery(output, price8, quan8, unit8, name8, inFileFound); printGrocery(screen, price8, quan8, unit8, name8, inFileFound); */ //****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 couponMAX, 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) { if (coupValue > couponMAX) { System.out.println("The coupon value is too large"); coupValue = 0; } else if (coupValue < 0) { System.out.println("The coupon value is negative"); coupValue = 0; } } else { System.out.println("The coupon date (DMY) "+dd+"/"+mm+"/"+yyyy+" is invalid"); coupValue = 0; } return coupValue; } 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 printGrocery(PrintWriter output, double price, double quantity, String unit, String name, boolean inFileFound) { //****print item name, price, quantity, unit to screen and // output file output.print(price+" per unit for "+quantity); if (inFileFound) { output.print(" "+unit+" of "+name); } output.println(); } }