import java.text.*;
import java.util.*;

public class TradingGame {

  static final int NUMBER_OF_DAYS = 10;
  static final double BASE_PRICE = 10;
  static final double VARIATION = 5;
  static final double INITIAL_CASH = 100;
  static double cash = INITIAL_CASH;
  static int appleInventory = 0;
  static int pearInventory = 0;
  static double applePrice, pearPrice;

  public static void main(String[] args) {
    for (int day = 1; day <= NUMBER_OF_DAYS; day++) {
      applePrice = computePrice(BASE_PRICE, VARIATION);
      pearPrice = computePrice(BASE_PRICE, VARIATION);
      System.out.println("Day: " + day + " out of 10");
      int choice;
      do {
        printMenu();
        choice = getChoice();
        switch (choice) {
          case 1: // Print cash balance and inventory
            System.out.println("Cash: " + currencyFormatter(cash));
            System.out.println("Apple inventory: " + appleInventory);
            System.out.println("Pair inventory: " + pearInventory);
            break;
          case 2: //Print today's prices
            System.out.println("Price of apples is: " + currencyFormatter(applePrice));
            System.out.println("Price of pears is: " + currencyFormatter(pearPrice));
            break;
          case 3: { //Buy apples
            int amount = getQuantity("apples", "buy");
            if (!buyApples(amount)) {
              System.out.println("You don't have enough money");
            }
            break;
          }
          case 4: { // Sell apples
            int amount = getQuantity("apples", "sell");
            if (!sellApples(amount)) {
              System.out.println("You don't have enough apples");
            }
            break;
          }
          case 5: { // Buy Pears
            int amount = getQuantity("pairs", "buy");
            if (!buyPears(amount)) {
              System.out.println("You don't have enough money");
            }
            break;
          }
          case 6: {  // Sell Pears
            int amount = getQuantity("pairs", "sell");
            if (!sellPears(amount)) {
              System.out.println("You don't have enough pears");
            }
            break;
          }
        }
      } while (choice != 7);

    }
    System.out.println("You finished with: " + currencyFormatter(cash));
  }

  public static double computePrice(double basePrice, double variation) {
    double result = basePrice;
    if (Math.random() > 0.5) {
      result += Math.random() * variation;
    } else {
      result -= Math.random() * variation;
    }
    return result;
  }

  public static String currencyFormatter(double amount) {
    DecimalFormat myFormatter = new DecimalFormat("$###,###.00");
    return myFormatter.format(amount);

  }

  public static int getQuantity(String product, String action) {
    System.out.println("How many " + product + " do you want to " + action + "? ");
    Scanner keyboard = new Scanner(System.in);
    return keyboard.nextInt();
  }

  public static boolean sellApples(int amount) {
    if (amount > appleInventory) {
      return false;
    }
    cash += amount * applePrice;
    appleInventory -= amount;
    return true;
  }

  public static boolean sellPears(int amount) {
    if (amount > pearInventory) {
      return false;
    }
    cash += amount * pearPrice;
    pearInventory -= amount;
    return true;
  }

  public static boolean buyApples(int amount) {
    if (amount * applePrice < cash) {
      cash -= amount * applePrice;
      appleInventory += amount;
      return true;
    }
    return false;
  }

  public static boolean buyPears(int amount) {
    if (amount * pearPrice < cash) {
      cash -= amount * pearPrice;
      pearInventory += amount;
      return true;
    }
    return false;
  }

  public static void printMenu() {
    System.out.println("1. Print cash balance and inventory");
    System.out.println("2. Print today's prices");
    System.out.println("3. Buy apples");
    System.out.println("4. Sell apples");
    System.out.println("5. Buy pears");
    System.out.println("6. Sell pears");
    System.out.println("7. I am done for today");
  }

  public static int getChoice() {

    Scanner keyboard = new Scanner(System.in);
    int choice;
    do {
      System.out.print("Your choice: ");
      choice = keyboard.nextInt();
    } while (choice > 7 || choice < 1);
    return choice;
  }
}