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; /** * Reference to the class CalculateData */ private Report report; /** * 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); // Calls the method printHeader printHeader(); // Variables to store the sells in Carrillo double carrilloHighSells = 0.0; double carrilloLowSells = 0.0; // Variables to store the sells in Liberia double liberiaHighSells = 0.0; double liberiaLowSells = 0.0; // Variables to store the sells in Sardinal double sardinalHighSells = 0.0; double sardinalLowSells = 0.0; // Declares the counters for the amount of sells in each store /* TODAS ESTAS VARIABLES DE CONTADORES DEBERIAN TENER LOS NOMBRES CON CAMEL CASE. Es decir, "cLowSells" en vez de "clowsells" */ // Counters for carrillo store int chighsells = 0; int clowsells = 0; // Counters for liberia store int lhighsells = 0; int llowsells = 0; // Counters for sardinal store int shighsells = 0; int slowsells = 0; // Create object to manage the data that was received from the input report = new Report(0, 0, ""); // The cicle to repeat whilst there is still something in the input while ( input.hasNext() ) { // It receives the Date in the format dd/mm/yyyy String date = input.next(); // It splits the date by the '/' into three parts: day, month and year String[] parts = date.split("/"); // We take the second part of the array (the month) and asign it to month int month = Integer.parseInt(parts[1]); // Save the name of the store in a String String store = input.next(); // Save the amount of money gotten in the variable sells Double sells = input.nextDouble(); // Create object to manage the data that was received from the input report = new Report(month, sells, store); // isHighSeason gets to value returned by the method setHighSeason boolean isHighSeason = report.setHighSeason(month); /** * Combination of ifs and elses that determine the store from where the sells * come from. It adds the sells to each store, plus it augments the counter * by one unit in each case. */ if ( store.equals("Carrillo")) { if ( isHighSeason) { carrilloHighSells += sells; ++chighsells; } else { carrilloLowSells += sells; ++clowsells; } } else if ( store.equals("Liberia")) { if ( isHighSeason) { liberiaHighSells += sells; ++lhighsells; } else { liberiaLowSells += sells; ++llowsells; } } else { if ( isHighSeason) { sardinalHighSells += sells; ++shighsells; } else { sardinalLowSells += sells; ++slowsells; } } } // Determines the average for carillo high sells double cHighAverage = report.calculateAverage( chighsells, carrilloHighSells ); // Determines the average for carillo low sells double cLowAverage = report.calculateAverage( clowsells, carrilloLowSells ); // Determines the average for liberia high sells double lHighAverage = report.calculateAverage( lhighsells, liberiaHighSells ); // Determines the average for liberia low sells double lLowAverage = report.calculateAverage( llowsells, liberiaLowSells ); // Determines the average for sardinal high sells double sHighAverage = report.calculateAverage( shighsells, sardinalHighSells ); // Determines the average for sardinal low sells double sLowAverage = report.calculateAverage( slowsells, sardinalLowSells ); // Calls the method printBody from the printing class printBody(cHighAverage, cLowAverage, lHighAverage, lLowAverage, sHighAverage, sLowAverage ); // Close the standard input input.close(); } /** * Method printHeader() * Prints the header of the report. * It will always print " TIENDA BAJA ALTA" * and '=' symbols below. * return void */ public void printHeader() { // Prints the words "TIENDA BAJA ALTA" System.out.print("TIENDA BAJA ALTA\n"); // Prints the division between the header and the rest of the data System.out.print("=============== =============== ===============\n"); } /** * Method printBody() * Prints the data of the report. * It prints the name of the store, the amount of money it sold in * low season, and the amount of money it sold in high season. * It always prints the data with two decimal places. If the store * didn't sell anything in a season, it prints "0.00". */ public void printBody(double carrilloHigh, double carrilloLow, double liberiaHigh, double liberiaLow, double sarHigh, double sarLow) { // Prints the data of Carrillo System.out.printf("Carrillo %,15.2f %,15.2f\n", carrilloLow, carrilloHigh); // Prints the data of Liberia System.out.printf("Liberia %,15.2f %,15.2f\n", liberiaLow, liberiaHigh); // Prints the data of Sardinal System.out.printf("Sardinal %,15.2f %,15.2f\n", sarLow, sarHigh); } } /** * Class to manage the calculations of the report * it will have a method to determine the month of * the sells, and whether it was high or low season * it will have another method to make the calculations * based on that category. */ /* public */ class Report { /** * Variable to store if it is highSeason */ public boolean isHighSeason = false; /** * Variable to store the month of the sell */ private int month = 0; /** * Variable to store the name of the store */ private String store = ""; /** * Variable to store the amount of money of the store */ private double sells = 0.0; /** * Constructor of the class * @param month The month of the sell * @param sells The money amount of the sell * @param store The name of the Store */ public Report(int month, double sells, String store) { this.month = month; this.sells = sells; this.store = store; } /** * boolean method to determine if the sells were made * in the low or high season. * @param month The month when the sells were made * @return true if it is highSeason, false if not */ public boolean setHighSeason(int month) { // Determines if the month is between December and April if ( month == 12 || month == 1 || month == 2 || month == 3 || month == 4 || month == 7) { // Sets the variable season to 1, as it is indeed high season return true; } // If it enters the else, it means the month is between may and november, therefore, low season. else { // Sets the variable season to 2; as it is low season return false; } } /** * Method to calculate the average of sells per store * * @param counter Int to know the amount of sells made * @param value Double to know the amount of money made * * @return the average if counter !=0, return 0 otherwise */ public double calculateAverage(int counter, double value) { // variable to store the average double average = 0.0; // Determines if it is a valid division if (counter != 0) { // The average is the amount of total money divided by the counters. average = value / counter; } // Returns the average, of 0 if the division was invalid return average; } }