Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class hw7 {
- public static void main(String[] args) throws IOException //added throw clause
- {
- //constant definition
- final int MAX_NUM = 10;
- //declare arrays
- int[] acctNumArray = new int[MAX_NUM];
- double[] balanceArray = new double[MAX_NUM];
- //declare variables
- char choice;
- int numAccts;
- //create scanner class
- Scanner inputFile = new Scanner(System.in);
- //create an PrintWriter reference variable to an output file PrintWriter object
- PrintWriter outputFile = new PrintWriter("myoutput.txt");
- //call method to read data
- numAccts = readAccts(acctNumArray, balanceArray, MAX_NUM);
- //call method to print initial database of accounts and balances
- printAccts(acctNumArray, balanceArray, numAccts, outputFile);
- //do while loop
- do {
- menu();
- choice = inputFile.next().charAt(0);
- switch (choice) {
- case 'Q':
- case 'q':
- printAccts(acctNumArray, balanceArray, numAccts, outputFile);
- break;
- case 'W':
- withdrawal(acctNumArray, balanceArray, numAccts, outputFile, inputFile);
- break;
- case 'D':
- deposit(acctNumArray, balanceArray, numAccts, outputFile, inputFile);
- break;
- case 'N':
- newAcct(acctNumArray, balanceArray, numAccts, outputFile, inputFile);
- break;
- case 'B':
- balance(acctNumArray, balanceArray, numAccts, outputFile, inputFile);
- break;
- default:
- System.out.print("An error has occured");
- menu();
- choice = inputFile.next().charAt(0);
- break;
- }
- } while (choice != 'Q' && choice != 'q');
- //close the files
- inputFile.close();
- outputFile.close();
- }
- /*Input:
- *acctNum - reference to an array of account numbers
- *balance - reference to an array of account balances
- *maxAccts - the max amount of accounts that can be filled up
- * Process:
- *reads in data values of account numbers and balance
- * Output:
- *prints the actual number of accounts read in
- */
- public static int readAccts(int[] acctNumArray, double[] balanceArray, int maxAccts) throws IOException //add throw clause
- {
- //local variable declaration
- int count = 0;
- //Create a scanner object to read the input file
- Scanner inputFile = new Scanner(System.in);
- //read in account number and balance arrays and count number of accounts read in
- while (inputFile.hasNext() && count < maxAccts)
- {
- acctNumArray[count] = inputFile.nextInt();
- balanceArray[count] = inputFile.nextDouble();
- count++;
- }
- //close the input file
- inputFile.close();
- return count;
- }
- /*Input:
- *acctNum - reference to an array of account numbers
- *balance- reference to an array of account balances
- *numAccts - number of account elements in the array
- *myout - reference to the output file
- * Process:
- *makes a table to store all customers account numbers and balances
- * Output:
- *prints a two column table of customer account number and balance
- */
- public static void printAccts(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outputFile)
- {
- outputFile.println("\t\tInitial database of accounts and balances");
- outputFile.println("\tAccount \tBalance");
- //use for loop to print account number and balances
- for (int count = 0; count < numAccts; count++)
- {
- outputFile.printf("%7d, $%7.2f", acctNumArray[count], balanceArray[count]);
- outputFile.println();
- }
- }
- /* Input:
- * none
- * Process:
- * display different types of interactions that can be used
- * Output:
- * prints the command you want to use
- */
- public static void menu() {
- System.out.println("\tSelect one of the following:\n\n"
- + "W Representing Withdrawal\n"
- + "D Representing Deposit\n"
- + "N Representing New account\n"
- + "B Representing Balance\n"
- + "Q Indicating the user wants to quit the program\n");
- }
- /*Input:
- *acctNum - reference to an array of integer account numbers
- *numAccts - the number of account elements in the array
- *account - array of integer account numbers
- * Process:
- *finds the account number if the account exists, or if it doesn’t exist
- * Output:
- *prints the index of account in acctNum array if the account exists, and -1 if it doesn’t.
- */
- public static int findAcct(int[] acctNumArray, int numAccts, int account)
- {
- for (int count = 0; count < numAccts; count++) {
- if (acctNumArray[count] == account) {
- return count;
- }
- }
- return -1;
- }
- /*Input:
- *acctNum - reference to an array of account numbers
- *balance - reference to an array of account balances
- *numAccts - number of account elements in the array
- * Process:
- *prompts the id the user entered, and prints an error if account is invalid. Then proceeds
- to identify the balance
- * Output:
- *prints the current account balance
- */
- public static void balance(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outputFile, Scanner inputFile)
- {
- int account; //declare variable
- int count;
- System.out.println("Enter an account number: ");
- account = inputFile.nextInt();
- //check to see if account exists by calling method
- count = findAcct(acctNumArray, numAccts, account);
- //if-else statement
- if (count == -1) {
- outputFile.println("Transaction Type: Balance");
- outputFile.println("Error the account number" + account + " does not exist\n\n");
- } else {
- outputFile.println("Transaction Type: Balance");
- outputFile.println("Account number" + account);
- outputFile.printf("Current Balance: $%.2f, balanceArray[index]\n\n");
- }
- }
- /*Input:
- *acctNum - reference to an array of integer account numbers
- *balance - reference to an array of account balances
- *num_accts - the number of account elements in the array
- * Process:
- *finds the account if it exists, and the amount of money being deposited
- * Output:
- *prints the amount of money being deposited, or an error message due to invalid acct
- */
- public static void deposit(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outputFile, Scanner inputFile)
- {
- int account; //declare variables
- int count;
- double amounttodeposit;
- outputFile.print("Enter an account number: ");
- account = inputFile.nextInt();
- //check to see if the account exists
- count = findAcct(acctNumArray, numAccts , account);
- //if-else statement
- if (count == -1) {
- outputFile.println("Transaction Type: Deposit");
- outputFile.println("Error, the account you entered" + account + "does not exist\n\n");
- } else {
- outputFile.println("Enter the amount to deposit: ");
- amounttodeposit = inputFile.nextDouble();
- if (amounttodeposit < 0.00) {
- outputFile.println("Transaction Type: Deposit");
- outputFile.println("Account number " + account);
- outputFile.println("Amount to Deposit" + amounttodeposit);
- outputFile.println("Error, can not deposit this amount of money");
- } else {
- outputFile.println("Transaction Type: Deposit");
- outputFile.println("Account number" + account);
- outputFile.println("Current Balance: $%.2f, balanceArray[count]\n\n");
- outputFile.println("Amount to Deposit" + amounttodeposit);
- outputFile.printf("New Balance: $%.2f, balanceArray[count]\n\n");
- }
- }
- }
- /*Input:
- *acctNum - reference to an array of integer account numbers
- *balance - reference to an array of account balances
- *numAccts - the number of account elements in the array
- * Process:
- *finds account number if it exists, the amount of money being withdrawn, and if there is
- sufficient funds
- * Output:
- *prints the amount of money withdrawn, or an error message saying non sufficient funds
- */
- public static void withdrawal(int[] acctNumArray, double[] balanceArray, int numAccts,PrintWriter outputFile,Scanner inputFile)
- {
- //declare variables
- int account;
- int count;
- double amounttowithdraw = 0;
- outputFile.println("Enter an account number: ");
- account = inputFile.nextInt();
- //check to see if the account exists
- count = findAcct(acctNumArray, numAccts , account);
- //if-else statement
- if (count == -1) {
- outputFile.println("Transaction Type: Withdrawal");
- outputFile.println("Error, the account you entered does not exist");
- } else {
- outputFile.println("Transaction Type: Withdrawal");
- outputFile.println("Enter the amount to withdraw: ");
- amounttowithdraw = inputFile.nextDouble();
- }
- if (amounttowithdraw > balanceArray[count] && amounttowithdraw < 0) {
- outputFile.println("Transaction Type: Withdraw");
- outputFile.println("Account number " + account);
- outputFile.println("Amount to Withdraw" + amounttowithdraw);
- outputFile.println("Error, can not withdraw this amount of money\n\n");
- }
- else {
- balanceArray[count] -= amounttowithdraw;
- outputFile.println("Transaction Type: Withdraw");
- outputFile.println("Account number " + account);
- outputFile.println("Current Balance: $%.2f, balanceArray[count]\n\n");
- outputFile.println("Amount to withdraw" + amounttowithdraw);
- outputFile.printf("New Balance: $%.2f, balanceArray[count]\n\n");
- }
- }
- /*Input:
- *acctNum - reference to an array of integer account numbers
- *balance - reference to an array of account balances
- *numAccts - the number of account elements in the array
- * Process:
- *prompts id the user entered and checks to see if account is already taken. If it is, prints
- an error message, and if not, adds the account to acctNum array
- * Output:
- *prints a new account with an initial balance of 0
- */
- public static int newAcct(int[] acctNum, double[] balance, int numAccts, PrintWriter outputFile, Scanner inputFile)
- {
- int newaccount;
- int count = 0;
- outputFile.println("Enter a new account number: ");
- newaccount = inputFile.nextInt();
- if (count == newaccount) {
- outputFile.println("Transaction Type: New Account");
- outputFile.println("Error, the account you entered already exist");
- return numAccts;
- }
- if (newaccount < 0 || newaccount > 99999) {
- outputFile.println("Transaction Type: New Account");
- outputFile.println("Error, the account number you entered does not exist");
- return numAccts;
- }
- int[] acctNumArray = null;
- acctNumArray[numAccts] = newaccount;
- double balanceArray = 0.00;
- outputFile.println("Transaction Type: New Account");
- outputFile.println("account" + newaccount);
- return ++numAccts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement