// Bank structure: import java.lang.*; import java.io.*; import edu.neu.ccs.demeter.dj.*; Bank = List(BusinessUnit). BusinessUnit = Index Country Location List(Party). Party : Individual | CorporateBody common Agreement PartyId. Individual = "individual" FirstName LastName Birthdate. CorporateBody = "corporate" CompanyName Rating. Agreement = List(Contract) List(Rule) Day. FirstName = String. LastName = String. CompanyName = String. PartyId = int. Rating : C0 | C1 | C9 | D0 | D1 | D3 | D4. Birthdate = "birthdate" Month Day Year. Contract = Location List(Portfolio). Rule = boolean. Portfolio = List(Account). Account = List(Posting) AccountID AccountType. Posting : Credit | Debit common Amount. AccountID = int. Amount = int. Credit = "credit". Debit = "debit". AccountType : Savings | Checking | Overdraft | CD | Loan. Savings = "savings". Checking = "checking". Overdraft = "overdraft". CD = "cd". Loan = "loan". Index = int. Location = String. Country = String. Age = int. Date = Month Day Year. Month = int. Day = int. Year = int. C0 = "c0". C1 = "c1". C9 = "c9". D0 = "d0". D1 = "d1". D3 = "d3". D4 = "d4". List(S) ~ "(" {S} ")". Main = String. Agreement { {{ private static int QUIT = 7; }} public void modifyAgreement(){{ /* Modifies this agreement */ int choice = 0; while (choice != QUIT){ try{ showMenu(); choice = Main.getChoice(); doMenuChoice(choice); } catch(Exception e){ System.out.println("Error in agreement: " + e.toString()); e.printStackTrace(); } } }} private void showMenu(){{ /* This function just displays a menu */ System.out.println(Main.divider); System.out.println("Modify agreement:"); System.out.println("1. Add a contract"); System.out.println("2. Change a contract"); System.out.println("3. View all contracts"); System.out.println("4. View rules"); System.out.println("5. Change a rule"); System.out.println("6. Change statement ending day"); System.out.println("7. Quit"); }} private void doMenuChoice(int choice) {{ if (choice == 1) addContract(); else if (choice == 2) changeContract(); //else if (choice == 3) showAllContracts(); else if (choice != QUIT) System.out.println("Invalid choice"); }} private void addContract(){{ /* adds a new contract to this agreement */ Contract_List addToMe = get_contract_list(); addToMe.addElement(Contract.promptContract(true, null)); }} private void changeContract(){{ /* asks the user for a contract to change and changes it */ Contract changeMe = Contract.promptContract(false, this); changeMe.modify(); }} public Contract findContract(Location here, boolean display){{ /* returns the contract in this agreement at location here, shows the contract if display is true */ Contract currContract; //current Contract in the enumeration boolean found = false; //flag to know when to stop looking java.util.Enumeration en = get_contract_list().elements(); //search contract list while (en.hasMoreElements() && !found) { currContract = (Contract) en.nextElement(); found = currContract.get_location().get_string() == here.get_string(); } if (!found) System.out.println("Contract at " + here.get_string() + " could not be found"); else if (display) currContract.print(); return currContract; }} }//end Agreement Contract { public void print() to * (PrintVisitor); public static Contract promptContract(boolean newContract, Agreement agreement){{ /* returns a contract. if newContract is true, a new contract is created by the user. Otherwise an existing one is returned agreement is used to find an existing contract */ Location newLocation; //location of this contract Contract returnContract; newLocation = Location.promptLocation(); if (newContract) { Contract returnContract = new Contract(); returnContract = new Contract(newLocation, new Portfolio_List()); } else returnContract = agreement.findContract(newLocation, true); return returnContract; }} public void modify(){{ /* modifies this contract with user input */ }} }//end Contract Bank { //void print() to * (PrintVisitor(new PrintWriter (new FileOutputStream("program.input")))); void print() to * (PrintVisitor); void display() to * (DisplayVisitor); public void newBusinessUnit(){{ /* This function will add a new business unit to the bank. The country and location will be specified by the user and the index will be generated */ Index index; //the index of the new unit Country country; //the country of the new unit Location location; //the location of the new unit BusinessUnit newBU; //the unit created that will be added to the bank try { location = Location.promptLocation(); country = Country.promptCountry(); //generate index if (get_businessunit_list() == null) index = new Index(1); else index = new Index(get_businessunit_list().size() + 1); //add the new business unit newBU = new BusinessUnit(index, country, location, new Party_List()); businessunit_list.addElement(newBU); //inform the user that add was successful System.out.println("Business unit "); newBU.printLocation(); System.out.println("has been added"); } catch (Exception e) { System.out.print ("Error in addBusinessUnit: " + e.toString());} }} public void addNewParty() {{ /* This function will add a new party to a business unit specified by the user. The party information will also be specified by the user. */ Party newParty; //the party we are going to add int index; //index of the business unit where the party will go BusinessUnit_List bul = get_businessunit_list(); System.out.println("Enter the id of the unit you would like to add to"); index = Main.getChoice(); if (bul.get_businessunit(index) != null){ newParty = Party.promptParty(true); if (Main.findParty(newParty.get_partyid(), true) ==null) bul.addParty(index, newParty); else System.out.println("Customer already exists with that id"); } else { System.out.println("invalid index ");} }} public void lookupParty() {{ /* This function will display a party */ PartyId displayMeId; Party displayMe; displayMeId = PartyId.promptPartyId("id number: "); displayMe = Main.findParty(displayMeId, true); }} public void modifyParty() {{ /* This function will allow the user to modify customer information */ Party changeMe; //the party that needs to be modified PartyId changeMeId; //id of the party we will modify changeMeId = PartyId.promptPartyId("id number: "); changeMe = Main.findParty(changeMeId, true); try { Individual dummy = (Individual) changeMe; dummy.modify(); } catch (ClassCastException e) { CorporateBody dummy = (CorporateBody) changeMe; dummy.modify(); } }} public void showAllUnits(){{ /* This function will display all of the banking units */ get_businessunit_list().printLocations(); }} }//end Bank BusinessUnit{ void printLocation() bypassing Party_List to * (PrintVisitor); void print() to * (PrintVisitor); public void addParty(Party addParty){{ /* This function will add addParty to this object's party list */ get_party_list().addElement(addParty); }} } //end Business Unit BusinessUnit_List{ public BusinessUnit get_businessunit(int index){{ /* This function will return the business unit with the given index or null if it does not exist */ BusinessUnit curr; //the current Business unit in the enumeration java.util.Enumeration en = this.elements(); //loop through until the unit at index is found, or return null while (en.hasMoreElements()){ curr = (BusinessUnit) en.nextElement(); if (index == curr.get_index().get_index()) return curr; } return null; }} public void addParty(int index, Party addParty){{ /* This function will add newParty to the BusinessUnit at index */ if (addParty != null) { get_businessunit(index).addParty(addParty); System.out.println("party added"); } }} void printLocations() {{ BusinessUnit tempUnit; java.util.Enumeration en = this.elements(); System.out.println(Main.divider); while (en.hasMoreElements()){ tempUnit = (BusinessUnit) en.nextElement(); tempUnit.printLocation(); } }} }//end Business Unit List Location{ void print() to * (PrintVisitor); public static Location promptLocation(){{ /* Returns a new location given by the user */ Location returnLoc = new Location(); System.out.println("Enter location:"); try{returnLoc.set_string(Main.stdin.readLine());} catch (IOException e) {System.out.println("Error reading location");} return returnLoc; }} } //end Location Country{ void print() to * (PrintVisitor); public static Country promptCountry(){{ /* Returns a new country given by the user */ Country returnCountry = new Country(); System.out.println("Enter country:"); try{returnCountry.set_string(Main.stdin.readLine());} catch (IOException e) {System.out.println("Error reading country");} return returnCountry; }} } //end Country Index{ void print() to * (PrintVisitor); } //end Index CorporateBody { {{ private static int QUIT = 5; }} public static CorporateBody promptCorporateBody(boolean newBody){{ /* This function will return a Corporate Body holding information specified by the user.If newBody is true, a new CorporateBody will be returned. Otherwise, an existing one will be returned */ CorporateBody returnMe; PartyId id; //tax id number CompanyName name; //corporation name Rating rating; //corporation risk rating //get the tax id number id = PartyId.promptPartyId("tax id number: "); //if the id number is not good, abort if (id.get_partyId() == 0) return null; //otherwise continue gathering necessary information if (newBody) { name = CompanyName.promptCompanyName(); rating = Rating.promptRating(); returnMe = new CorporateBody(new Agreement(new Contract_List(), new Rule_List(), new Day()),id, name, rating); } else { returnMe = (CorporateBody) Main.findParty(id, false); } return returnMe; }} public void modify(){{ /* This function will allow the user to modify any information about this corporate body */ int choice = 0; while (choice != QUIT){ try{ showMenu(); choice = Main.getChoice(); doMenuChoice(choice); } catch(Exception e){ System.out.println("Error in corporate body: " + e.toString()); e.printStackTrace(); } } }} private void showMenu(){{ /* This function just displays a menu */ System.out.println(Main.divider); System.out.println("Modify corporate body:"); System.out.println("1. Change company name"); System.out.println("2. Change company rating"); System.out.println("3. Modify agreement"); System.out.println("5. Quit"); }} private void doMenuChoice(int choice) {{ if (choice == 1) changeName(); else if (choice == 2) changeRating(); else if (choice == 3) changeAgreement(); else if (choice != QUIT) System.out.println("Invalid choice"); }} private void changeName(){{ /* This function will modify the name of this corporate body */ set_companyname(CompanyName.promptCompanyName()); }} private void changeRating() {{ /* Modifies the rating of this corporate body */ set_rating(Rating.promptRating()); }} } //end CorporateBody CompanyName{ public static CompanyName promptCompanyName(){{ /* This function will return a company name entered by the user */ CompanyName returnName = new CompanyName(); System.out.println("Enter corporate name:"); try{returnName.set_string(Main.stdin.readLine());} catch (IOException e) {System.out.println("Error reading corporate name");} return returnName; }} } Rating { public static Rating promptRating(){{ /* This function will return a rating entered by the user */ Rating returnRating = null; String tempRating = ""; //needed to figure out what subclass of Rating System.out.println("Enter corporate rating: [C0][C1][C9][D0][D1][D3][D4]"); try{tempRating = Main.stdin.readLine();} catch (IOException e) {System.out.println("Error reading corporate rating");} if (tempRating == "C0") returnRating = new C0(); else if (tempRating == "C1") returnRating = new C1(); else if (tempRating == "C9") returnRating = new C9(); else if (tempRating == "D0") returnRating = new D0(); else if (tempRating == "D1") returnRating = new D1(); else if (tempRating == "D3") returnRating = new D3(); else if (tempRating == "D4") returnRating = new D4(); else { System.out.println("invalid rating entered, rating will be set to C0"); returnRating = new C0(); } return returnRating; }} } Individual { {{ private static int QUIT = 5; }} public static Individual promptIndividual(boolean newIndividual){{ /* This function will return an individual holding information specified by the user.If newIndividual is true, a new Individual will be created and returned. Otherwise, an existing one will be returned */ Individual returnIndividual;//individual to be returned PartyId id; //social security number FirstName first; //individual first name LastName last; //individual last name Birthdate birthdate; //individual birthdate //get social security number id = PartyId.promptPartyId("social security number:"); //if id is not good, abort if (id.get_partyId() == 0) return null; //otherwise continue gathering necessary information if (newIndividual) { first = FirstName.promptFirstName(); last = LastName.promptLastName(); birthdate = Birthdate.promptBirthdate(); returnIndividual = new Individual(new Agreement(new Contract_List(), new Rule_List(), new Day()), id, first, last, birthdate); } else { returnIndividual = (Individual) Main.findParty(id, false); } return returnIndividual; }} public void modify(){{ /* This function will modify an individual */ int choice = 0; while (choice != QUIT){ try{ showMenu(); choice = Main.getChoice(); doMenuChoice(choice); } catch(Exception e){ System.out.println("Error in corporate body: " + e.toString()); e.printStackTrace(); } } }} private void showMenu() {{ /* This function just displays a menu */ System.out.println(Main.divider); System.out.println("Modify individual:"); System.out.println("1. Change first name"); System.out.println("2. Change last name"); System.out.println("3. Change birthdate"); System.out.println("4. Modify agreement"); System.out.println("5. Quit"); }} private void doMenuChoice(int choice) {{ if (choice == 1) changeFirstName(); else if (choice == 2) changeLastName(); else if (choice == 3) changeBirthdate(); else if (choice == 4) changeAgreement(); else if (choice != QUIT) System.out.println("Invalid choice"); }} private void changeFirstName(){{ /* Modifies this individual's first name */ set_firstname(FirstName.promptFirstName()); }} private void changeLastName(){{ /* Modifies this individual's last name */ set_lastname(LastName.promptLastName()); }} private void changeBirthdate(){{ /* Modifies this individual's birthdate */ set_birthdate(Birthdate.promptBirthdate()); }} } //end Individual FirstName { public static FirstName promptFirstName(){{ /* This function will return a name given by the user */ FirstName returnName = new FirstName(); System.out.println("Enter individual first name:"); try{returnName.set_string(Main.stdin.readLine());} catch (IOException e) {System.out.println("Error reading individual first name");} return returnName; }} }//end FirstName LastName { public static LastName promptLastName() {{ /* This function will prompt the user for a last name and return it */ LastName returnName = new LastName(); System.out.println("Enter individual last name:"); try{returnName.set_string(Main.stdin.readLine());} catch (IOException e) {System.out.println("Error reading individual last name");} return returnName; }} }//end LastName Party { void print() to * (PrintVisitor); private static char whatKind(){{ /* This function will return a string specifying what kind of party the user wants return char is guaranteed to be i c or q */ char option = ' '; //dummy string holds user menu choice //find out what kind of party (individual or corporate) while (option != 'i' && option != 'c' && option != 'q'){ System.out.println("Is this an individual or a corporate account? (i/c/q to quit)"); try{ String temp = Main.stdin.readLine(); option = temp.charAt(0); } catch (IOException e) { System.out.println("Error reading selection in whatKind of Party"); option = 'q'; } } return option; }} public static Party promptParty(boolean newParty){{ /* This function will return a party that is built based on user input. If newParty is true a new Party will be created and returned. Otherwise, an existing party will be returned */ Party returnParty; //party to be returned char partyType; //char represents type of party we are adding partyType = whatKind(); if (partyType == 'i') returnParty = Individual.promptIndividual(newParty); else if (partyType == 'c') returnParty = CorporateBody.promptCorporateBody(newParty); else returnParty = null; return returnParty; }} protected void changeAgreement(){{ /* Modifies this party's agreement */ get_agreement().modifyAgreement(); }} }//end Party PartyId { public static PartyId promptPartyId(String idType){{ /* This function will return a partyId given by the user */ PartyId returnId = new PartyId(); Integer tempId = new Integer(0); System.out.println("Enter " + idType); try{tempId = new Integer(Main.stdin.readLine());} catch (Exception e) {System.out.println("Error reading id number");} returnId.set_partyId(tempId.intValue()); return returnId; }} } // end PartyId PrintVisitor { public void after (BusinessUnit host) {{out.println();}} public void after (Party host) {{out.println();}} }//end Print Visitor Birthdate { public static Birthdate promptBirthdate() {{ /* This function will get a new birthdate from the user */ Month month; Day day; Year year; Birthdate returnBDay; System.out.println("Please enter customer birthdate"); month = Month.promptMonth(); day = Day.promptDay(); year = Year.promptYear(); returnBDay = new Birthdate(month, day, year); return returnBDay; }} }//end Birthdate Month { public static Month promptMonth() {{ /* This function will get a new month from the user */ Integer tempMonth; //user input boolean valid = false; Month returnMonth = new Month(); while (!valid){ System.out.println("Enter month:"); try{ tempMonth = new Integer(Main.stdin.readLine()); valid = (tempMonth.intValue() > 0 && tempMonth.intValue() < 13); returnMonth.set_month(tempMonth.intValue()); } catch (Exception e) { System.out.println("Error reading month"); } } return returnMonth; }} }//end Month Day { public static Day promptDay() {{ /* This function will get a new day from the user */ Integer tempDay; //user input Day returnDay = new Day(); boolean valid = false; while (!valid){ System.out.println("Enter day:"); try{ tempDay = new Integer(Main.stdin.readLine()); valid = (tempDay.intValue() > 0 && tempDay.intValue() < 32); returnDay.set_day(tempDay.intValue()); } catch (Exception e) {System.out.println("Error reading day");} } return returnDay; }} }//end Day Year { public static Year promptYear() {{ /* This function will get a new day from the user */ Integer tempYear; //user input Year returnYear = new Year(); boolean valid = false; while (!valid){ System.out.println("Enter year:"); try{ tempYear = new Integer(Main.stdin.readLine()); valid = (tempYear.intValue() > 1800 && tempYear.intValue() < 3000); returnYear.set_year(tempYear.intValue()); } catch (Exception e) {System.out.println("Error reading year");} } return returnYear; }} }//end Year Main { {{ //constants private static int QUIT = 10; private static String FILE_IN = "program.input"; private static String FILE_OUT = "program.output"; public static String divider = "------------------------------------------------"; //private members private static Bank b; //the one bank instance private static FileInputStream fis; //file to read in bank private static FileOutputStream fos; //file to read bank out to private static ClassGraph cg; //classgraph of the whole program private static PrintVisitor pv; //print visitor to output the bank at the end //public members public static BufferedReader stdin; //used by all classes to get user input }} public static void main(String args[]) throws Exception {{ /* This program is designed to be used by a bank. It holds functionality needed to run a bank with several branches */ //set up members fis = new FileInputStream(FILE_IN); b = Bank.parse(fis); cg = new ClassGraph(true, false); fos = new FileOutputStream(FILE_OUT); pv = new PrintVisitor(new PrintWriter(fos)); stdin = new BufferedReader(new InputStreamReader(System.in)); int choice = 0; while (choice != QUIT){ try{ showMenu(); choice = getChoice(); doMenuChoice(choice); } catch(Exception e){ System.out.println("Error in main: " + e.toString()); e.printStackTrace(); } } b.print(); //cg.traverse((Object) b, "to *", (Visitor) pv); //writeOutputToFile(); //b.display(); }} private static void showMenu(){{ /* This function just displays a menu */ System.out.println(divider); System.out.println("Select a menu option:"); System.out.println("1. Add a new customer"); System.out.println("2. Lookup a customer"); System.out.println("3. Modify customer information"); System.out.println("4. Add a new banking unit"); System.out.println("5. View all business units"); System.out.println("6. Customer transaction"); System.out.println("10.Exit"); }} public static int getChoice(){{ /* This function will be used by all classes to get a menu choice */ Integer choice = new Integer(0); try{choice = new Integer(stdin.readLine());} catch (Exception e){System.out.println("Please enter a number");} return choice.intValue(); }} private static void doMenuChoice(int choice) throws IOException{{ if (choice == 1) b.addNewParty(); else if (choice == 2) b.lookupParty(); else if (choice == 3) b.modifyParty(); else if (choice == 4) b.newBusinessUnit(); else if (choice == 5) b.showAllUnits(); else if (choice == 6) b.modifyParty(); else if (choice != QUIT) System.out.println("Invalid choice"); }} public static Party findParty(PartyId findMeId, boolean display){{ /* This function will return a party based on ID only. If display is true, the party will also be displayed to the screen */ Party currP = null; //current party in the enumeration BusinessUnit currBU; //current Business Unit in the enumeration boolean found = false; //flag to know when to stop looking java.util.Enumeration p_en; //party enumeration java.util.Enumeration bul_en = b.get_businessunit_list().elements(); //search business units while (bul_en.hasMoreElements() && !found) { currBU = (BusinessUnit) bul_en.nextElement(); p_en = currBU.get_party_list().elements(); //search parties while (p_en.hasMoreElements() && !found) { currP = (Party) p_en.nextElement(); found = currP.get_partyid().get_partyId() == findMeId.get_partyId(); } } if (!found) System.out.println("Party " + findMeId.get_partyId() + " could not be found"); else if (display) currP.print(); return currP; }} }//end Main