import java.util.Scanner; /** * Gives the statistics for the total earnings of Lily's stores */ public class Solution { /** * Gets data from standard input */ private Scanner input; // Creates the reference for the class Store private Store store = new Store(); /** * 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 this.input = new Scanner(System.in); // Space to store the average of the high season double highAverage = 0.0; // Space to store the average of the low season double lowAverage = 0.0; // Create the stores Carrillo, Liberia and Sardinal Store carrillo = new Store(); Store liberia = new Store(); Store sardinal = new Store(); // Start reading Lili's register store.readRegisters(input, carrillo, liberia, sardinal); // Print the header printHeader(); //Calculate the high season average of each store double carrilloHighAverage = calculateHighAverage(carrillo); double liberiaHighAverage = calculateHighAverage(liberia); double sardinalHighAverage = calculateHighAverage(sardinal); //Calculate the low season average of each store double carrilloLowAverage = calculateLowAverage(carrillo); double liberiaLowAverage = calculateLowAverage(liberia); double sardinalLowAverage = calculateLowAverage(sardinal); // Print the results with Carrillo System.out.printf("%-15s %,15.2f %,15.2f%n", "Carrillo", carrilloLowAverage, carrilloHighAverage); // Print the results with Liberia System.out.printf("%-15s %,15.2f %,15.2f%n", "Liberia", liberiaLowAverage, liberiaHighAverage); // Print the results with Sardinal System.out.printf("%-15s %,15.2f %,15.2f", "Sardinal", sardinalLowAverage, sardinalHighAverage); // Close the standard input input.close(); } /** * Prints the header with the store, low and high texts */ public void printHeader() { System.out.printf("%-15s %15s %15s%n", "TIENDA", "BAJA", "ALTA"); System.out.println("=============== =============== ==============="); } /** * Método para manejar los NaN/Espacios vacíos tomado de: * https://stackoverflow.com/questions/18220198/convert-float-nan-to-0 * @param another The read store that is going to be used for the averages calculation * @return Return 0 if there is no high season earnings, otherwise return the high season average earnings * */ public double calculateHighAverage(Store another) { // If there are high season values, calculate the average if (another.getHE() != 0.0 && another.getHC() != 0.0) { double average = another.getHE() / another.getHC(); return average; } else { return 0.0; } } /** * Método para manejar los NaN/Espacios vacíos tomado de: * https://stackoverflow.com/questions/18220198/convert-float-nan-to-0 * @param another The read store that is going to be used for the averages calculation * @return Return 0 if there is no low season earnings, otherwise return the low season average earnings */ public double calculateLowAverage(Store another) { // If there are low season values, calculate the average if (another.getLE() != 0.0 && another.getLC() != 0.0) { double average = another.getLE() / another.getLC(); return average; } else { return 0.0; } } } /** * Represents a Store with some statistics given from standard input * that let the program calculate the average of the earnings in a selected season */ /* public */ class Store { // Space to save the read day private double day = 0.0; // Space to save the read month private double month = 0.0; // Space to save the read year private double year = 0.0; // Space to save the read store private String store = null; // Space to store the earnings of the high season private double highEarnings = 0.0; // Space to store the earnings of the low season private double lowEarnings = 0.0; // Space to store the number of cashier closings of the high season private double highClosings = 0.0; // Space to store the number of cashier closings of the low season private double lowClosings = 0.0; /** * Gets a copy of the high season earnings * @return a copy of the high season earnings */ public double getHE() { return this.highEarnings; } /** * Gets a copy of the low season earnings * @return a copy of the low season earnings */ public double getLE() { return this.lowEarnings; } /** * Gets a copy of the high season cashier closings * @return a copy of the high season cashier closings */ public double getHC() { return this.highClosings; } /** * Gets a copy of the low season cashier closings * @return a copy of the low season cashier closings */ public double getLC() { return this.lowClosings; } /** * read the registers from standard input * @param input the input from the standard input * @param carrillo the store from Carrillo * @param liberia the store from Liberia * @param sardinal the store from Sardinal */ public void readRegisters(Scanner input, Store carrillo, Store liberia, Store sardinal) { // Use delimiters to read correctly the date and values input.useDelimiter("[/\\t\\s\\n]"); // Start reading the line and store the data while ( input.hasNext() ) { // If there is a blank line between the inputs, skip it from reading try { // Read all the data given in one line day = input.nextDouble(); month = input.nextDouble(); year = input.nextDouble(); store = input.next(); double earnings = input.nextDouble(); // When all the data is read, store it on the corresponding store storeData(day, month, year, store, earnings, carrillo, liberia, sardinal); } catch (java.util.InputMismatchException exception) { // Skip the blank line input.nextLine(); } } } /** * Stores the read data on the corresponding store, each reading process ends with a cashier closing, so add one when the earnings addition is done * @param day the read day * @param month the read month * @param year the read year * @param store the read store * @param earnings the read earnings * @param carrillo the store Carrillo * @param liberia the store from Liberia * @param sardinal the store from Sardinal */ public void storeData(double day, double month, double year, String store, double earnings, Store carrillo, Store liberia, Store sardinal) { if(store.equals("Carrillo")) { // if it is high season, add the read numbers to the total earnings and add one cashier closing if(month <= 4.0 || month == 7.0 || month == 12.0 ) { carrillo.highEarnings += earnings; carrillo.highClosings += 1.0; } // if it is low season, add the read numbers to the total earnings and add one cashier closing else { carrillo.lowEarnings += earnings; carrillo.lowClosings += 1.0; } } else if(store.equals("Liberia")) { // if it is high season, add the read numbers to the total earnings and add one cashier closing if(month <= 4.0 || month == 7.0 || month == 12.0 ) { liberia.highEarnings += earnings; liberia.highClosings += 1.0; } // if it is low season, add the read numbers to the total earnings and add one cashier closing else { liberia.lowEarnings += earnings; liberia.lowClosings += 1.0; } } else if(store.equals("Sardinal")) { // if it is high season, add the read numbers to the total earnings and add one cashier closing if(month <= 4.0 || month == 7.0 || month == 12.0 ) { sardinal.highEarnings += earnings; sardinal.highClosings += 1.0; } // if it is low season, add the read numbers to the total earnings and add one cashier closing else { sardinal.lowEarnings += earnings; sardinal.lowClosings += 1.0; } } } }