/* * TestBankExHand.java * * 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 NegativeAmount because it is in the same // package // don't have to import class Account because it is in the same package /** * * @author mike */ 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(); Account all[] = new Account[10]; // define array of 10 accounts // define array of 4 accounts, initializing accounts - can skip loop below then //Account all[] = { new Account("1234",0), new Account("2345",0), // new Account("3456",0), new Account("4567",0) }; // actually create the accounts // 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 Account(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 (NegativeAmount 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 (NegativeAmount 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 accounts System.out.println("All accounts: "); 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 accounts System.out.println("Sorted accounts: "); for (int cnt = 0; cnt < all.length; cnt++) { System.out.println(all[cnt].toString()); } //////// For showing dynamic allocation of arrays Account unknown[]; // unknown size int size = 5; // could be set by asking user, finding needed size in a file, or calculations unknown = new Account[size]; // actually create the array // actually create the accounts 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 Account(newPin,0); } // display the new accounts System.out.println("unknown accounts: "); for (int cnt = 0; cnt < unknown.length; cnt++) { System.out.println(unknown[cnt].toString()); } // try to go outside the bounds - crash //unknown[5] = new Account("12345","9999",0); // try to expand the array - crashs during display - because elements 0-14 were blown away //unknown = new Account[size+1]; //unknown[5] = new Account("12345","9999",0); // instead must create a new array, copy contents Account expanded [] = new Account[size+1]; expanded[5] = new Account("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 accounts System.out.println("original unknown accounts: "); for (int cnt = 0; cnt < unknown.length; cnt++) { System.out.println(unknown[cnt].toString()); } // display the new accounts System.out.println("expanded unknown accounts: "); for (int cnt = 0; cnt < expanded.length; cnt++) { System.out.println(expanded[cnt].toString()); } // change one of the accounts - 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 accounts System.out.println("expanded original unknown accounts: "); for (int cnt = 0; cnt < unknown.length; cnt++) { System.out.println(unknown[cnt].toString()); } // change one of the accounts - 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 Account myClone[] = (Account[]) unknown.clone(); System.out.println("clone of expanded unknown accounts: "); for (int cnt = 0; cnt < myClone.length; cnt++) { System.out.println(myClone[cnt]); } // change one of the accounts - 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]); Account cantClone = new Account("1234",100.00); System.out.println("cantClone: " + cantClone); Account cloned = new Account(); // dummy to make compile try { cloned = (Account) 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); } }