{ Arup Guha 11/10/99 Program Description: This program allows up to 10 people to open bank accounts and then deposit and withdraw money from their account. They are allowed negative balances, and relatively little error checking is done because this program is an example of records. } program Bank; type Entry = record {Keeps information for one bank customer } last : string; first : string; balance : real; end; Entry_Array = array[0..9] of Entry; {Stores info for 10 bank customers. } Bank_Record = record {Used to keep track of all bank customers. } people : Entry_Array; num_people : integer; end; {Prints out general menu.} procedure Menu; begin writeln('Welcome to Pascal''s Bank. Here are your options:'); writeln('1. Open an Account'); writeln('2. Deposit Money'); writeln('3. Withdraw Money'); writeln('4. Check Balance.'); writeln('5. Quit'); end; {Sets up uninitialized Bank_Record variable.} procedure Initialize(var bank : Bank_Record); begin bank.num_people := 0; end; {Given a first and last name, and a Bank_Record, this function returns true if the person is already stored in the Bank_Record, and false otherwise. } function CheckAccount(fname, lname : string;var bank : Bank_Record): boolean; var index : integer; found : boolean; begin found := false; for index := 0 to bank.num_people-1 do begin if ((fname = bank.people[index].first) AND (lname = bank.people[index].last)) then found := true; end; CheckAccount := found; end; {This function can only be called with the first and last name of an existing bank customer stored in the Bank_Record bank. It returns the place in the array (inside of bank) that this customer's information is stored. } function WhichMember(fname, lname : string;var bank : Bank_Record): integer; var index : integer; begin for index := 0 to bank.num_people-1 do begin if ((fname = bank.people[index].first) AND (lname = bank.people[index].last)) then WhichMember := index; end; end; {Creates a new account and adds it into the appropriate place, checking to see if the person already has an account or if the bank is full. In these cases an error message is printed. } procedure NewAccount(var bank: Bank_Record); var first, last: string; begin writeln('What is your first name?'); readln(first); writeln('What is your last name?'); readln(last); if (NOT (CheckAccount(first, last, bank))) then begin if (bank.num_people < 10) then begin writeln('Great, we have just added your account with a balance of $0.'); bank.people[bank.num_people].first := first; bank.people[bank.num_people].last := last; bank.people[bank.num_people].balance := 0; bank.num_people := bank.num_people + 1; end else writeln('Our bank can not take any more customers at this time.'); end else writeln('Sorry, you already have an account with us.'); end; {Executes a deposit.} procedure Deposit(var bank : Bank_Record); var first, last : string; place : integer; dep : real; begin writeln('What is your first name?'); readln(first); writeln('What is your last name?'); readln(last); if (CheckAccount(first, last, bank)) then begin place := WhichMember(first, last, bank); writeln('How much would you like to deposit?'); readln(dep); bank.people[place].balance := bank.people[place].balance + dep; end else writeln('Sorry you do not have an account with us.'); end; {Executes a withdraw.} procedure Withdraw(var bank : Bank_Record); var first, last : string; place : integer; withdraw : real; begin writeln('What is your first name?'); readln(first); writeln('What is your last name?'); readln(last); if (CheckAccount(first, last, bank)) then begin place := WhichMember(first, last, bank); writeln('How much would you like to withdraw?'); readln(withdraw); bank.people[place].balance := bank.people[place].balance - withdraw; end else writeln('Sorry you do not have an account with us.'); end; {Prints the balance requested for.} procedure PrintBalance(var bank : Bank_Record); var first, last : string; place : integer; begin writeln('What is your first name?'); readln(first); writeln('What is your last name?'); readln(last); if (CheckAccount(first, last, bank)) then begin place := WhichMember(first, last, bank); write('Your current balance is $'); writeln(bank.people[place].balance:0:2); end else writeln('Sorry you do not have an account with us.'); end; var ans : integer; all : Bank_Record; begin Initialize(all); Menu; readln(ans); while (ans <> 5) do begin if (ans = 1) then NewAccount(all) else if (ans = 2) then Deposit(all) else if (ans = 3) then Withdraw(all) else if (ans = 4) then PrintBalance(all) else if (ans <> 5) then writeln('Sorry that is not a valid menu choice. Please try again.'); Menu; readln(ans); end; end.