/* * TestBankExHand.java * * Created on February 10, 2005, 5:39 PM * Created on August 21, 2002, 4:09 PM */ package bank; // import IDs.Id; import SortSearchEtc.Sorts; import java.util.Random; // don't have to import exception class NegativeAmountException because it is in the same // package // don't have to import class BankAccount because it is in the same package /** * * @author redmond */ public class TestBankExHand { /** Creates a new instance of TestBankExHand */ public TestBankExHand() { } /** * @param args the command line arguments */ public static void main(String[] args) { Random generator = new Random(); BankAccount all[] = new BankAccount[10]; // define array of 10 BankAccounts // define array of 4 BankAccounts, initializing BankAccounts - can skip loop below then //BankAccount all[] = { new BankAccount("1234",0), new BankAccount("2345",0), // new BankAccount("3456",0), new BankAccount("4567",0) }; // actually create the BankAccounts // Significant Java difference - the array itself is just a reference really for (int cnt = 0; cnt < all.length; cnt++) { // assign a pin randomly - generated random integer and convert to string int rand = generator.nextInt(10000); String newPin = Integer.toString(rand); all[cnt] = new BankAccount(newPin,0); } boolean ok; double newBal = 0; ok = all[0].deposit(50); if (! ok) { System.out.println(" My bad #1"); } ok = all[1].deposit(-30); if (! ok) { System.out.println(" My bad #2"); } ///////////////////////////////////////////////// // exception handling example - don't show until cover ///////////////////////////////////////////////// try { newBal = all[2].depositEx(80); System.out.println("New Balance: " + newBal); } catch (NegativeAmountException except) { System.out.println(" My bad #3"); System.out.println(except.getMessage()); System.out.println("value: " + except.getAmount()); // In Java, we could actually use private instance variable amount because we'returnin the same package except.printStackTrace(); // shows whose calling caused problem // if this amount was a problem due to bad user input we could reprompt } try { newBal = all[3].depositEx(-60); System.out.println("New Balance: " + newBal); } catch (NegativeAmountException except) { System.out.println(" My bad #4"); System.out.println(except.getMessage()); System.out.println("value: " + except.getAmount()); //except.printStackTrace(); // shows whose calling caused problem // useful for debugging when caused by program error // (Not nice for users to see) // if this amount was a problem due to bad user input we could reprompt } // display the BankAccounts System.out.println("All BankAccounts: "); for (int cnt = 0; cnt < all.length; cnt++) { System.out.println(all[cnt].toString()); } ///////// Don't do the below (or at least don't show how it works) until we cover interfaces // try sorting array Sorts.insertSort(all); // display the BankAccounts System.out.println("Sorted BankAccounts: "); for (int cnt = 0; cnt < all.length; cnt++) { System.out.println(all[cnt].toString()); } //////// For showing dynamic allocation of arrays BankAccount unknown[]; // unknown size int size = 5; // could be set by asking user, finding needed size in a file, or calculations unknown = new BankAccount[size]; // actually create the array // actually create the BankAccounts inside the array // Significant Java difference - the array itself is just a reference really for (int cnt = 0; cnt < unknown.length; cnt++) { // assign a pin randomly - generated random integer and convert to string int rand = generator.nextInt(10000); String newPin = Integer.toString(rand); unknown[cnt] = new BankAccount(newPin,0); } // display the new BankAccounts System.out.println("unknown BankAccounts: "); for (int cnt = 0; cnt < unknown.length; cnt++) { System.out.println(unknown[cnt].toString()); } // try to go outside the bounds - crash //unknown[5] = new BankAccount("12345","9999",0); // try to expand the array - crashs during display - because elements 0-14 were blown away //unknown = new BankAccount[size+1]; //unknown[5] = new BankAccount("12345","9999",0); // instead must create a new array, copy contents BankAccount expanded [] = new BankAccount[size+1]; expanded[5] = new BankAccount("12345","9999",0); // copy //for (int cnt = 0; cnt < unknown.length; cnt++) { // expanded[cnt] = unknown[cnt]; //} System.arraycopy(unknown,0,expanded,0,unknown.length); // display the BankAccounts System.out.println("original unknown BankAccounts: "); for (int cnt = 0; cnt < unknown.length; cnt++) { System.out.println(unknown[cnt].toString()); } // display the new BankAccounts System.out.println("expanded unknown BankAccounts: "); for (int cnt = 0; cnt < expanded.length; cnt++) { System.out.println(expanded[cnt].toString()); } // change one of the BankAccounts - even though unknown points to a // different array than expanded, // a change in one is a change in both // this is true whether expanded is filled using arraycopy or by element // by element assignments - since element assignments are assigning // references - so actually referring to the same elements, not copies unknown[0].deposit(200); System.out.println("unknown: " + unknown[0]); System.out.println("expanded: " + expanded[0]); // presumably you want this back in the same variable so code can go on unknown = expanded; // now points to the same array // display the BankAccounts System.out.println("expanded original unknown BankAccounts: "); for (int cnt = 0; cnt < unknown.length; cnt++) { System.out.println(unknown[cnt].toString()); } // change one of the BankAccounts - since unknown points to the same array // as expanded - a change in one is a change in both // this is true whether expanded is filled using arraycopy or by element // by element assignments - since element assignments are assigning // references - so actually referring to the same elements, not copies unknown[0].deposit(200); System.out.println("unknown: " + unknown[0]); System.out.println("expanded: " + expanded[0]); // make a copy - but make it a clone BankAccount myClone[] = (BankAccount[]) unknown.clone(); System.out.println("clone of expanded unknown BankAccounts: "); for (int cnt = 0; cnt < myClone.length; cnt++) { System.out.println(myClone[cnt]); } // change one of the BankAccounts - since unknown points to the same array // as expanded - a change in one is a change in both // this is true whether expanded is filled using arraycopy or by element // by element assignments - since element assignments are assigning // references - so actually referring to the same elements, not copies myClone[0].deposit(200); System.out.println("unknown: " + unknown[0]); System.out.println("myClone: " + myClone[0]); BankAccount cantClone = new BankAccount("1234",100.00); System.out.println("cantClone: " + cantClone); BankAccount cloned = new BankAccount(); // dummy to make compile /* try { //cloned = (BankAccount) cantClone.clone(); System.out.println("cloned: " + cloned); } catch (CloneNotSupportedException ex) { System.out.println("You can't clone this guy"); System.exit(0); } cloned.setAcctNum("1234567"); System.out.println("cantClone: " + cantClone); System.out.println("cloned: " + cloned); **/ System.exit(0); } }