import java.io.*;

public class Bank
{  
   public static void main(String[] args)
   {  new Bank();  }
   
   public Bank()
   {  int choice = 0;
      while (choice != 9)
      {
         output("\nChoose by typing a number:");
         output(" 1 : Make a new account");
         output(" 2 : Show an account");
         output(" ... ");
         output(" 9 : Quit");
         choice = inputInt();
         if (choice == 1) { makeAccount(); }
         if (choice == 2) { showAccount(); }
      }   
      System.exit(0);
   }

   public void makeAccount()
   {  String name = input("What is your name?");
      int account = inputInt("What is your account number(integer)?");
      int money   = inputInt("How much money do you want (integer)?");

      try
      {  RandomAccessFile data = new RandomAccessFile("bank.dat","rw");

         data.seek(20*account);
         data.writeUTF(name);

         data.seek(20*account+15);
         data.writeInt(money);         

         data.close();
      }
      catch(IOException ex)
      { output(ex.toString()); }         
   }
   
   public void showAccount()
   {
      int account = inputInt("What is your account number (integer)?");
  
      try
      {  RandomAccessFile data = new RandomAccessFile("bank.dat","rw");
         
         data.seek(account*20);
         String name = data.readUTF();
         
         data.seek(account*20+15);
         int money = data.readInt();
         
         System.out.println(name + " : " + money );         
         data.close();
      }
      catch(IOException ex)
      {  output(ex.toString()); }         
   }

/*******

The program above is a very simple PROTOTYPE of a bank-account database.
It works at a very simple level, but is prone to errors.

(1) Copy the code, run the program, and make an account for yourself
     by typing in your first name and choosing an account number.
     The account number must be an integer, something like 789.
     Put in a million for the amount of money.
   
    Then use option 2 (show account) to check whether you have the
     right amount of money in your account.

(2) Find out the LARGEST amount of money that you can p  ut into an account.
     What error occurs if the amount of money is too large?

(3) Put 100 Euros into account #1 - use the name "Phony".
    Now use a Hex Editor to look inside the file at the bytes in
     record #1. Explain the contents of ALL the bytes in this record.
   
(4) How long is the longest name you can type into this program?
     Why?  What error occurs if the name is too long?
    
(5) Put some money into account #123456.  Without looking at the file,
     PREDICT how long the file must be to contain this record.
     Then use explorer to check the length of the file.
     
(6) What happens if you use option 2 to show an account that does
     not contain a name or money (has not been created yet) ?
     
(7) What happens if you use option 2 to show an account with a
     REALLY BIG NUMBER (like 1,000,000,000)? Does an error occur?
     Explain the error message.
     
(8) Delete the data.  Now what happens if you try to use 2 to show
     the contents of an account when there is no data file?
     Explain the result.
     
(9) Put 100 Euros into account #1 again.  Now use the Hex Editor
     to DOUBLE the amount of money.  Then run the program again and
     show the amount in the account.  Once you have succeeded,
     put some random amount of money in the account, and use
     the Hex Editor to DOUBLE that amount.  You will need to
     think about BINARY to succeed.
     
(10) How do banks prevent hackers from doing what you did in #9
     with the account data files in the bank's computer?
     
(11) Change this to a GUI interface.

(12) Add another FIELD with a PIN (Personal Identification Number).
     The PIN is limited to 4 digits.
     
   
********/   
   
   //===========================================================
   //   IBIO Standard Input and Output
   //===========================================================
   
       static void output(String info)
      { System.out.println(info);   }
   
       static void output(char info)
      { System.out.println(info);   }
   
       static void output(byte info)
      { System.out.println(info);   }
   
       static void output(int info)
      { System.out.println(info);   }
   
       static void output(long info)
      { System.out.println(info);   }
   
       static void output(double info)
      { System.out.println(info);   }
   
       static void output(boolean info)
      { System.out.println(info);   }
   
       static String input(String prompt)
      {  String inputLine = "";
         System.out.print(prompt);
         try
         {inputLine = (new java.io.BufferedReader(
               new java.io.InputStreamReader(System.in))).readLine();
         }
             catch (Exception e)
            { String err = e.toString();
               System.out.println(err);
               inputLine = "";
            }
         return inputLine;
      }
   
       static String inputString(String prompt)
      {
         return input(prompt);   }
   
       static String input()
      {
         return input("");       }
   
       static int inputInt()
      {  
         return inputInt(""); }
   
       static double inputDouble()
      {
         return inputDouble(""); }
   
       static char inputChar(String prompt)
      {  char result=(char)0;
         try{result=input(prompt).charAt(0);}
             catch (Exception e){result = (char)0;}
         return result;
      }
   
       static byte inputByte(String prompt)
      {  byte result=0;
         try{result=Byte.valueOf(input(prompt).trim()).byteValue();}
             catch (Exception e){result = 0;}
         return result;
      }
   
       static int inputInt(String prompt)
      {  int result=0;
         try{result=Integer.valueOf(
               input(prompt).trim()).intValue();}
             catch (Exception e){result = 0;}
         return result;
      }
   
       static long inputLong(String prompt)
      {  long result=0;
         try{result=Long.valueOf(input(prompt).trim()).longValue();}
             catch (Exception e){result = 0;}
         return result;
      }
   
       static double inputDouble(String prompt)
      {  double result=0;
         try{result=Double.valueOf(
               input(prompt).trim()).doubleValue();}
             catch (Exception e){result = 0;}
         return result;
      }
   
       static boolean inputBoolean(String prompt)
      {  boolean result=false;
         try{result=Boolean.valueOf(
               input(prompt).trim()).booleanValue();}
             catch (Exception e){result = false;}
         return result;
      }
   //=========== end IBIO ===========================================//
}