/*  Craig Persiko - InflationBetter.java
    Sample Program using functions for CS 111A

    Computes inflation rate for an item
    over one year, using several functions to do so.
*/
import java.util.Scanner;

class InflationBetter
{
  public static void main(String args[])
  {
    double p1, p2;

    do
    {
      p1 = getValue("To determine the rate of inflation in the past year"
                    + " for your item,\nplease enter last year's price: $");

      p2 = getValue("Now please enter this year's price: $");

      dispInflation(p1, p2);

    } while (ask_run_again());
  }

  // Displays prompt to user, inputs a value, 
  // and ensures it's positive before returning it.
  static double getValue(String prompt)
  {
    Scanner keyIn = new Scanner(System.in);
    double val;

    do
    {
      System.out.print(prompt);
      val = keyIn.nextDouble();
      if (val <= 0)
        System.out.println("Please enter a positive number when asked");
    }while(val <= 0);

    return val;
  }

  // calculate and output the inflation rate
  static void dispInflation(double first_price, double second_price)
  {
    double inflation_rate;

    inflation_rate = inflation_calc(first_price, second_price);
    System.out.println("The inflation rate for your item from "
                       + "last year to this year is " + inflation_rate + "%");
  }

  // Return the inflation rate for an item whose price changed from
  // year_1_price to year_2_price in one year.  If year_2_price is less than
  // year_1_price, then returned rate will be negative.  year_1_price must
  // not be zero, or else a division by zero error will occur.
  static double inflation_calc(double year_1_price, double year_2_price)
  {
    // The result of the following calculation is
    // the function's return value.
    return ((year_2_price - year_1_price)/year_1_price) * 100;
  }

  // Ask the user if they want to try another item,
  // return true if they say "yes", false otherwise.
  static boolean ask_run_again()
  {
    Scanner sc = new Scanner(System.in);
    String run_again;
    System.out.print("Would you like to try another item? ");
    run_again = sc.nextLine();

    return Character.toUpperCase(run_again.charAt(0)) == 'Y';
  }
}

/* Sample Output:

To determine the rate of inflation in the past year for your item,
please enter last year's price: $100
Now please enter this year's price: $102.50
The inflation rate for your item from last year to this year is 2.5%
Would you like to try another item? y
To determine the rate of inflation in the past year for your item,
please enter last year's price: $-4
Please enter a positive number when asked
To determine the rate of inflation in the past year for your item,
please enter last year's price: $0
Please enter a positive number when asked
To determine the rate of inflation in the past year for your item,
please enter last year's price: $2.50
Now please enter this year's price $3.00
The inflation rate for your item from last year to this year is 20.0%
Would you like to try another item? n

*/



syntax highlighted by Code2HTML, v. 0.9