// Arup Guha
// 7/12/06
// Example for BHCSI Intro Java Course - Simulates a Bank Account
// allowing for deposits, withdrawals and balance inquiries.

// This example was edited in class to make use of static methods.
// An option was added to play blackjack for free!

import java.util.*;

public class bankblackjack {
	
	final static double INTEREST_RATE = 0.02;
	final static int QUIT = 6;

	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);

		// Get the starting balance.		
		System.out.println("Welcome to the Bank.");
		System.out.println("What is your starting balance?");
		double balance = stdin.nextDouble();
		
		// Print the menu and get the user's choice.
		bank.menu(); // menu();
		int choice = stdin.nextInt();
		
		// Continue processing choices until the user quits.
		while (choice != QUIT) {
			
			// Handle a deposit.
			if (choice == 1) {
				System.out.println("Enter the amount of your deposit.");
				int deposit = stdin.nextInt();
				balance = balance + deposit;
			}
			// Handle a withdrawal.
			else if (choice == 2) {
				System.out.println("Enter the amount of your withdrawal.");
				int withdrawal = stdin.nextInt();
				balance = balance - withdrawal;
			}
			// Print out the current balance.
			else if (choice == 3) {
				System.out.println("Your balance is $"+balance+".");
			}
			else if (choice == 4) {
				balance = balance + balance*INTEREST_RATE;
				//change the choice to 3 (print balance)
				choice = 3;
				//continue from the beginning of the while loop
				continue;
			}
			else if (choice == 5) {
				blackjack();
			}
			// Print out an error message for an invalid choice.
			else if (choice != QUIT) {
				System.out.println("Sorry that is not a valid choice.");
			}
			
			// Reprompt the menu.
			menu();
			choice = stdin.nextInt();
		}
		
		System.out.println("Thank you for using the bank!");
	}	
		
	// Prints out the menu of choices.
	public static void menu() {
		System.out.println("Here are your menu options:");
		System.out.println("1. Make a deposit.");
		System.out.println("2. Make a withdrawal.");
		System.out.println("3. Print your balance.");
		System.out.println("4. Accrue interest in your accout.");
		System.out.println("5. Play blackjack for free!");
		System.out.println("6. Quit.");
		System.out.println("Please enter the number of your choice.");
	}
	
	// Lets the user play a game of blackjack. To win, the user must
	// score in between 18 and 21 points inclusive. This game is 
	// simplified considerably compared to the real one.
	public static void blackjack() {
		
		Random r = new Random();
		Scanner stdin = new Scanner(System.in);
		
		// This is not 100% accurate, since it's more likely one will
		// get 10 points than anything else.
		int sum = r.nextInt(11) + 1 + r.nextInt(11) + 1;
		
		System.out.println("Your sum is now "+sum);
		if(sum >= 21)
			return;
		
		// Prompt for another card.
		System.out.println("Do you want another card? (Y=1, N=0)");
		int answer = stdin.nextInt();
		
		// Continue if they want another card and have a score less than 21.
		while (answer == 1 && sum < 21) {
			int card = r.nextInt(11) + 1;
			sum = sum + card;
			
			System.out.println("Your new sum is "+sum);
			if(sum >= 21)
				break;
					
			System.out.println("Do you want another card? (Y=1, N=0)");
			answer = stdin.nextInt();
				
		}
		
		// End result.
		if (sum > 17 && sum <= 21)
			System.out.println("You won!");
		else
			System.out.println("You lost.");
	}
}