import java.util.Scanner; /** * Replace this JavaDoc comment for the purpose of this class */ public class Solution { /** * Gets data from standard input */ private Scanner input; /** * Start the execution of the solution * @param args Command line arguments */ public static void main(String args[]) { Solution solution = new Solution(); solution.run(); } /** * Run the solution. This method is called from main() */ public void run() { // Create object to read data from standard input input = new Scanner(System.in); // Set Delimiter so it reads every piece of data separately. Including month, day, year, storeName string, etc. input.useDelimiter("[\\t\\s/]"); // Create the three different instances for class Store // Create reference for Carrillo class. Additionaly, pass it it's name. Store carrillo = new Store("Carrillo"); // Create reference for Liberia class. Additionaly, pass it it's name. Store liberia = new Store("Liberia"); // Create reference for Sardinal class. Additionaly, pass it it's name. Store sardinal = new Store("Sardinal"); // Create a loop that reads sales data and interprets it. While this may look messy at first, it is secure and safe in the long run. // This loop repeats for every line that records a total sale for a store a specific day. while ( input.hasNextInt() ) { // Initialize variables int day = 0; int month = 0; int year = 0; String storeName = "CreationName"; double totalSales = 0; // If there's a next int assign it to day if ( input.hasNextInt() ) { day = input.nextInt(); // If there's a next int assign it to month if ( input.hasNextInt() ) { month = input.nextInt(); // If there's a next int assign it to year if ( input.hasNextInt() ) { year = input.nextInt(); // If there's a next string assign it to storeName if ( input.hasNext() ) { storeName = input.next(); // If there's a next double assign it to totalSales if ( input.hasNextDouble() ) { totalSales = input.nextDouble(); } } } } } // // // Now that all the info about a specific day is known, assign the total sales to the respective Store(instance) assignSales(storeName, month, totalSales, carrillo, liberia, sardinal); } //while // Print header printHeader(); // Print the information for each store // Print name, low season sales, and high season sales for sale Carrillo carrillo.printSales(); // Print name, low season sales, and high season sales for sale Liberia liberia.printSales(); // Print name, low season sales, and high season sales for sale Sardinal sardinal.printSales(); // Close the standard input input.close(); } /** * */ public void assignSales(String storeName, int month, double totalSales, Store carrillo, Store liberia, Store sardinal) { switch (storeName) { // if storeName = Carrillo, fill its respective object with the sales data case "Carrillo": carrillo.fillData(month, totalSales); break; case "Liberia": liberia.fillData(month, totalSales); break; case "Sardinal": sardinal.fillData(month, totalSales); break; default: System.err.println("ERROR: Invalid storeName"); break; } } /** * */ public void printHeader() { System.out.println("TIENDA BAJA ALTA"); System.out.println("=============== =============== ==============="); } } class Store { /** * */ private String storeName = "Invalid"; /** * */ private double lowSeasonSalesTotal = 0.0; /** * */ private double highSeasonSalesTotal = 0.0; /** * */ private long highSeasonDayEntries = 0; /** * */ private long lowSeasonDayEntries = 0; /** * Constructor */ public Store(String name) { storeName = name; } /** * */ public void fillData(int month, double totalDaySales) { //if month is high season, add totalDaySales to highSales if ( isHighSeason(month) ) { highSeasonSalesTotal += totalDaySales; highSeasonDayEntries++; } //if month is low season, add totalDaySales to lowSales else if ( !isHighSeason(month) ) { lowSeasonSalesTotal += totalDaySales; lowSeasonDayEntries++; } //else, month is not valid else { System.err.println("ERROR: Month is not bewteen 1 and 12"); } } /** * */ public boolean isHighSeason(int month) { return ( (month == 1) || (month == 2) || (month == 3) || (month == 4) || (month == 7) || (month == 12) ); } /** * */ public void printSales() { //print self name System.out.printf("%-15s ", storeName); // get low season sales // print low season sales if ( lowSeasonDayEntries != 0 ) { System.out.printf("%,15.2f ", (lowSeasonSalesTotal / (double)lowSeasonDayEntries) ); } else { System.out.printf("%,15.2f ", 0.00 ); } // get high season sales // print high season sales if ( highSeasonDayEntries != 0 ) { System.out.printf("%,15.2f%n", (highSeasonSalesTotal / (double)highSeasonDayEntries) ); } else { System.out.printf("%,15.2f%n", 0.00 ); } } }