summaryrefslogtreecommitdiffstats
path: root/bank-eiffel/bank.e
blob: f9390c7889a226ebe55ee81c45d713e203e26cca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
class
	BANK

create
	run

feature {NONE} -- Implementation

	store: BANK_STORE

	over: BOOLEAN

	last_error: STRING

	data_file: RAW_FILE

	data_filename: STRING = "bank.data"

feature {NONE} -- Initialization

	run
		do
			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.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.retireeaccount.interest_deposit := [0.02, 0.03]
			store.ranges.retireeaccount.interest_debit := [0.03, 0.06]
			create last_error.make_empty

			create data_file.make (data_filename)
			if data_file.exists then
				data_file.open_read
				store ?= store.retrieved (data_file)
				data_file.close
			end

			session

			if data_file.is_creatable then
				data_file.open_write
				store.independent_store (data_file)
				data_file.close
			end
		end

feature -- Basic operations

	session
		do
			from
			until
				over
			loop
				main_screen
			end
		end

	print_last_error
		do
			if not last_error.is_empty then
				print (last_error + "%N%N")
				last_error := ""
			end
		end

	print_header
		do
			clear_screen
			print ("**************************************%N")
			print ("*********    BANK of FOOP    *********%N")
			print ("**************************************%N")
		end

	clear_screen
		do
			io.put_character ((0x1B).to_character_8)
			io.put_string ("[2J")
			io.put_character ((0x1B).to_character_8)
			io.put_string ("[H")
		end

	main_screen
		do
			print_header
			print ("Operations:%N")
			print ("  r ...list/edit global ranges%N")
			print ("  p ...list/create/edit/delete persons%N")
			print ("  a ...list/create/edit/delete accounts%N")
			print ("  q ...quit%N")
			print ("%N")
			print_last_error
			print ("Enter a command, followed by <return>: ")
			io.read_character
			io.next_line

			inspect io.last_character.lower
			when 'r' then
				ranges_screen
			when 'p' then
				persons_screen
			when 'a' then
				accounts_screen
			when 'q' then
				over := True
			else
				last_error := "Error: Unknown command '" + io.last_character.out + "'"
			end
			print ("%N%N")
		end

	ranges_screen
		local
			back: BOOLEAN
		do
			from
			until
				back
			loop
				print_header
				print ("Global Ranges:%N")
				print ("  Account:%N")
				print_ranges(store.ranges.account)
				print ("  Student account:%N")
				print_ranges(store.ranges.studentaccount)
				print ("  Retiree account:%N")
				print_ranges(store.ranges.retireeaccount)
				print ("%N")
				print ("Operations:%N")
				print ("  a ...edit account ranges%N")
				print ("  s ...edit student account ranges%N")
				print ("  r ...edit retiree account ranges%N")
				print ("  b ...back%N")
				print ("%N")
				print_last_error
				print ("Enter a command, followed by <return>: ")
				io.read_character
				io.next_line

				inspect io.last_character
				when 'a' then
					edit_ranges("account", store.ranges.account)
				when 's' then
					edit_ranges("student account", store.ranges.studentaccount)
				when 'r' then
					edit_ranges("retiree account", store.ranges.retireeaccount)
				when 'b' then
					back := True
				else
					last_error := "Error: Unknown command '" + io.last_character.out + "'"
				end
				print ("%N%N")
			end
		end

	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")
		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

	edit_ranges(type: STRING_8; range: like {BANK_STORE}.account_range)
		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)
		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
				range.min := io.last_string.to_double
			end

			print ("Enter " + type + " maximum [" + fd.formatted(range.max) + "]: ")
			io.readline
			if io.last_string.is_double then
				range.max := io.last_string.to_double
			end
		end

	persons_screen
		local
			back: BOOLEAN
		do
			from
			until
				back
			loop
				print_header
				print ("Operations:%N")
				print ("  l ...list persons%N")
				print ("  c ...create person%N")
				print ("  e ...edit person%N")
				print ("  d ...delete person%N")
				print ("  b ...back%N")
				print ("%N")
				print_last_error
				print ("Enter a command, followed by <return>: ")
				io.read_character
				io.next_line

				inspect io.last_character
				when 'l' then
					list_persons
				when 'c' then
					create_persons
				when 'e' then
					edit_person
				when 'd' then
					delete_person
				when 'b' then
					back := True
				else
					last_error := "Error: Unknown command '" + io.last_character.out + "'"
				end
				print ("%N%N")
			end
		end

	accounts_screen
		local
			back: BOOLEAN
		do
			from
			until
				back
			loop
				print_header
				print ("Operations:%N")
				print ("  l ...list accounts%N")
				print ("  c ...create account%N")
				print ("  e ...edit account%N")
				print ("  d ...delete account%N")
				print ("  b ...back%N")
				print ("%N")
				print_last_error
				print ("Enter a command, followed by <return>: ")
				io.read_character
				io.next_line

				inspect io.last_character
				when 'l' then
					list_accounts
				when 'c' then
				when 'e' then
				when 'd' then
				when 'b' then
					back := True
				else
					last_error := "Error: Unknown command '" + io.last_character.out + "'"
				end
				print ("%N%N")
			end
		end

	list_persons
		do
			print ("%N")
			print ("Persons: " + store.persons.count.out + "%N%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")
			print ("Press <return> to go back")
			io.read_line
		end

	list_accounts
		do
			print ("%N")
			print ("Accounts: " + store.accounts.count.out + "%N%N")
			from
				store.accounts.start
			until
				store.accounts.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)")
				end
				print ("%N")
				store.accounts.forth
			end

			print ("%N")
			print ("Press <return> to go back")
			io.read_line
		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 <return>: ")
				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
				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
		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)

					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
				else
					last_error := "Invalid person id"
				end
			end
		end

	delete_person
		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"
				else
					last_error := "Invalid person id"
				end
			end
		end
end