View the step-by-step solution to:

JAVA Hello, I am working on UMUC's CMIS 242 Project 2 which has changed and am having some issues. My main areas of concern are:

JAVA


Hello,

I am working on UMUC's CMIS 242 Project 2 which has changed and am having some issues. My main areas of concern are:


  1. adding the accountNumber and balance to arrays (or objects) and being able to call the balance based on the user entered accountNumber


2) being able to access the savings and checking accounts together and to be able to call the various actionPerformed methods between them


I should be able to fix the rest once I have these 2 difficult (for me) parts out of the way


Text file layout:

7623, S, Jones, Phyllis, 2000.00, 0

7623, C, Jones, Phyllis, 1200.00, 2

etc.


Below is a mismatch of code from examples and previous versions that should give a pretty good idea of what I am intending. If you have any questions feel free to comment, I will be checking regularly.

public class ATM {
    private static ArrayList<Account> arrayChecking = new ArrayList<>();
    private static ArrayList<Account> arraySavings = new ArrayList<>();
    
    public static class Gui extends JFrame {
        public void readFile() {
            try {
                BufferedReader inputStream = new BufferedReader(new FileReader("PRJ2Accounts.txt"));
                Scanner input = new Scanner(inputStream);
                String inputLine;
                String[] values;

                while(input.hasNext()) {
                    inputLine = input.nextLine();
                    values = inputLine.split(", ");
                    int accountNumber = Integer.parseInt(values[0]);
                    String accountType = values[1];
                    String lastName = values[2];                                      //unused?
                    String firstName = values[3];
                    double balance = Double.parseDouble(values[4]);
                    int withdraws = Integer.parseInt(values[5]);

                    if (accountType.equals("S")) {
                        arraySavings.add(new Account().new Savings(accountNumber, balance)); 
                    } 

                    if (accountType.equals("C")) {
                        arrayChecking.add(new Account().new Checking(accountNumber, balance)); 
                    } //end of if
                } //end of while 
            } //end of try
            catch (IOException io) {
                System.out.println("File not found or unreadable");
            } //end of catch
        } //end of readFile()
        
        private static DecimalFormat currency = new DecimalFormat("$0.00");
        
        JButton inquireBalance, deposit, withdraw, transfer;
        JTextField accountEntry, amountEntry; 
        JLabel entry1, entry2, entry2a, entry3, result1;
        ButtonGroup radios = new ButtonGroup();
        JRadioButton checkingRadio = new JRadioButton("Checking");
        JRadioButton savingsRadio = new JRadioButton("Savings");
        
        public Gui() {     
            // Edited out a bunch of unneccessary layout junk
            entry1 = new JLabel ("Enter Account # (four digits) ");
            accountEntry = new JTextField(10);
            inquireBalance = new JButton("Request Balance");  // some adds edited out 
            entry2 = new JLabel("Initial Balance");
            entry2a = new JLabel("  ");
            entry3 = new JLabel("Transaction Amount");
            amountEntry = new JTextField(10);
            add(checkingRadio, window);
            add(savingsRadio, window);
            add(amountEntry, window);            
            deposit = new JButton("Deposit");
            withdraw = new JButton("Withdraw");
            transfer = new JButton("Transfer To");
            result1 = new JLabel(" ");
            
            event mathAction = new event();
            inquireBalance.addActionListener(mathAction);
            deposit.addActionListener(mathAction);           
            withdraw.addActionListener(mathAction);
        } //end of Gui Constructor
        
        class event implements ActionListener {
            private int accountNumEntered, accountIndex;
            private double amountEntered, balance, newBalance;
            
            public void actionPerformed (ActionEvent mathAction) {
                String operators = mathAction.getActionCommand();   // Action caused by click of button
                
                if (operators.equals("Request Balance")) {
                    accountNumEntered = Integer.parseInt(accountEntry.getText());
                    int match = 0;

                    for (int prefix = 0; prefix < 10000; prefix++) {
                        int accountNo = values ;
                        result1.setText(" ");

                        if (accountNo == accountNumEntered) {
                            balance = Double.parseDouble(aryValues[prefix][1]);                            
                            accountIndex = prefix;
                            entry2a.setText(Double.toString(balance));
                            entry2a.setForeground(Color.BLACK);
                            match = 1;
                        }// end  if (accountNo == accountNumEntered)
                        
                        if (match != 1) {
                            entry2a.setText(Integer.toString(0));
                            result1.setText("Account number does not exist. Please try again.");
                            result1.setForeground(Color.RED);
                        }// end if (match != 1)   
                    }// end  for()
                }//end  if (operators.equals("Request Balance"))
                
                if (operators.equals("Deposit")) {
                    amountEntered = Integer.parseInt(amountEntry.getText());
                    // Check if the amount entered is positive and increment of 20
                    if (getEntryValue() > 0 && getEntryValue() % 20 == 0) {
                        // Check radio button selection
                        if (checkingRadio.isSelected()) {
                            arrayChecking.deposit(getEntryValue());
                            result1.setText(currency.format(getEntryValue()) + 
                                            " deposited into Checking.");
                        } 
                        else if (savingsRadio.isSelected()) {
                                savings.deposit(getEntryValue());
                                result1.setText(currency.format(getEntryValue()) +
                                                " deposited into Savings.");
                        }
                        clearEntryValue();
                    } //end of check negative and increment
                    else errorValidNumber();
                    clearEntryValue();
                    newBalance =  Double.parseDouble(aryValues[accountIndex][1]);                
                    balance = newBalance;                    
                    String newSBalance = Double.toString(newBalance);
                    result1.setText("The new Balance for Account " + aryValues[accountIndex][0] + 
                                    " is " + newSBalance);
                    result1.setForeground(Color.BLUE);
                }// end if (operators.equals("Deposit"))
                
                if (operators.equals("Withdraw")) {
                    amountEntered = Integer.parseInt(amountEntry.getText());
                    try {
                        // Check for negative number and increment of 20
                        if (getEntryValue() > 0 && getEntryValue() % 20 == 0) {
                            // Check radio button selection
                            if (checkingRadio.isSelected()) {
                                checking.withdraw(getEntryValue());
                                result1.setText(currency.format(getEntryValue()) +
                                                " withdrawn from Checking.");
                            } 
                            else if (savingsRadio.isSelected()) {
                                savings.withdraw(getEntryValue());
                                result1.setText(currency.format(getEntryValue()) +
                                                " withdrawn from Savings.");
                            }
                            clearEntryValue();
                        } //end of check negative and increment
                        else errorValidNumber();
                        clearEntryValue();
                    } //end of try
                    catch (InsufficientFunds insufficientFunds) {
                        System.out.println("Caught in Insufficient Funds");
                    } //end of catch
                    newBalance =  Double.parseDouble(aryValues[accountIndex][1]);     
                    balance = newBalance;                    
                    String newSBalance = Double.toString(newBalance);
                    result1.setText("The new Balance for Account " + aryValues[accountIndex][0] + 
                                    " is " + newSBalance);
                    result1.setForeground(Color.BLUE);
                }// end if (operators.equals("Withdraw"))
                
                if (operators.equals("Transfer To")) {
                    try {
                        // Checks for positive number
                        if (getEntryValue() > 0) {
                            // Check radio button selection
                            if (checkingRadio.isSelected()) {
                                // Separate methods for transferFrom and transferTo
                                savings.transferFrom(getEntryValue());
                                checking.transferTo(getEntryValue());
                                result1.setText(currency.format(getEntryValue()) +
                                                " transferred from Savings to Checking.");
                            } 
                            else if (savingsRadio.isSelected()) {
                                checking.transferFrom(getEntryValue());
                                savings.transferTo(getEntryValue());
                                result1.setText(currency.format(getEntryValue()) +
                                                " transferred from Checking to Savings.");
                            }
                            clearEntryValue();
                        } //end of check positive number
                        else errorValidNumber();
                        clearEntryValue();
                    } //end of try 
                    catch (InsufficientFunds insufficientFunds) {
                        System.out.println("Caught in insufficient funds");
                    } //end of catch
                } //end of if(operators.equals("Transfer To"))
            } //end actionPerformed()
        } //end class event
        
        public void errorValidNumber() {
            result1.setText("Please enter a valid amount. " +
                            "n remember to please use $20 increments.");
        }
        
        // This method returns the text in the text entry field as a double
        public double getEntryValue() {
            try {
                return Double.parseDouble(amountEntry.getText());
            } 
            catch (NumberFormatException e) {
                System.out.println("Caught in getEntryValuen");
                clearEntryValue();
                return 0;
            }
        } //end of getEntryValue()

        // Clears the text entry field
        public void clearEntryValue() {
            amountEntry.setText("");
        } //end of clearEntryValue()
    } //end class Gui
    
    static class Account {
        int accountNumber;
        double balance;
        int withdraws; 
        private double charge = 1.50;

        // Default constructor for account is needed for the sub classes
        public Account() {
            
        }

        // Sets the balance of the account
        public void setBalance(double balance) {
            this.balance = balance;
        }
        
        // Sets the number of withdraws from the account
        public void setWithdraws(int withdraws) {
            this.withdraws = withdraws;
        }

        // Returns the balance of the account
        public double getBalance() {
            return this.balance;
        }

        // Subclass for Checking accounts
        public class Checking extends Account {
            public Checking(int accountNumber, double balance) {
                this.accountNumber = accountNumber;
                this.balance = balance;
            }
        }

        // Subclass for Savings accounts
        public class Savings extends Account {
            public Savings(int accountNumber, double balance) {
                this.accountNumber = accountNumber;
                this.balance = balance;
            }
        }

        // Method for Withdraw action listener
        public void withdraw(double withdrawAmount) throws InsufficientFunds {
            // If requested amount brings balance to less than 0, throws error
            if (this.balance - withdrawAmount < 0) {
                throw new InsufficientFunds();
            }
            this.balance = this.balance - withdrawAmount;
            withdraws++;

            if (withdraws >=3) {
             JOptionPane warning = new JOptionPane();
                result1.setText(" You have exceeded the number of "
                     + "free withdrawals. A $1.50 service charge will be applied"
                     + " to you next withdrawal.");
            }
            if (withdraws >=4) {
                this.balance = this.balance - charge;
            }
        } //end of withdraw()

        //Method for Deposit action listener
        public void deposit(double depositAmount) {
            this.balance = this.balance + depositAmount;
        }

        //Method for Transfer To action listener this will add the amount to the current balance
        public void transferTo(double transferAmount) {
            this.balance = this.balance + transferAmount;
        }

        //Method for Transfer To action listener this will subtract the amount from the current balance
        public void transferFrom(double transferAmount) throws InsufficientFunds {
            // Checks to ensure sufficient funds
            if (this.balance - transferAmount < 0) {
                throw new InsufficientFunds();
            }
            this.balance = this.balance - transferAmount;
        } //end of transferFrom() 
    } //end of class Account
    
    static class InsufficientFunds extends Exception {
        //Exception is thrown for when the account balance is less than 0
        public InsufficientFunds() {
            result1.setText("Sorry, you have insufficient funds for this transaction");
        } //end of InsufficientFunds()
    } //end of class InsufficientFunds

    public static void main(String[] args) {
        Gui program = new Gui();
        program.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        program.setLocationRelativeTo(null);
        program.setVisible(true);
        program.setSize(500, 400);
        program.setTitle("ATM: Welcome");
    } //end of main()
} //end of ATM

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question