import java.util.Scanner; /** * Gets the data from the standard input and clasifies it into the corresponding store. * Calls the information from each store to print the results for low and high seasons on each store. */ 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); input.useDelimiter("[\\s/]+"); // Create references to the three stores to be used Store carrillo = new Store(); Store liberia = new Store(); Store sardinal = new Store(); // Reads all of the information provided on the standard input while ( input.hasNextInt() ) { // Gets the date from the standard input int day = input.nextInt(); int month = input.nextInt(); int year = input.nextInt(); // Gets the store name from the input String store = input.next(); // Gets the sold amount for that day double sales = input.nextDouble(); switch(store) { // Chooses the right store to add the sale to case "Carrillo": carrillo.addSale( month, sales); break; case "Liberia": liberia.addSale( month, sales); break; case "Sardinal": sardinal.addSale( month, sales); break; } } printTable(carrillo, liberia, sardinal); // Close the standard input input.close(); } /** * Prints the table with the sales for high season and low season on each of the stores. */ public void printTable(Store carrillo, Store liberia, Store sardinal) { // Prints the table header System.out.println("TIENDA BAJA ALTA"); System.out.println("=============== =============== ==============="); // Prints the stats for each store System.out.printf("Carrillo %,15.2f %,15.2f%n", carrillo.getLowSeason(), carrillo.getHighSeason()); System.out.printf("Liberia %,15.2f %,15.2f%n", liberia.getLowSeason(), liberia.getHighSeason()); System.out.printf("Sardinal %,15.2f %,15.2f%n", sardinal.getLowSeason(), sardinal.getHighSeason()); } } class Store { // The amount the store sold in low season private double lowSeasonSales = 0; // The amount the store sold in high season private double highSeasonSales =0; // How many sales are reported in low season private int lowSeasonChecks=0; // How many sales are reported in high season private int highSeasonChecks=0; public Store () { } /** * Adds the sales reported to the appropiate category, depending on the month where it occurred. */ public void addSale(int month, double sales) { // High season months if ( month <= 4 || month == 7 || month == 12) { this.highSeasonSales += sales; ++ this.highSeasonChecks; } // Low season months else { this.lowSeasonSales += sales; ++ this.lowSeasonChecks; } } /** * Determines if the store has had sales this season, and calculates the average sales for the month. * @return the average sales this store had during low season */ public double getLowSeason() { double averageSales=0; //Confirms the store had sales in the period to avoid divison by 0 if (this.lowSeasonChecks !=0) averageSales = lowSeasonSales/lowSeasonChecks; else averageSales = lowSeasonSales; return averageSales; } /** * Determines if the store has had sales this season, and calculates the average sales for the month. * @return the average sales this store had during low season */ public double getHighSeason() { double averageSales=0; //Confirms the store had sales in the period to avoid divison by 0 if (this.highSeasonChecks !=0) averageSales = highSeasonSales/highSeasonChecks; else averageSales = highSeasonSales; return averageSales; } }