首页 > > 详细

讲解Python程序、Java语言辅导、讲解Statistics统计、Processing讲解

 

Here is a more complete example of running the software:
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): aaaa
You must type an integer!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): bbbb cccc
You must type an integer!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): -100
Positive integers only!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 7
Unknown action!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 1
Total amount of money in the bank: 0
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): -100
Positive integers only!
Type the account type (credit:1 student:2): 3
Unknown type of account!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): 1
Enter the name of the customer: Philippe
Enter the initial amount of money: -100
Positive integers only!
Enter the initial amount of money: 500
Credit account for Philippe with 500 yuan has been added
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): 2
Enter the name of the customer: Meunier
Enter the initial amount of money: 1500
Student account for Meunier with 1500 yuan has been added
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 1
Total amount of money in the bank: 2000
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Meunier
Meunier has 1500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: aaaa
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: Philippe
Enter the amount of money to withdraw: -100
Positive integers only!
Enter the amount of money to withdraw: 200
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 300 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: aaaa
Enter the amount of money to withdraw: 200
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: Meunier
Enter the amount of money to withdraw: 2000
Cannot withdraw 2000 yuan from account, only 1500 yuan is available
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Meunier
Meunier has 1500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 5
Enter the name of the customer: Philippe
Enter the amount of money to deposit: -100
Positive integers only!
Enter the amount of money to deposit: 200
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 5
Enter the name of the customer: aaaa
Enter the amount of money to deposit: 200
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 1
Total amount of money in the bank: 2000
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 6
Goodbye!
Question 7
We now want to create a graphical user interface (GUI) for our bank software. Since we want our bank software to have
multiple views, we will use the Model-View-Controller design pattern.
First, create a ModelListener interface with the following UML specification:
+-------------------+
| > |
| ModelListener |
+-------------------+
| + update(): void |
+-------------------+
This interface will be implemented by views and the model will use this interface to notify the views that they need to
update themselves.
Second, the Bank class is the class that contains all the data for the bank. Therefore the Bank class plays the role of the
model. Therefore the Bank class needs to keep an arraylist of model listeners that need to be notified every time the
bank (the model) changes. Therefore add to the Bank class an arraylist of ModelListener. Also add to the Bank
class an addListener method that takes a ModelListener as argument and adds it to the arraylist of listeners.
Also add to the Bank class a private notifyListeners method that takes nothing as argument and calls the
update method of all the listeners of the bank. Then change the addAccount and withdraw methods so that they
call the notifyListeners every time a change is made to the bank's data (only the addAccount and withdraw
methods change the bank data, so only these two methods need to call the notifyListeners method; the
totalMoney and getMoney methods do not change the bank's data, they only inspect the data, so they do not need
to call the notifyListeners method).
Use the Test class to make sure all your tests still work. Use the TUI class to make sure your textual user interface still
works.
Third, create a ViewSimple class that extends JFrame, implements the ModelListener interface, and has the
following UML specification:
+------------------------------------------+
| ViewSimple |
+------------------------------------------+
| - m: Bank |
| - c: ControllerSimple |
| - label: JLabel |
+------------------------------------------+
| + ViewSimple(Bank m, ControllerSimple c) |
| + update(): void |
+------------------------------------------+
The constructor of the ViewSimple class creates a JLabel object, stores it in the label instance variable of the
ViewSimple class, initializes it to display the total amount of money in all the bank accounts of the bank, and adds it
to the view (which is a frame). The update method of the ViewSimple class updates the text of the label as
necessary so that the label always displays the current value of the total amount of money in all the bank accounts of
the bank.
Fourth, create a ControllerSimple class with the following UML specification:
+------------------------------------------+
| ControllerSimple |
+------------------------------------------+
| - m: Bank |
+------------------------------------------+
| + ControllerSimple(Bank m) |
+------------------------------------------+
Since the ViewSimple does not have any button, it cannot perform. any action, therefore the corresponding controller
ControllerSimple does nothing. (We still want to have ControllerSimple class so that our application follows
the correct Model-View-Controller design pattern.)
Fifth, create a GUI class with a main method. In this main method, create an anonymous class that implements the
Runnable interface with a run method and use the javax.swing.SwingUtilities.invokeLater method
to run that code on the event dispatch thread.
Sixth, we need to connect the model, the view, and the controller to each other. So in the run method of the
anonymous class:
 create a Bank object (the model object);
 then create a ControllerSimple object (the controller object) that takes the model object as argument;
 then create a ViewSimple object that takes the model object and the controller object as argument;
 then register the view object with the model object using the addListener method of the model object.
Use the GUI class to run your GUI: you should see a window that shows the total amount of money in all the bank
accounts of the bank. This total amount must be zero, since the bank (model object) you just created above does not
contain any account!
As a test, in the run method of the anonymous class, you can try to manually add to your bank (model object) some
student accounts and credit accounts to check that your GUI displays the correct total amount of money in all the bank
accounts of the bank.
Question 8
In the next questions we want to add more views. So, to simplify the next questions, create a View class which is going
to be the superclass of all views. This View class extends JFrame, implements the ModelListener interface, and
has the following UML specification:
+------------------------------------------+
| View |
+------------------------------------------+
| = m: Bank |
+------------------------------------------+
| + View(Bank m) |
| + update(): void |
+------------------------------------------+
The m instance variable of the View class is protected (so that it can be easily used in all the subclasses of View).
The update method of the View class is abstract.
Then modify the ViewSimple class to be a subclass of the View class. To simplify a little the code of the next
questions, also move the setDefaultCloseOperation method call from the constructor of ViewSimple to the
constructor of View.
Also create a Controller class which is going to be the superclass of all controllers. This Controller class has the
following UML specification:
+------------------------------------------+
| Controller |
+------------------------------------------+
| = m: Bank |
+------------------------------------------+
| + Controller(Bank m) |
+------------------------------------------+
The m instance variable of the Controller class is protected (so that it can be easily used in all the subclasses of
Controller).
Then modify the ControllerSimple class to be a subclass of the Controller class.
Run your GUI and check that it still works as before.
Question 9
We now want to add a new “get money” view that allows the user of the software to check how much money there is in
a specific account.
Create a ViewGetMoney class that extends View and has the following UML specification:
+----------------------------------------------+
| ViewGetMoney |
+----------------------------------------------+
| - c: ControllerGetMoney |
| - t: JTextField |
+----------------------------------------------+
| + ViewGetMoney(Bank m, ControllerGetMoney c) |
| + update(): void |
+----------------------------------------------+
The ViewGetMoney shows the text field called t (where the user can type text) and a button. Use a grid layout
manager to position the two components.
The user can type in the text field t the name of a bank account customer. When the user then clicks on the button, the
action listener of the button must read the name of the customer that was typed in the text field (using the getText
method of the text field) and must call the getMoney method of the controller with that customer name as argument.
The getMoney method of the controller returns a string as result which must then be displayed back to the user using
a message dialog (using the showMessageDialog method of the JOptionPane class).
The update method of the ViewGetMoney class does nothing, because the ViewGetMoney class does not
graphically display any data from the bank (the model).
Also create a ControllerGetMoney class that extends Controller and has the following UML specification:
+------------------------------------------+
| ControllerGetMoney |
+------------------------------------------+
+------------------------------------------+
| + ControllerGetMoney(Bank m) |
| + getMoney(String name): String |
+------------------------------------------+
The getMoney method takes the name of a bank customer as argument. The getMoney method of the controller
then calls the getMoney method of the bank to get the amount of money currently stored in the bank account that
belongs to that customer. The getMoney method of the controller then transforms the integer result of the
getMoney method of the bank into a string and returns that string as result (to the view). If the getMoney method of
the bank throws an UnknownCustomerException then the getMoney method of the controller must catch this
exception and return as result the error message from the exception object.
Modify the run method of the GUI class to add a ViewGetMoney view that uses a ControllerGetMoney
controller and the same model as before (not a new model!)
Run your GUI and check that you can correctly use the new view to query the amount of money for different customers
of your bank (obviously your bank must have some customers in it to test this: see the last paragraph of Question 7).
Also check that querying the amount of money of an unknown customer correctly shows an error message.
Question 10
We now want to add a new “withdraw” view that allows the user of the software to withdraw money from a specific
account.
Create a ViewWithdraw class that extends View and has the following UML specification:
+----------------------------------------------+
| ViewWithdraw |
+----------------------------------------------+
| - c: ControllerWithdraw |
| - t1: JTextField |
| - t2: JTextField |
+----------------------------------------------+
| + ViewWithdraw(Bank m, ControllerWithdraw c) |
| + update(): void |
+----------------------------------------------+
The ViewWithdraw shows the two text field called t1 and t2 (where the user can type text) and a button. Use a grid
layout manager to position the two components.
The user can type in the first text field the name of a bank account customer and can type in the second text field an
amount of money. When the user then clicks on the button, the action listener of the button must read the name of the
customer that was typed in the first text field (using the getText method of the text field) and the amount of money
that was typed in the second text field (using again the getText method) and calls the withdraw method of the
controller with these two strings as arguments. The withdraw method of the controller then returns a string as result.
If the string returned by the withdraw method of the controller is different from the empty string "" then this string
must be displayed back to the user using a message dialog (using the showMessageDialog method of the
JOptionPane class). If the string returned by the withdraw method of the controller is equal to the empty string ""
then nothing happens in ViewWithdraw.
The update method of the ViewWithdraw class does nothing, because the ViewWithdraw class does not
graphically display any data from the bank (the model).
Also create a ControllerWithdraw class that extends Controller and has the following UML specification:
+------------------------------------------------+
| ControllerWithdraw |
+------------------------------------------------+
+------------------------------------------------+
| + ControllerWithdraw(Bank m) |
| + withdraw(String name, String amount): String |
+------------------------------------------------+
The withdraw method takes the name of a bank customer and an amount of money (as a string) as arguments. The
withdraw method of the controller then transforms the amount of money from a string to an integer (using the
Integer.parseInt static method) and calls the withdraw method of the bank to withdraw that amount of money
from the amount of money currently stored in the bank account that belongs to that customer. If no exception occurs
then the withdraw method of the controller returns the empty string. If the withdraw method of the bank throws
an UnknownCustomerException then the withdraw method of the controller must catch this exception and
return as result the error message from the exception object. If the withdraw method of the bank throws a
NotEnoughMoneyException then the withdraw method of the controller must catch this exception and return
as result the error message from the exception object. If the parseInt method of the Integer class throws a
NumberFormatException (because the user typed something which is not an integer) then the withdraw method
of the controller must catch this exception and return as result the error message from the exception object.
Modify the run method of the GUI class to add a ViewWithdraw view that uses a ControllerWithdraw
controller and the same model as before (not a new model!)
Run your GUI and check that you can correctly use the new view to withdraw money for different customers of your
bank (obviously your bank must have some customers in it to test this: see the last paragraph of Question 7). Check that,
when you withdraw money, the simple view is automatically correctly updated to show the new total amount of money
in all the bank accounts of the bank. Also check that withdrawing money from an unknown customer correctly shows an
error message. Also check that withdrawing too much money from a student account correctly shows an error message.
Also check that trying to withdraw an amount of money which is not an integer correctly shows an error message (do
not worry about the content of the error message).
Question 11
We now want to add a new “create” view that allows the user of the software to create a new bank account.
Create a ViewCreate class that extends View and has the following UML specification:
+----------------------------------------------+
| ViewCreate |
+----------------------------------------------+
| - c: ControllerCreate |
| - t1: JTextField |
| - t2: JTextField |
| - cb: JComboBox |
+----------------------------------------------+
| + ViewCreate(Bank m, ControllerCreate c) |
| + update(): void |
+----------------------------------------------+
The ViewCreate shows the two text field called t1 and t2 (where the user can type text), the combo box cb (where
the user can select one option from a menu) and a button. Use a grid layout manager to position the two components.
The user can type in the first text field the name of a new bank customer and can type in the second text field an
amount of money for the new account. The combo box offers only two menu options: "credit account" and
"student account". When the user then clicks on the button, the action listener of the button must read the name
of the new customer that was typed in the first text field (using the getText method of the text field), read the
amount of money that was typed in the second text field (using again the getText method), and read which menu
option was selected in the combo box (using the getSelectedIndex method of the combo box, which returns the
integer 0 or 1 depending on which menu option the user selected in the combo box), and calls the create method of
the controller with these two strings and the integer as arguments. The create method of the controller then returns a
string as result. If the string returned by the create method of the controller is different from the empty string ""
then this string must be displayed back to the user using a message dialog (using the showMessageDialog method
of the JOptionPane class). If the string returned by the create method of the controller is equal to the empty string
"" then nothing happens in ViewCreate.
The update method of the ViewCreate class does nothing, because the ViewCreate class does not graphically
display any data from the bank (the model).
Also create a ControllerCreate class that extends Controller and has the following UML specification:
+----------------------------------------------------------+
| ControllerCreate |
+----------------------------------------------------------+
+----------------------------------------------------------+
| + ControllerCreate(Bank m) |
| + create(String name, String amount, int type): String |
+----------------------------------------------------------+
The create method takes as arguments the name of a new bank customer, an amount of money (as a string), and an
integer representing the type of bank account to create (where the integer 0 means a credit account and the integer 1
means a student account). The create method of the controller then transforms the amount of money from a string to
an integer (using the Integer.parseInt static method), creates an object from the correct account class (based on
the type of account specified by the user: credit account or student account) and calls the addAccount method of the
bank to add the new account object to the bank. If no exception occurs then the create method of the controller
returns the empty string. If the constructor of the StudentAccount class throws a NotEnoughMoneyException
then the create method of the controller must catch this exception and return as result the error message from the
exception object. If the parseInt method of the Integer class throws a NumberFormatException (because
the user typed something which is not an integer) then the create method of the controller must catch this exception
and return as result the error message from the exception object.
Modify the run method of the GUI class to add a ViewCreate view that uses a ControllerCreate controller and
the same model as before (not a new model!)
Note: if at the end of Question 7 you had manually added to your bank (model object) some student accounts and credit
accounts for testing, then you must now remove those accounts from the run method of the anonymous class inside
the GUI class. You do not need these test accounts anymore because you have now a graphical user interface to create
new accounts!
Run your GUI and check that you can correctly use the new view to create different customers for your bank, with
different types of accounts. Check that, when you create a new account, the simple view is automatically correctly
updated to show the new total amount of money in all the bank accounts of the bank. Also use the “get money” window
of your software to check that the accounts are correctly created with the correct names and correct amounts of money.
Also check that trying to create a student account with a negative amount of money correctly shows an error message.
Also check that trying to create an account with an amount of money which is not an integer correctly shows an error
message (do not worry about the content of the error message). After you created a new account, you can also check
whether it is a credit account or a student account by using the “withdraw” window to withdraw an amount of money
which is bigger than the amount that the account has: if the account you created is a credit account then trying to
withdraw from the account an amount of money which is bigger than the amount that the account has will work and the
amount in the account will become negative (you can then check that using the “get money” window); if the account
you created is a student account then trying to withdraw from the account an amount of money which is bigger than the
amount that the account has will fail with an error message and the amount in the account will not change (you can
then check that using the “get money” window).
Question 12
We now want to add a new “history” view that allows the user of the software to keep track of how the total amount of
money in all the bank accounts of the bank changes over time.
Before we can add such a view to the GUI, first we need to change the model (the Bank class) to keep track of how the
total amount of money in all the bank accounts of the bank changes over time. Therefore, in the Bank class, add a new
private instance variable called history which is an arraylist of integers. This arraylist must be initialized to contain
only one value: zero (meaning that, when the bank is created, it has no money).
We know that the data of the bank can change only in two methods of the Bank class: in the addAccount method
and in the withdraw method (this is why these two methods both call notifyListeners: to tell the views that
data has changed and that the views must update themselves). Therefore it is in these two methods addAccount and
withdraw that we must keep track of how the total amount of money in all the bank accounts of the bank changes
over time. Therefore, in these two methods addAccount and withdraw, call the totalMoney method and add the
result to the history arraylist. Note: in each of the two methods addAccount and withdraw, you must call the
totalMoney method and add the result to the history arraylist before notifyListeners is called, otherwise
the “history” view that you are going to create below will not show the correct results when it is notified by the bank
that it must update itself!
Also add to the Bank class a getHistory method that returns as result the arraylist of integers from the bank’s
history.
Create a HistoryPanel class that extends JPanel. The constructor of HistoryPanel takes as argument a model
object of type Bank, which you need to store in some private instance variable. Add to the HistoryPanel class two
private methods called historyMax and historyMin that take an arraylist of integers as argument and return as
result the maximum and minimum number in the arraylist, respectively (you can assume that the arraylist contains at
least one number). Then add to the HistoryPanel class a private method called historyRange that takes an
arraylist of integers as argument and returns as result the difference between the max and min of the integers in the
arraylist, or returns as result 100 if the difference between the man and min of the integers in the arraylist is strictly less
than 100.
Override the protected void paintComponent(Graphics g) method inherited from JPanel, and, inside
your new paintComponent method, draw graphically how the total amount of money in all the bank accounts of the
bank changes over time, as follows:
 Compute the following variables (where history is the result of calling the getHistory method of the
model):
int min = historyMin(history);
int range = historyRange(history);
int maxX = getWidth() - 1;
int maxY = getHeight() - 1;
int zero = maxY + min * maxY / range;
 Draw a blue line between the point (0, zero) and the point (maxX, zero) (this blue line then represents
the horizontal “zero” axis).
 For each value v at index i in the history arraylist that you want to draw:
o Use x = 10 * i for the horizontal coordinate;
o Use y = zero - v * maxY / range for the vertical coordinate;
o Draw red lines between all the points (x, y) (if there is only one value in the arraylist then just draw a
rectangle of size 1 by 1 at position (x, y)).
Create a ViewHistory class that extends View and has the following UML specification:
+----------------------------------------------+
| ViewHistory |
+----------------------------------------------+
| - c: ControllerHistory |
+----------------------------------------------+
| + ViewHistory(Bank m, ControllerHistory c) |
| + update(): void |
+----------------------------------------------+
The ViewHistory shows only a HistoryPanel object, nothing else. The update method of the ViewHistory
class calls Swing’s repaint method (this forces Swing to redraw everything every time the model changes, which in
turn forces Swing to automatically call the paintComponent method of the HistoryPanel to redraw the updated
version of the bank’s history).
Also create a ControllerHistory class that extends Controller and has the following UML specification:
+----------------------------------------------------------+
| ControllerHistory |
+----------------------------------------------------------+
+----------------------------------------------------------+
| + ControllerHistory(Bank m) |
| + create(String name, String amount, int type): String |
+----------------------------------------------------------+
Since the ViewHistory does not receive any input from the user, the ControllerHistory does nothing (we just
need it so that our software follows the Model-View-Controller design pattern).
Modify the run method of the GUI class to add a ViewHistory view that uses a ControllerHistory controller
and the same model as before (not a new model!)
Run your GUI and check that adding new bank customers and withdrawing money from customers correctly updates the
graphical history of the bank’s total amount of money. For example, if the user of the software creates a student
account with 2000 yuan, then withdraws 500 yuan from the student account, then creates a credit account with 1000
yuan, then withdraws 4000 yuan from the credit account, then creates a student account with 2000 yuan, then the
graphical history of the bank’s total amount of money must look like this:

Check that all the other features of your software still work correctly. Also run your tests and the TUI to make sure
everything still works correctly.
The end. That was fun!
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!