11
22import java .util .ArrayList ;
3+ import java .util .Arrays ;
4+ import java .util .List ;
35import java .util .Scanner ;
6+ import java .util .logging .Level ;
7+ import java .util .logging .Logger ;
48
59public class Menu {
610
711 //Instance Variables
8-
912 Scanner keyboard = new Scanner (System .in );
1013 Bank bank = new Bank ();
1114 boolean exit ;
@@ -62,9 +65,14 @@ private void performAction(int choice) {
6265 System .out .println ("Thank you for using our application." );
6366 System .exit (0 );
6467 break ;
65- case 1 :
66- createAnAccount ();
67- break ;
68+ case 1 : {
69+ try {
70+ createAnAccount ();
71+ } catch (InvalidAccountTypeException ex ) {
72+ System .out .println ("Account was not created successfully." );
73+ }
74+ }
75+ break ;
6876 case 2 :
6977 makeADeposit ();
7078 break ;
@@ -79,26 +87,36 @@ private void performAction(int choice) {
7987 }
8088 }
8189
82- private void createAnAccount () {
83- String firstName , lastName , ssn , accountType = "" ;
84- double initialDeposit = 0 ;
85- boolean valid = false ;
86- while (!valid ) {
87- System .out .print ("Please enter an account type(checking/savings): " );
88- accountType = keyboard .nextLine ();
89- if (accountType .equalsIgnoreCase ("checking" ) || accountType .equalsIgnoreCase ("savings" )) {
90- valid = true ;
91- } else {
92- System .out .println ("Invalid account type selected. Please enter checking or savings." );
90+ private String askQuestion (String question , List <String > answers ) {
91+ String response = "" ;
92+ Scanner input = new Scanner (System .in );
93+ boolean choices = ((answers == null ) || answers .size () == 0 ) ? false : true ;
94+ boolean firstRun = true ;
95+ do {
96+ if (!firstRun ) {
97+ System .out .println ("Invalid selection. Please try again." );
9398 }
94- }
95- System .out .print ("Please enter your first name: " );
96- firstName = keyboard .nextLine ();
97- System .out .print ("Please enter your last name: " );
98- lastName = keyboard .nextLine ();
99- System .out .print ("Please enter your social security number: " );
100- ssn = keyboard .nextLine ();
101- valid = false ;
99+ System .out .print (question );
100+ if (choices ) {
101+ System .out .print ("(" );
102+ for (int i = 0 ; i < answers .size () - 1 ; ++i ) {
103+ System .out .print (answers .get (i ) + "/" );
104+ }
105+ System .out .print (answers .get (answers .size () - 1 ));
106+ System .out .print ("): " );
107+ }
108+ response = input .nextLine ();
109+ firstRun = false ;
110+ if (!choices ) {
111+ break ;
112+ }
113+ } while (!answers .contains (response ));
114+ return response ;
115+ }
116+
117+ private double getDeposit (String accountType ) {
118+ double initialDeposit = 0 ;
119+ Boolean valid = false ;
102120 while (!valid ) {
103121 System .out .print ("Please enter an initial deposit: " );
104122 try {
@@ -120,41 +138,52 @@ private void createAnAccount() {
120138 }
121139 }
122140 }
141+ return initialDeposit ;
142+ }
143+
144+ private void createAnAccount () throws InvalidAccountTypeException {
145+ //Get account information
146+ String accountType = askQuestion ("Please enter an account type: " , Arrays .asList ("checking" , "savings" ));
147+ String firstName = askQuestion ("Please enter your first name: " , null );
148+ String lastName = askQuestion ("Please enter your last name: " , null );
149+ String ssn = askQuestion ("Please enter your social security number: " , null );
150+ double initialDeposit = getDeposit (accountType );
123151 //We can create an account now;
124152 Account account ;
125153 if (accountType .equalsIgnoreCase ("checking" )) {
126154 account = new Checking (initialDeposit );
127- } else {
155+ } else if ( accountType . equalsIgnoreCase ( "savings" )) {
128156 account = new Savings (initialDeposit );
157+ } else {
158+ throw new InvalidAccountTypeException ();
129159 }
130160 Customer customer = new Customer (firstName , lastName , ssn , account );
131161 bank .addCustomer (customer );
132162 }
133163
164+ private double getAmount (String question ) {
165+ System .out .print (question );
166+ double amount = 0 ;
167+ try {
168+ amount = Double .parseDouble (keyboard .nextLine ());
169+ } catch (NumberFormatException e ) {
170+ amount = 0 ;
171+ }
172+ return amount ;
173+ }
174+
134175 private void makeADeposit () {
135176 int account = selectAccount ();
136177 if (account >= 0 ) {
137- System .out .print ("How much would you like to deposit?: " );
138- double amount = 0 ;
139- try {
140- amount = Double .parseDouble (keyboard .nextLine ());
141- } catch (NumberFormatException e ) {
142- amount = 0 ;
143- }
178+ double amount = getAmount ("How much would you like to deposit?: " );
144179 bank .getCustomer (account ).getAccount ().deposit (amount );
145180 }
146181 }
147182
148183 private void makeAWithdrawal () {
149184 int account = selectAccount ();
150185 if (account >= 0 ) {
151- System .out .print ("How much would you like to withdraw?: " );
152- double amount = 0 ;
153- try {
154- amount = Double .parseDouble (keyboard .nextLine ());
155- } catch (NumberFormatException e ) {
156- amount = 0 ;
157- }
186+ double amount = getAmount ("How much would you like to withdraw?: " );
158187 bank .getCustomer (account ).getAccount ().withdraw (amount );
159188 }
160189 }
@@ -168,22 +197,22 @@ private void listBalances() {
168197
169198 private int selectAccount () {
170199 ArrayList <Customer > customers = bank .getCustomers ();
171- if (customers .size () <= 0 ){
200+ if (customers .size () <= 0 ) {
172201 System .out .println ("No customers at your bank." );
173202 return -1 ;
174203 }
175204 System .out .println ("Select an account:" );
176205 for (int i = 0 ; i < customers .size (); i ++) {
177206 System .out .println ((i + 1 ) + ") " + customers .get (i ).basicInfo ());
178207 }
179- int account = 0 ;
208+ int account ;
180209 System .out .print ("Please enter your selection: " );
181210 try {
182211 account = Integer .parseInt (keyboard .nextLine ()) - 1 ;
183212 } catch (NumberFormatException e ) {
184213 account = -1 ;
185214 }
186- if (account < 0 || account > customers .size ()){
215+ if (account < 0 || account > customers .size ()) {
187216 System .out .println ("Invalid account selected." );
188217 account = -1 ;
189218 }
0 commit comments