/* ------------------------------------
 * Author: Thao Vo
 * MIS 301 - Section 003
 * Programming Assignment #2
 * BankingSystem.java
 * ------------------------------------
 */
 
 
import java.io.*; 
import java.util.*;  
public class BankingSystem{  
 
 // -------------------------------
 // DEFINING INSTANCE VARIABLES
 // --------------------------------
 
private Account[] BankingSystem;
int index = 0;
 
// -------------------------------
// CALL FILES FROM FOLDER
// --------------------------------
 
        private String[] getAllFiles() {   
             
            List<String> results = new ArrayList<String>();  
            File folder = new File("data/");  
            File[] listOfFiles = folder.listFiles();    
             
            String filename, extension;           
            for (File file : listOfFiles) {             
                filename = file.getName();            
                extension = filename.substring(filename.lastIndexOf(".") + 1, 
                        filename.length());
                 
                if (file.isFile() && extension.equals("txt")) {             
                    results.add("data/" + filename);      
                }       
            }     
             
            return results.toArray(new String[results.size()]);  }    
     
      
// -------------------------------
// BANKING SYSTEM
// --------------------------------
 
public BankingSystem() {   
 
    BankingSystem = new Account [100];
    String[] fileNames = getAllFiles();        
    for (String filename: fileNames)  {        
        try {                
            BufferedReader reader = new BufferedReader( new FileReader(filename) );     
             
 
// ---------------------------------------------------------
// CREATE ACCOUNT OBJECT FROM READING 4 LINES
// Note: BufferedReader reader 
// use reader to call
// Need: account number, checking, saving, and the index
// --------------------------------------------------------
      String name = reader.readLine();
      int number = Integer.parseInt(reader.readLine());
      double checking_balance = Double.parseDouble(reader.readLine());
      double saving_balance = Double.parseDouble(reader.readLine());
      BankingSystem[index] = new Account(name,number,checking_balance,saving_balance);
      index++;
  } 
         
                      
        catch (IOException ex) {    
            ex.printStackTrace();   } 
}}
 
// ------------------------------------
// FIND ACCOUNT USING INFO PROVIDED
// ------------------------------------
 
    public int findAccount (String find_name, int find_account)
    {
        String name;
        int account;
         
        for (int position = 0 ; position < index; position++)
        {
            name = BankingSystem[position].getName();
            account = BankingSystem[position].getAccount();
            if (name.equals(find_name) && account == find_account)
            {
            return position;    
            }
             
        }
        return -1;
         
    }
     
 
 // ----------------------------------------
 // toString() METHOD TO CREAT REPORT
 // ----------------------------------------
     
     public String toString()
    
      {
       String summary;
        
 
     summary = "Number of Accounts: " + index + "\n";
 
    summary += "\n\nList of Accounts in System\n\n";
 
        for (int summary_position = 0; summary_position < index; summary_position++)
           {summary += BankingSystem[summary_position ].toString() + "\n";
            
         
           }
 
        return summary;
      }
      
 
     // -------------------------------------------------
     //
     // START TRANSACTIONS BY INTERACTING WITH USERS
     // 
     // -------------------------------------------------
                 
      
     public void transaction() {
      
  Scanner scan = new Scanner(System.in);
 
  System.out.print("Please enter your name: ");
  String name = scan.nextLine();
  System.out.print("Please enter your account number:  ");
  int account = scan.nextInt();
 
 
  int counter = findAccount(name, account);
  if (counter > 1)
  {
  BankingSystem[counter].getChecking();
  BankingSystem[counter].getSaving();
  
     System.out.print("\nWelcome "+name+"!");
     System.out.print(" Choose a transaction type: \n");
     System.out.print("Enter 1 for deposit, ");
     System.out.print("2 for withdraw, ");
     System.out.print("3 for transfer from the Checking account to the saving account, ");
     System.out.print("\n4 for transfer from the Savings to checking account, ");
     System.out.print("5 for display of account information:  ");
      
     // ----------------------------------------
     // CASE SWITCH FOR CUSTOMER TO CHOOSE
     // ----------------------------------------
                 
      
     int option = scan.nextInt();
      
      
     switch (option)
  {
     case 1:
         System.out.print("Enter a deposit amount ($xx.xx:): ");
         double user_input = scan.nextDouble();
         BankingSystem[counter].deposit(user_input);
         break;
     case 2:
         System.out.print("Enter a withdraw amount ($xx.xx:): ");
        user_input = scan.nextDouble();
         BankingSystem[counter].withdraw(user_input);
         break;
     case 3:
         System.out.print("Enter an amount to transfer from Checking to Savings account: ");
        user_input = scan.nextDouble();
         BankingSystem[counter].transferCheckingToSaving(user_input);
         break;
     case 4:
         System.out.print("Enter an amount to transfer from Savings to Checking account: ");
        user_input = scan.nextDouble();
         BankingSystem[counter].transferSavingToChecking(user_input);
         break;
     case 5:
         System.out.print(BankingSystem[counter].toString());
         break;     
          
// if those 5 cases aren't an option then default will run.
    default:
        System.out.println("Error. The account number you entered is not an option"); }
     System.out.println();
     System.out.println("After the transaction, your balance is: ");
     System.out.print(BankingSystem[counter].toString());
   
     try{
    BufferedWriter get = new BufferedWriter(new FileWriter("data/"+ account+ ".txt"));
     
    get.write(name);
    get.newLine();
    get.write(Integer.toString(account));
    get.newLine();
    get.write(Double.toString(BankingSystem[counter].getChecking()));
    get.newLine();
    get.write(Double.toString(BankingSystem[counter].getSaving()));
     
    get.close();
 
  }
 
  catch (IOException ex) {
  ex.printStackTrace(); }
  }
 
  else
  {
     System.out.println("There is no account associated with this name and account number. Enter 1 to try again!");
     int input1 = scan.nextInt();
     if (input1 == 1) {
     transaction();
     }
 
  }
     }
} //close class
 
 
     
