· 6 years ago · Nov 13, 2019, 02:14 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 inputFile = 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
31 //do while loop
32 do {
33 menu();
34 choice = inputFile.next().charAt(0);
35 switch (choice) {
36 case 'Q':
37 case 'q':
38 printAccts(acctNumArray, balanceArray, numAccts, outputFile);
39 break;
40 case 'W':
41 withdrawal(acctNumArray, balanceArray, numAccts, outputFile, inputFile);
42 break;
43 case 'D':
44 deposit(acctNumArray, balanceArray, numAccts, outputFile, inputFile);
45 break;
46 case 'N':
47 newAcct(acctNumArray, balanceArray, numAccts, outputFile, inputFile);
48 break;
49 case 'B':
50 balance(acctNumArray, balanceArray, numAccts, outputFile, inputFile);
51 break;
52 default:
53 System.out.print("An error has occured");
54 menu();
55 choice = inputFile.next().charAt(0);
56 break;
57 }
58 } while (choice != 'Q' && choice != 'q');
59
60 //close the files
61 inputFile.close();
62 outputFile.close();
63
64 }
65 /*Input:
66 *acctNum - reference to an array of account numbers
67 *balance - reference to an array of account balances
68 *maxAccts - the max amount of accounts that can be filled up
69 * Process:
70 *reads in data values of account numbers and balance
71 * Output:
72 *prints the actual number of accounts read in
73 */
74 public static int readAccts(int[] acctNumArray, double[] balanceArray, int maxAccts) throws IOException //add throw clause
75 {
76 //local variable declaration
77 int count = 0;
78
79 //Create a scanner object to read the input file
80 Scanner inputFile = new Scanner(System.in);
81
82 //read in account number and balance arrays and count number of accounts read in
83 while (inputFile.hasNext() && count < maxAccts)
84 {
85 acctNumArray[count] = inputFile.nextInt();
86 balanceArray[count] = inputFile.nextDouble();
87 count++;
88 }
89 //close the input file
90 inputFile.close();
91
92 return count;
93 }
94
95 /*Input:
96 *acctNum - reference to an array of account numbers
97 *balance- reference to an array of account balances
98 *numAccts - number of account elements in the array
99 *myout - reference to the output file
100 * Process:
101 *makes a table to store all customers account numbers and balances
102 * Output:
103 *prints a two column table of customer account number and balance
104 */
105 public static void printAccts(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outputFile)
106 {
107 outputFile.println("\t\tInitial database of accounts and balances");
108 outputFile.println("\tAccount \tBalance");
109 //use for loop to print account number and balances
110 for (int count = 0; count < numAccts; count++)
111 {
112 outputFile.printf("%7d, $%7.2f", acctNumArray[count], balanceArray[count]);
113 outputFile.println();
114 }
115 }
116 /* Input:
117 * none
118 * Process:
119 * display different types of interactions that can be used
120 * Output:
121 * prints the command you want to use
122 */
123 public static void menu() {
124 System.out.println("\tSelect one of the following:\n\n"
125 + "W Representing Withdrawal\n"
126 + "D Representing Deposit\n"
127 + "N Representing New account\n"
128 + "B Representing Balance\n"
129 + "Q Indicating the user wants to quit the program\n");
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 outputFile.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 outputFile.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 outputFile.println("Enter an account number: ");
243 account = inputFile.nextInt();
244
245 //check to see if the account exists
246 count = findAcct(acctNumArray, numAccts , account);
247
248 //if-else statement
249 if (count == -1) {
250 outputFile.println("Transaction Type: Withdrawal");
251 outputFile.println("Error, the account you entered does not exist");
252 } else {
253 outputFile.println("Transaction Type: Withdrawal");
254 outputFile.println("Enter the amount to withdraw: ");
255 amounttowithdraw = inputFile.nextDouble();
256 }
257 if (amounttowithdraw > balanceArray[count] && amounttowithdraw < 0) {
258 outputFile.println("Transaction Type: Withdraw");
259 outputFile.println("Account number " + account);
260 outputFile.println("Amount to Withdraw" + amounttowithdraw);
261 outputFile.println("Error, can not withdraw this amount of money\n\n");
262 }
263 else {
264 balanceArray[count] -= amounttowithdraw;
265 outputFile.println("Transaction Type: Withdraw");
266 outputFile.println("Account number " + account);
267 outputFile.println("Current Balance: $%.2f, balanceArray[count]\n\n");
268 outputFile.println("Amount to withdraw" + amounttowithdraw);
269 outputFile.printf("New Balance: $%.2f, balanceArray[count]\n\n");
270 }
271 }
272 /*Input:
273 *acctNum - reference to an array of integer account numbers
274 *balance - reference to an array of account balances
275 *numAccts - the number of account elements in the array
276 * Process:
277 *prompts id the user entered and checks to see if account is already taken. If it is, prints
278an error message, and if not, adds the account to acctNum array
279 * Output:
280 *prints a new account with an initial balance of 0
281 */
282 public static int newAcct(int[] acctNum, double[] balance, int numAccts, PrintWriter outputFile, Scanner inputFile)
283 {
284 int newaccount;
285 int count = 0;
286
287 outputFile.println("Enter a new account number: ");
288 newaccount = inputFile.nextInt();
289
290 if (count == newaccount) {
291 outputFile.println("Transaction Type: New Account");
292 outputFile.println("Error, the account you entered already exist");
293
294 return numAccts;
295
296 }
297 if (newaccount < 0 || newaccount > 99999) {
298 outputFile.println("Transaction Type: New Account");
299 outputFile.println("Error, the account number you entered does not exist");
300
301 return numAccts;
302
303 }
304
305 int[] acctNumArray = null;
306 acctNumArray[numAccts] = newaccount;
307 double balanceArray = 0.00;
308
309 outputFile.println("Transaction Type: New Account");
310 outputFile.println("account" + newaccount);
311
312 return ++numAccts;
313 }
314
315}