/*
 * hw7#4
 * CS 381 homework 7 problem 4
 * Jacob Lundberg
 */

/*
 * Problem Four
 */

import java.io.*;
import java.util.*;
import java.lang.*;

// CALC may throw a BadArgsException for several reasons.
class BadArgsException extends Exception {

   BadArgsException(String s) {
      super(s);
   }

}

// This is just my cheater way of flow control in MAIN.
class ShortCircuit extends Exception {}

// This class provided for use in CS 381 [read: I didn't write it]
class IntegerReadin {
   // This stdin stream can be used to read input from the user
   public static BufferedReader stdin;

   // Static initalizer for stdin
   static {
      stdin = new BufferedReader (new InputStreamReader(System.in));
   }

   /////////////////////////////////////////////////////////////////////
   //
   // getInput
   //
   // This function  
   // reads a line hopefully containing an integer and returns the answer
   //
   public static int getInput() throws IOException, NumberFormatException {
      return Integer.parseInt (stdin.readLine());
   }
}

// Compute the consumer price of an item based on purchase quantity, wholesale price, and dealer markup.
class calc {

   double calc(int units, double price, double markup) throws BadArgsException, ArithmeticException {

      double total;

      // check the arguments for range validity, may throw BadArgsException
      if(units <= 0)    throw new BadArgsException("You must buy at least one item.");
      if(price <= 0.0)  throw new BadArgsException("The price must be larger than zero.");
      if(markup > 1.0)  throw new BadArgsException("The markup cannot be more than +1 (+100 percent).");
      if(markup < -1.0) throw new BadArgsException("The markup cannot be less than -1 (-100 percent).");

      // Compute the total.  May throw various ArithmeticException(s).
      total = (units * price) * (1 + markup);

      return(total);

   }

}

// My version of a MAIN that catches (hopefully) any exception that may get thrown when using CALC.
class main {

   // main()
   public static void main(String argv[]) {

      calc calculator = new calc();
      int units = 1, price = 0, markup = 0;
      double dprice, dmarkup;

      do {
         try {
            // read in units
            System.out.print("Enter number of units: ");
            units = IntegerReadin.getInput();

            // zero units means we're ready to terminate so we'll take a short-cut
            if(units == 0) throw new ShortCircuit();

            // read in price
            System.out.print("Enter price as an integer: ");
            price = IntegerReadin.getInput();

            // read in markup
            System.out.print("Enter markup percent as integer: ");
            markup = IntegerReadin.getInput();

            // acquire double versions of price and markup for clarity
            // using the integer copies of these numbers would not raise an exception
            dprice = (double)price;
            dmarkup = (double)markup / 100.0;

            // try to do the math on the numbers we've got.
            System.out.println("Total price: " + calculator.calc(units, dprice, dmarkup));
         }
         // fix the boo-boos.
         catch(BadArgsException e) {
            System.out.println("BadArgsException: " + e.getMessage());
         }
         catch(ArithmeticException e) {
            System.out.println("ArithmeticException: " + e.getMessage());
         }
         catch(IOException e) {
            System.out.println("The system encountered an error with the IO stream: " + e.getMessage());
         }
         catch(NumberFormatException e) {
            System.out.println("The input you gave was not a parseable number: " + e.getMessage());
         }
         catch(ShortCircuit e) {
            System.out.println("Exiting");
         }
         catch(Exception e) {
            System.out.println("Caught an unrecognized exception: " + e.getMessage());
         }
      } while(units != 0);

   }

}

