// Arup Guha
// 7/14/04 BHCSI Example: Introduction to Classes
// The following program defines a class to store a BankAcct object.
// A main is also defined to illustrate the use of the object. The
// overall purpose of the code is similar to a class example from 7/13.

import java.io.*;

public class BankAcct {

    private String name; // Stores bank account owner's name
    private double checking; // stores checking balance
    private double interestrate; // stores interest rate as a decimal
    
    // Creates default Bank Acount object - balances are set to 0, 
    // Interest rate set to 5%
    public BankAcct() {
	name = "JohnDoe";
	checking = 0;
	interestrate = 0.05;
    }
    
    // Creates Bank Account object with owner's name set to the parameter 
    // passed to the method. Other values are default values.
    public BankAcct(String n) {
	name = n;
	checking = 0;
	interestrate = 0.05;
    }
    
    // Creates Bank Account object based on parameters passed to the method.
    public BankAcct(String n, double check, double ir) {
	name = n;
	checking = check;
	interestrate = ir;
    }


    // Withdraws the amount of the parameter from checking account. Next three
    // methods work similarly, either withdrawing or depositing from the
    // checking or savings account.
    public void withdraw(double amount_sub) {
	checking = checking - amount_sub;
    }
    
    public void deposit(double amount_add) {
	checking = checking + amount_add;
    }

    // Return's money in checking account.
    public double getChecking() {
	return checking;
    }

    // Returns the interest rate
    public double getIrate() {
	return interestrate;
    }

    // Changes the interest rate to parameter passed to the method.
    public void changeRate(double rate) {
	interestrate = rate;
    }
    
    // Matures the account 1 year based on current checking and 
    //interest rate.
    public void matureAccount() {
	checking = checking*(1+interestrate);
    }

    // Matures the account num_years number of years.
    public void yearsPassed(int num_years) {

      for (int i=0; i<num_years; i++)
        matureAccount();
    }

    // Returns true if its okay to write a check of the amount passed in as a
    // parameter.
    public boolean noBounce(double check) {
	return (check <= checking);
    }

    // Prints all account information.
    public void printInfo() {
	System.out.println("Account Information");
	System.out.println("-------------------");
	System.out.println("Name : " + name);
	System.out.println("Checking Balance : $" + checking);
	System.out.println("Interest Rate : " + 100*interestrate + "%");
    }

    public String toString() {
	return "Name : " + name+"\nChecking Balance : $" + checking
	 + "\nInterest Rate : " + 100*interestrate + "%\n";
    }

    // Prints out a menu for the user.
    public static int menu() throws IOException {

      BufferedReader stdin = new BufferedReader
				(new InputStreamReader(System.in));

      // Print out the user's options.
      System.out.println("Here are your options:");
      System.out.println("1.Deposit money");
      System.out.println("2.Withdraw money");
      System.out.println("3.Print your current balance");
      System.out.println("4.Mature your account");
      System.out.println("5.Quit");

      // Read in their choice.
      int ans = Integer.parseInt(stdin.readLine());

      // If the choice isn't valid, the program defaults to quit.
      if (ans < 1 || ans > 5) 
        ans = 5;

      return ans; // Return their choice.
    }

    public static void main(String[] args) throws IOException {

      BufferedReader stdin = new BufferedReader
				(new InputStreamReader(System.in));

      // Get initial account information.
      System.out.println("What is your name?");
      String name = stdin.readLine();
      System.out.println("What is your initial account balance?");
      double cash = Double.parseDouble(stdin.readLine());

      // Set up the account.
      BankAcct useract = new BankAcct(name, cash, 0.05);

      // Continue until the user quits.
      int ans = menu();
      while (ans < 5) {
        
	// Take care of a deposit.
        if (ans == 1) {
          System.out.println("How much are you depositing?");
          double dep = Double.parseDouble(stdin.readLine());
          useract.deposit(dep);
        }

	// Take care of a withdrawal.
        if (ans == 2) {
          System.out.println("How much are you withdrawing?");
          double takeout = Double.parseDouble(stdin.readLine());

	  // Make sure the user has enough money before proceeding.
	  if (useract.noBounce(takeout))
            useract.withdraw(takeout);
          else
            System.out.println("Sorry, you don't have enough to do that.");

        }

	// Print the account information.
        if (ans == 3) {
          System.out.println(useract);
          //useract.printInfo();
        }

	// Mature the account one year.
        if (ans == 4) {
          System.out.println("How many years has the account matured?");
          int time = Integer.parseInt(stdin.readLine());
          useract.yearsPassed(time);
 	}
       
	// Reprompt the user for the next choice.
        ans = menu();
      }

      // Print out final account information.
      System.out.println("Thank you for banking with us today!");
      System.out.println("Here is your final account information.");
      useract.printInfo();

    }
}
