/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package spr17mar7; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** * * @author jcmtiernan */ public class Spr17Mar7 { /** * @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 = ""; 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))) { // ****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: "); } price = input.nextDouble(); // Verify the price if (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: "); } quantity = input.nextInt(); // Verify the quantity if (quantity > 0) { //****read string data from input file if there is a file if (inFileFound) { unit = input.next(); name = removeBlanks(input.nextLine()); } //****print item name, price, quantity, unit to screen and // output file System.out.print(price+" per unit for "+quantity); if (inFileFound) { System.out.print(" "+unit+" of "+name); } System.out.println(); output.print(price+" per unit for "+quantity); if (inFileFound) { output.print(" "+unit+" of "+name); } output.println(); groceryBill = price * quantity + 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 < price) { highestPrice = price; if (inFileFound) { highestPriceName = name; } } if (largestQuantity < quantity) { largestQuantity = quantity; if (inFileFound) { largestQuantityUnit = unit; largestQuantityName = name; } } itemCount++; } else // (quantity > 0) is false { System.out.println("Valid prices and quantities must be positive."); output.println("Valid prices and quantities must be positive."); input.nextLine(); } } else // (price > 0) is false { System.out.println("Valid prices and quantities must be positive."); output.println("Valid prices and quantities 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 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 // check expiration date against current date d, m, y // check coupon value using inputCheck // A valid coupon has to be a value less than $5 // Rewrite date check using new algorithm // lgorithm for expiration /* If yyyy > current year, Coupon is good If yyyy == current year and mm > current month, Coupon is good If yyyy == current year and mm == current month, and dd >= current day Coupon is good */ couponAmt = couponCalc(yyyy, mm, dd, y, m, d, couponAmt, 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"); } */ /* if (yyyy >= y) // y is OK { if (mm >= m) { if (dd >= d) { couponAmt = couponAmt + coupValue; } else if (((dd < d) && (yyyy > y)) || ((dd < d) && (mm > m))) { couponAmt = couponAmt + coupValue; } else { System.out.println("The coupon date (DMY) "+dd+"/"+mm+"/"+yyyy+" is invalid"); } } else if ((mm < m) && (yyyy > y)) // valid date { //couponAmt += coupValue; couponAmt = couponAmt + coupValue; } else { System.out.println("The coupon date (DMY) "+dd+"/"+mm+"/"+yyyy+" is invalid"); } } else { System.out.println("The coupon date (DMY) "+dd+"/"+mm+"/"+yyyy+" is invalid"); } */ } } } //****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); /* System.out.println("Coupon savings is :"+couponAmt); System.out.println("Original Grocery bill is :"+groceryBill); System.out.println("Final Grocery bill is :"+(groceryBill -= couponAmt)); System.out.println("Item count is "+itemCount); // ****Print Highest price and Largest quantity with items names/units System.out.print("Highest price is "+highestPrice); if (inFileFound) { System.out.print(" for item "+highestPriceName); } System.out.println(); System.out.print("Largest quantity is "+largestQuantity); if (inFileFound) { System.out.print(" "+largestQuantityUnit+" of "+largestQuantityName); } System.out.println(""); */ //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 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(""); } }