From 2da65ceefaf84437d21e7e4a5697d57af02501d3 Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 26 May 2011 17:44:06 +0200 Subject: more bank example stuff last missing: edit account + account operations --- bank-eiffel/bank.e | 567 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 469 insertions(+), 98 deletions(-) diff --git a/bank-eiffel/bank.e b/bank-eiffel/bank.e index f9390c7..523878b 100644 --- a/bank-eiffel/bank.e +++ b/bank-eiffel/bank.e @@ -16,20 +16,23 @@ feature {NONE} -- Implementation data_filename: STRING = "bank.data" + fd: FORMAT_DOUBLE + feature {NONE} -- Initialization run do + create fd.make(2, 2) create store.make(100) - store.ranges.account.creditline := [-1000.0, 0.0] store.ranges.account.interest_deposit := [0.01, 0.04] store.ranges.account.interest_debit := [0.02, 0.1] - store.ranges.studentaccount.creditline := [-300.0, 0.0] + store.ranges.account.creditline := [-1000.0, 0.0] store.ranges.studentaccount.interest_deposit := [0.02, 0.03] store.ranges.studentaccount.interest_debit := [0.03, 0.06] - store.ranges.retireeaccount.creditline := [-300.0, 0.0] + store.ranges.studentaccount.creditline := [-300.0, 0.0] store.ranges.retireeaccount.interest_deposit := [0.02, 0.03] store.ranges.retireeaccount.interest_debit := [0.03, 0.06] + store.ranges.retireeaccount.creditline := [-300.0, 0.0] create last_error.make_empty create data_file.make (data_filename) @@ -63,7 +66,7 @@ feature -- Basic operations print_last_error do if not last_error.is_empty then - print (last_error + "%N%N") + print ("Error: " + last_error + "%N%N") last_error := "" end end @@ -141,7 +144,7 @@ feature -- Basic operations io.read_character io.next_line - inspect io.last_character + inspect io.last_character.lower when 'a' then edit_ranges("account", store.ranges.account) when 's' then @@ -159,16 +162,13 @@ feature -- Basic operations print_ranges(range: like {BANK_STORE}.account_range) do - print (" - Creditline: " + print_range(range.creditline) + "%N") print (" - Interest deposit: " + print_range(range.interest_deposit) + "%N") print (" - Interest debit: " + print_range(range.interest_debit) + "%N") + print (" - Creditline: " + print_range(range.creditline) + "%N") end print_range(range: like {BANK_STORE}.range): STRING_8 - local - fd: FORMAT_DOUBLE do - create fd.make(2, 2) Result := "[" + fd.formatted(range.min) + ", " + fd.formatted(range.max) + "]" end @@ -176,16 +176,13 @@ feature -- Basic operations do print ("%N") print ("Edit " + type + " range:%N") - edit_range("creditline", range.creditline) edit_range("interest depost", range.interest_deposit) edit_range("interest debit", range.interest_debit) + edit_range("creditline", range.creditline) end edit_range(type: STRING_8; range: like {BANK_STORE}.range) - local - fd: FORMAT_DOUBLE do - create fd.make(2, 2) print ("Enter " + type + " minimum [" + fd.formatted(range.min) + "]: ") io.readline if io.last_string.is_double then @@ -220,9 +217,11 @@ feature -- Basic operations io.read_character io.next_line - inspect io.last_character + inspect io.last_character.lower when 'l' then list_persons + print ("Press to go back") + io.read_line when 'c' then create_persons when 'e' then @@ -238,9 +237,161 @@ feature -- Basic operations end end + list_persons + do + print ("%N") + print ("Persons: " + store.persons.count.out + "%N") + from + store.persons.start + until + store.persons.off + loop + print ("#" + store.persons.index.out + " " + store.persons.item.surname + ", " + store.persons.item.firstname) + if attached {RETIREE} store.persons.item then + print (" (RETIREE)") + elseif attached {STUDENT} store.persons.item then + print (" (STUDENT)") + end + print ("%N") + store.persons.forth + end + print ("%N") + end + + create_persons + local + back: BOOLEAN + firstname: STRING + surname: STRING + do + from + until + back + loop + print_header + print ("Operations:%N") + print (" p ...create person%N") + print (" s ...create student%N") + print (" r ...create retiree%N") + print (" b ...back%N") + print ("%N") + print_last_error + print ("Enter a command, followed by : ") + io.read_character + io.next_line + + inspect io.last_character.lower + when 'p', 's', 'r' then + print ("Enter surname: ") + io.readline + surname := io.last_string.twin + + print ("Enter firstname: ") + io.readline + firstname := io.last_string.twin + + inspect io.last_character.lower + when 'p' then + store.persons.put(create {PERSON}.make(surname, firstname)) + when 's' then + store.persons.put(create {STUDENT}.make(surname, firstname)) + when 'r' then + store.persons.put(create {RETIREE}.make(surname, firstname)) + else + end + last_error := "Person (#" + store.persons.count.out + ") created successfully" + back := True + when 'b' then + back := True + else + last_error := "Error: Unknown command '" + io.last_character.out + "'" + end + print ("%N%N") + end + rescue + if not (create {EXCEPTIONS}).is_signal then + last_error := "Exception: " + (create {EXCEPTIONS}).tag_name + retry + end + end + + edit_person + local + back: BOOLEAN + num: INTEGER + do + list_persons + from + until + back + loop + print_last_error + print ("Enter person id (b ...back): ") + io.readline + io.last_string.to_lower + if io.last_string.is_integer then + num := io.last_string.to_integer + if store.persons.valid_index (num) then + store.persons.go_i_th (num) + + print ("Enter surname [" + store.persons.item.surname + "]: ") + io.readline + if not io.last_string.is_empty then + store.persons.item.surname := io.last_string.twin + end + + print ("Enter firstname [" + store.persons.item.firstname + "]: ") + io.readline + if not io.last_string.is_empty then + store.persons.item.firstname := io.last_string.twin + end + last_error := "Person (#" + num.out + ") edited successfully" + back := True + else + last_error := "Invalid person id" + end + elseif io.last_string.is_equal ("b") then + back := True + else + last_error := "Not a number" + end + end + end + + delete_person + local + back: BOOLEAN + do + list_persons + from + until + back + loop + print_last_error + print ("Enter person id (b ...back): ") + io.readline + io.last_string.to_lower + if io.last_string.is_integer then + if store.persons.valid_index (io.last_string.to_integer) then + store.persons.go_i_th (io.last_string.to_integer) + store.persons.remove + last_error := "Person (#" + io.last_string + ") removed successfully" + back := True + else + last_error := "Invalid person id" + end + elseif io.last_string.is_equal ("b") then + back := True + else + last_error := "Not a number" + end + end + end + accounts_screen local back: BOOLEAN + done: BOOLEAN do from until @@ -259,12 +410,42 @@ feature -- Basic operations io.read_character io.next_line - inspect io.last_character + inspect io.last_character.lower when 'l' then list_accounts + + from + done := False + until + back or done + loop + print ("Enter account id for details (b ...back): ") + io.readline + io.last_string.to_lower + if io.last_string.is_integer then + if store.accounts.valid_index (io.last_string.to_integer) then + store.accounts.go_i_th (io.last_string.to_integer) + print ("%N") + print ("Account #" + store.accounts.index.out + ":%N") + list_account_details(store.accounts.item) + print ("Press to go back") + io.read_line + done := True + else + print ("Invalid account id%N%N") + end + elseif io.last_string.is_equal("b") then + done := True + else + print ("Not a number%N%N") + end + end when 'c' then + create_accounts when 'e' then + edit_accounts when 'd' then + delete_account when 'b' then back := True else @@ -274,62 +455,82 @@ feature -- Basic operations end end - list_persons + list_accounts + local + account: ACCOUNT + person: PERSON do print ("%N") - print ("Persons: " + store.persons.count.out + "%N%N") + print ("Accounts: " + store.accounts.count.out + "%N") from - store.persons.start + store.accounts.start until - store.persons.off + store.accounts.off loop - print ("#" + store.persons.index.out + " " + store.persons.item.surname + ", " + store.persons.item.firstname) - if attached {RETIREE} store.persons.item then - print (" (RETIREE)") - elseif attached {STUDENT} store.persons.item then - print (" (STUDENT)") + account := store.accounts.item + account.get_authorized_signers.linear_representation.start + person := account.get_authorized_signers.linear_representation.item + + print ("#" + store.accounts.index.out + " " + person.surname + ", " + person.firstname) + if attached {RETIREEACCOUNT} account then + print (" (RETIREE ACCOUNT)") + elseif attached {STUDENTACCOUNT} account then + print (" (STUDENT ACCOUNT)") end print ("%N") - store.persons.forth - end + store.accounts.forth + end print ("%N") - print ("Press to go back") - io.read_line end - list_accounts + list_account_details(account: ACCOUNT) + local + person: PERSON + range: like {BANK_STORE}.account_range do - print ("%N") - print ("Accounts: " + store.accounts.count.out + "%N%N") + range := store.ranges.account + if attached {RETIREEACCOUNT} account then + range := store.ranges.retireeaccount + elseif attached {STUDENTACCOUNT} account then + range := store.ranges.studentaccount + end + + print (" - Balance: " + fd.formatted(account.balance) + "%N") + print (" - Interest deposit: " + fd.formatted(account.interest_deposit) + " " + print_range(range.interest_deposit) + "%N") + print (" - Interest debit: " + fd.formatted(account.interest_debit) + " " + print_range(range.interest_debit) +"%N") + print (" - Creditline: " + fd.formatted(account.creditline) + " " + print_range(range.creditline) +"%N") + print (" - Authorized signers:%N") from - store.accounts.start + account.get_authorized_signers.linear_representation.start until - store.accounts.off + account.get_authorized_signers.linear_representation.off loop - --TODO - print ("#" + store.accounts.index.out + "%N") - print (" balance=" + store.accounts.item.balance.out + "%N") - print (" creditline=" + store.accounts.item.creditline.out + "%N") - if attached {RETIREEACCOUNT} store.accounts.item then - print (" (RETIREEACCOUNT)") - elseif attached {STUDENTACCOUNT} store.accounts.item then - print (" (STUDENTACCOUNT)") + person := account.get_authorized_signers.linear_representation.item + print (" - #" + account.get_authorized_signers.linear_representation.index.out + " " + person.surname + ", " + person.firstname) + if attached {RETIREE} person then + print (" (RETIREE)") + elseif attached {STUDENT} person then + print (" (STUDENT)") end print ("%N") - store.accounts.forth + account.get_authorized_signers.linear_representation.forth end - print ("%N") - print ("Press to go back") - io.read_line + print (" - Type: ") + if attached {RETIREEACCOUNT} account then + print ("Retiree account") + elseif attached {STUDENTACCOUNT} account then + print ("Student account") + else + print ("Account") + end + print ("%N%N") end - create_persons + create_accounts local back: BOOLEAN - firstname: STRING - surname: STRING do from until @@ -337,9 +538,9 @@ feature -- Basic operations loop print_header print ("Operations:%N") - print (" p ...create person%N") - print (" s ...create student%N") - print (" r ...create retiree%N") + print (" a ...create account%N") + print (" s ...create student account%N") + print (" r ...create retiree account%N") print (" b ...back%N") print ("%N") print_last_error @@ -347,27 +548,13 @@ feature -- Basic operations io.read_character io.next_line - inspect io.last_character - when 'p', 's', 'r' then - print ("Enter surname: ") - io.readline - surname := io.last_string.twin - - print ("Enter firstname: ") - io.readline - firstname := io.last_string.twin - - inspect io.last_character - when 'p' then - store.persons.put(create {PERSON}.make(surname, firstname)) - when 's' then - store.persons.put(create {STUDENT}.make(surname, firstname)) - when 'r' then - store.persons.put(create {RETIREE}.make(surname, firstname)) - else - end - last_error := "Person (#" + store.persons.count.out + ") created successfully" - back := True + inspect io.last_character.lower + when 'a' then + back := create_account('a', store.ranges.account) + when 's' then + back := create_account('s', store.ranges.studentaccount) + when 'r' then + back := create_account('r', store.ranges.retireeaccount) when 'b' then back := True else @@ -382,44 +569,228 @@ feature -- Basic operations end end - edit_person + create_account(type: CHARACTER; range: like {BANK_STORE}.account_range): BOOLEAN + local + back: BOOLEAN + next: BOOLEAN + interest_deposit: like {ACCOUNT}.interest_deposit + interest_debit: like {ACCOUNT}.interest_debit + creditline: like {ACCOUNT}.creditline do - print ("%N") - print ("Enter person id (0 ...back): ") - io.read_integer - if io.last_integer > 0 then - if store.persons.valid_index (io.last_integer) then - store.persons.go_i_th (io.last_integer) + Result := false + interest_deposit := (range.interest_deposit.min + range.interest_deposit.max) / 2 + interest_debit := (range.interest_debit.min + range.interest_debit.max) / 2 + creditline := (range.creditline.min + range.creditline.max) / 2 - print ("Enter surname [" + store.persons.item.surname + "]: ") - io.readline - if not io.last_string.is_empty then - store.persons.item.surname := io.last_string.twin + print_last_error + from + next := False + list_persons + until + back or next + loop + print ("Enter person (authorized signer) id (b ...back): ") + io.readline + io.last_string.to_lower + if io.last_string.is_integer then + if store.persons.valid_index (io.last_string.to_integer) then + store.persons.go_i_th (io.last_string.to_integer) + next := True + else + print ("Invalid person id%N%N") end + elseif io.last_string.is_equal ("b") then + back := True + else + print ("Not a number%N%N") + end + end - print ("Enter firstname [" + store.persons.item.firstname + "]: ") - io.readline - if not io.last_string.is_empty then - store.persons.item.firstname := io.last_string.twin + from + next := False + until + back or next + loop + print ("Enter interest deposit " + print_range(range.interest_deposit) + " [" + fd.formatted(interest_deposit) + "] (b ...back): ") + io.readline + io.last_string.to_lower + if io.last_string.is_empty then + next := True + elseif io.last_string.is_double then + interest_deposit := io.last_string.to_double + next := True + elseif io.last_string.is_equal ("b") then + back := True + else + print ("Invalid interest deposit number%N%N") + end + end + + from + next := False + until + back or next + loop + print ("Enter interest debit " + print_range(range.interest_debit) + " [" + fd.formatted(interest_debit) + "] (b ...back):") + io.readline + io.last_string.to_lower + if io.last_string.is_empty then + next := True + elseif io.last_string.is_double then + interest_debit := io.last_string.to_double + next := True + elseif io.last_string.is_equal ("b") then + back := True + else + print ("Invalid interest debit number%N%N") + end + end + + from + next := False + until + back or next + loop + print ("Enter creditline " + print_range(range.creditline) + " [" + fd.formatted(creditline) + "] (b ...back): ") + io.readline + io.last_string.to_lower + if io.last_string.is_empty then + next := True + elseif io.last_string.is_double then + creditline := io.last_string.to_double + next := True + elseif io.last_string.is_equal ("b") then + back := True + else + print ("Invalid creditline number%N%N") + end + end + + if not back then + inspect type + when 'a' then + store.accounts.put(create {ACCOUNT}.make(store.persons.item, + interest_deposit, interest_debit, creditline, + range.interest_deposit, range.interest_debit, range.creditline)) + when 's' then + store.accounts.put(create {STUDENTACCOUNT}.make(store.persons.item, + interest_deposit, interest_debit, creditline, + range.interest_deposit, range.interest_debit, range.creditline)) + when 'r' then + store.accounts.put(create {RETIREEACCOUNT}.make(store.persons.item, + interest_deposit, interest_debit, creditline, + range.interest_deposit, range.interest_debit, range.creditline)) + else + end + last_error := "Account (#" + store.accounts.count.out + ") created successfully" + Result := true + back := True + end + end + + edit_accounts + local + back: BOOLEAN + do + list_accounts + from + until + back + loop + print_last_error + print ("Enter account id (b ...back): ") + io.readline + io.last_string.to_lower + if io.last_string.is_integer then + if store.accounts.valid_index (io.last_string.to_integer) then + store.accounts.go_i_th (io.last_string.to_integer) + edit_account(store.accounts.index, store.accounts.item) + back := True + else + last_error := "Invalid account id" end + elseif io.last_string.is_equal ("b") then + back := True else - last_error := "Invalid person id" + last_error := "Not a number" end end end - delete_person + edit_account(index: INTEGER; account: ACCOUNT) + local + back: BOOLEAN do - print ("%N") - print ("Enter person id (0 ...back): ") - io.read_integer - if io.last_integer > 0 then - if store.persons.valid_index (io.last_integer) then - store.persons.go_i_th (io.last_integer) - store.persons.remove - last_error := "Person (#" + io.last_integer.out + ") removed successfully" + from + until + back + loop + print_header + print ("Edit account #" + index.out + ":%N") + list_account_details (account) + + print ("Operations:%N") + print (" 1 ...edit interest deposit%N") + print (" 2 ...edit interest debit%N") + print (" 3 ...edit creditline%N") + print (" 4 ...add authorized signer%N") + print (" 5 ...remove authorized signer%N") + print (" 6 ...deposit%N") + print (" 7 ...withdraw%N") + print (" 8 ...transfer%N") + print (" 9 ...advance%N") + print (" b ...back%N") + print ("%N") + print_last_error + print ("Enter a command, followed by : ") + io.read_character + io.next_line + + inspect io.last_character.lower + when '1' then + when '2' then + when '3' then + when '4' then + when '5' then + when '6' then + when '7' then + when '8' then + when '9' then + when 'b' then + back := True + else + last_error := "Error: Unknown command '" + io.last_character.out + "'" + end + print ("%N%N") + end + end + + delete_account + local + back: BOOLEAN + do + list_accounts + from + until + back + loop + print_last_error + print ("Enter account id (b ...back): ") + io.readline + io.last_string.to_lower + if io.last_string.is_integer then + if store.accounts.valid_index (io.last_string.to_integer) then + store.accounts.go_i_th (io.last_string.to_integer) + store.accounts.remove + last_error := "Account (#" + io.last_string + ") removed successfully" + back := True + else + last_error := "Invalid account id" + end + elseif io.last_string.is_equal ("b") then + back := True else - last_error := "Invalid person id" + last_error := "Not a number" end end end -- cgit v1.2.3