· 6 years ago · Nov 14, 2019, 12:08 AM
1import java.io.*;
2import java.util.Scanner;
3
4public class hw7 {
5
6 public static void main(String[] args) throws IOException //added throw clause
7 {
8 //constant definition
9 final int MAX_NUM = 10;
10
11 //declare arrays
12 int[] acctNumArray = new int[MAX_NUM];
13 double[] balanceArray = new double[MAX_NUM];
14
15 //declare variables
16 char choice;
17 int numAccts;
18
19 //create scanner class
20 Scanner keyboard = new Scanner(System.in);
21
22 //create an PrintWriter reference variable to an output file PrintWriter object
23 PrintWriter outputFile = new PrintWriter("myoutput.txt");
24
25 //call method to read data
26 numAccts = readAccts(acctNumArray, balanceArray, MAX_NUM);
27
28 //call method to print initial database of accounts and balances
29 printAccts(acctNumArray, balanceArray, numAccts, outputFile);
30 outputFile.println();
31
32 //do while loop
33 do {
34 menu();
35 choice = keyboard.next().charAt(0);
36 switch (choice) {
37 case 'Q':
38 case 'q':
39 printAccts(acctNumArray, balanceArray, numAccts, outputFile);
40 break;
41 case 'W':
42 withdrawal(acctNumArray, balanceArray, numAccts, outputFile, keyboard);
43 break;
44 case 'D':
45 deposit(acctNumArray, balanceArray, numAccts, outputFile, keyboard);
46 break;
47 case 'N':
48 numAccts = newAcct(acctNumArray, balanceArray, numAccts, outputFile, keyboard);
49 break;
50 case 'B':
51 balance(acctNumArray, balanceArray, numAccts, outputFile, keyboard);
52 break;
53 default:
54 outputFile.println("An error has occured");
55 menu();
56 choice = keyboard.next().charAt(0);
57 break;
58 }
59 } while (choice != 'Q' && choice != 'q');
60
61 //close the files
62 keyboard.close();
63 outputFile.close();
64 }
65
66 /*Input:
67 *acctNum - reference to an array of account numbers
68 *balance- reference to an array of account balances
69 *numAccts - number of account elements in the array
70 *myout - reference to the output file
71 * Process:
72 *makes a table to store all customers account numbers and balances
73 * Output:
74 *prints a two column table of customer account number and balance
75 */
76 public static void printAccts(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outputFile)
77 {
78 outputFile.println("\t\tInitial database of accounts and balances");
79 outputFile.println("\tAccount \tBalance");
80 //use for loop to print account number and balances
81 for (int count = 0; count < numAccts; count++)
82 {
83 outputFile.printf("%7d, $%7.2f", acctNumArray[count], balanceArray[count]);
84 outputFile.println();
85 }
86 }
87 /* Input:
88 * none
89 * Process:
90 * display different types of interactions that can be used
91 * Output:
92 * prints the command you want to use
93 */
94 public static void menu() {
95 System.out.println("\tSelect one of the following:\n\n"
96 + "W Representing Withdrawal\n"
97 + "D Representing Deposit\n"
98 + "N Representing New account\n"
99 + "B Representing Balance\n"
100 + "Q Indicating the user wants to quit the program\n");
101 }
102 /*Input:
103 *acctNum - reference to an array of account numbers
104 *balance - reference to an array of account balances
105 *maxAccts - the max amount of accounts that can be filled up
106 * Process:
107 *reads in data values of account numbers and balance
108 * Output:
109 *prints the actual number of accounts read in
110 */
111 public static int readAccts(int[] acctNumArray, double[] balanceArray, int maxAccts) throws IOException //add throw clause
112 {
113 //local variable declaration
114 int count = 0;
115
116 //file
117 Scanner inputFile = new Scanner(System.in);
118
119 //read in account number and balance arrays and count number of accounts read in
120 while (inputFile.hasNext() && count < maxAccts) {
121
122 acctNumArray[count] = inputFile.nextInt();
123 balanceArray[count] = inputFile.nextDouble();
124 count++;
125 }
126 //close the input file
127 inputFile.close();
128
129 return count;
130 }
131 /*Input:
132 *acctNum - reference to an array of integer account numbers
133 *numAccts - the number of account elements in the array
134 *account - array of integer account numbers
135 * Process:
136 *finds the account number if the account exists, or if it doesn’t exist
137 * Output:
138 *prints the index of account in acctNum array if the account exists, and -1 if it doesn’t.
139 */
140 public static int findAcct(int[] acctNumArray, int numAccts, int account)
141 {
142 for (int count = 0; count < numAccts; count++) {
143 if (acctNumArray[count] == account) {
144 return count;
145 }
146 }
147 return -1;
148 }
149
150 /*Input:
151 *acctNum - reference to an array of account numbers
152 *balance - reference to an array of account balances
153 *numAccts - number of account elements in the array
154 * Process:
155 *prompts the id the user entered, and prints an error if account is invalid. Then proceeds
156to identify the balance
157 * Output:
158 *prints the current account balance
159 */
160 public static void balance(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outputFile, Scanner inputFile)
161 {
162 int account; //declare variable
163 int count;
164
165 System.out.println("Enter an account number: ");
166 account = inputFile.nextInt();
167
168 //check to see if account exists by calling method
169 count = findAcct(acctNumArray, numAccts, account);
170
171 //if-else statement
172 if (count == -1) {
173 outputFile.println("Transaction Type: Balance");
174 outputFile.println("Error the account number" + account + " does not exist\n\n");
175 } else {
176 outputFile.println("Transaction Type: Balance");
177 outputFile.println("Account number" + account);
178 outputFile.printf("Current Balance: $%.2f, balanceArray[index]\n\n");
179 }
180
181 }
182 /*Input:
183 *acctNum - reference to an array of integer account numbers
184 *balance - reference to an array of account balances
185 *num_accts - the number of account elements in the array
186 * Process:
187 *finds the account if it exists, and the amount of money being deposited
188 * Output:
189 *prints the amount of money being deposited, or an error message due to invalid acct
190 */
191 public static void deposit(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outputFile, Scanner inputFile)
192 {
193 int account; //declare variables
194 int count;
195 double amounttodeposit;
196
197 System.out.print("Enter an account number: ");
198 account = inputFile.nextInt();
199
200 //check to see if the account exists
201 count = findAcct(acctNumArray, numAccts , account);
202
203 //if-else statement
204 if (count == -1) {
205 outputFile.println("Transaction Type: Deposit");
206 outputFile.println("Error, the account you entered" + account + "does not exist\n\n");
207 } else {
208 System.out.println("Enter the amount to deposit: ");
209 amounttodeposit = inputFile.nextDouble();
210 if (amounttodeposit < 0.00) {
211 outputFile.println("Transaction Type: Deposit");
212 outputFile.println("Account number " + account);
213 outputFile.println("Amount to Deposit" + amounttodeposit);
214 outputFile.println("Error, can not deposit this amount of money");
215 } else {
216 outputFile.println("Transaction Type: Deposit");
217 outputFile.println("Account number" + account);
218 outputFile.println("Current Balance: $%.2f, balanceArray[count]\n\n");
219 outputFile.println("Amount to Deposit" + amounttodeposit);
220
221 outputFile.printf("New Balance: $%.2f, balanceArray[count]\n\n");
222 }
223 }
224}
225 /*Input:
226 *acctNum - reference to an array of integer account numbers
227 *balance - reference to an array of account balances
228 *numAccts - the number of account elements in the array
229 * Process:
230 *finds account number if it exists, the amount of money being withdrawn, and if there is
231sufficient funds
232 * Output:
233 *prints the amount of money withdrawn, or an error message saying non sufficient funds
234 */
235 public static void withdrawal(int[] acctNumArray, double[] balanceArray, int numAccts,PrintWriter outputFile,Scanner inputFile)
236 {
237 //declare variables
238 int account;
239 int count;
240 double amounttowithdraw = 0;
241
242 System.out.println("Enter an account number: ");
243 account = inputFile.nextInt();
244 //check to see if the account exists
245 count = findAcct(acctNumArray, numAccts , account);
246
247 //if-else statement
248 if (count == -1) {
249 outputFile.println("Transaction Type: Withdrawal");
250 outputFile.println("Error, the account you entered does not exist");
251 } else {
252 outputFile.println("Transaction Type: Withdrawal");
253 System.out.println("Enter the amount to withdraw: ");
254 amounttowithdraw = inputFile.nextDouble();
255 }
256 if (amounttowithdraw > balanceArray[count] && amounttowithdraw < 0) {
257 outputFile.println("Transaction Type: Withdraw");
258 outputFile.println("Account number " + account);
259 outputFile.println("Amount to Withdraw" + amounttowithdraw);
260 outputFile.println("Error, can not withdraw this amount of money\n\n");
261 }
262 else {
263 balanceArray[count] -= amounttowithdraw;
264 outputFile.println("Transaction Type: Withdraw");
265 outputFile.println("Account number " + account);
266 outputFile.println("Current Balance: $%.2f, balanceArray[count]\n\n");
267 outputFile.println("Amount to withdraw" + amounttowithdraw);
268 outputFile.printf("New Balance: $%.2f, balanceArray[count]\n\n");
269 }
270 }
271 /*Input:
272 *acctNum - reference to an array of integer account numbers
273 *balance - reference to an array of account balances
274 *numAccts - the number of account elements in the array
275 * Process:
276 *prompts id the user entered and checks to see if account is already taken. If it is, prints
277an error message, and if not, adds the account to acctNum array
278 * Output:
279 *prints a new account with an initial balance of 0
280 */
281 public static int newAcct(int[] acctNum, double[] balance, int numAccts, PrintWriter outputFile, Scanner keyboard)
282 {
283 int newaccount;
284 int count = 0;
285
286 System.out.println("Enter a new account number: ");
287 newaccount = keyboard.nextInt();
288
289 if (count == newaccount) {
290 outputFile.println("Transaction Type: New Account");
291 outputFile.println("Error, the account you entered already exist");
292
293 return numAccts;
294
295 }
296 if (newaccount < 0 || newaccount > 99999) {
297 outputFile.println("Transaction Type: New Account");
298 outputFile.println("Error, the account number you entered does not exist");
299
300 return numAccts;
301
302 }
303
304
305 acctNum[numAccts] = newaccount;
306 double balanceArray = 0.00;
307
308 outputFile.println("Transaction Type: New Account");
309 outputFile.println("account" + newaccount);
310
311 return ++numAccts;
312 }
313
314}