Question

In: Computer Science

• 1. Write a java program using methods to find the information about a account holder...

• 1. Write a java program using methods to find the information about a account holder at bank.

c) For every transaction make sure make use of his account balance.

Solutions

Expert Solution

// File: BankAccountTest2.java
class BankAccount
{

    private String accountNum;     // the account number
    private double balance;        // the amount on deposit

    public BankAccount(String acctNum, double initialBalance)
    {
        accountNum = acctNum;
        balance = initialBalance;
    }

    public void deposit(double amount)             // note "mutator" method
    {
        double newBalance = balance + amount;
        balance = newBalance;                      // modifies instance var                        
    }

    public void withdraw(double amount)            // note "mutator" method
    {
        double newBalance = balance - amount;
        balance = newBalance;                   // modifies instance var
    }

    public String getAccount()              // note "accessor" method
    {
        return accountNum;                // returns value of instance var
    }

    public double getBalance()             // note "accessor" method
    {
        return balance;                  // returns value of instance var
    }

    public void transferFundsA(BankAccount destination, double amount)
    {
        destination.balance = destination.balance + amount;
        // note explicit use of this to reference instance variables of the 
        // object for which the method was called
        this.balance = this.balance - amount;
    }

    public void transferFundsB(BankAccount destination, double amount)
    {
        destination.deposit(amount);    // deposit to "destination" account
        this.withdraw(amount);           // withdraw from this account
    }
}
//******************** end of BankAccount class definition ********************

/** A class to test the BankAccount2 class  */

public class BankAccountTest2
{

    public static void main(String[] args)
    {
        BankAccount first = new BankAccount("1111111", 20000);
        BankAccount second = new BankAccount("2222222", 30000);

        System.out.printf("Account #%s has initial balance of $%.2f%n",
                first.getAccount(), first.getBalance());

        System.out.printf("Account #%s has initial balance of $%.2f%n",
                second.getAccount(), second.getBalance());

        first.transferFundsA(second, 5000);

        System.out.println("\nAfter \"first.transferFunds(second, 5000)\" ...");
        System.out.printf("Account #%s has new balance of $%.2f%n",
                first.getAccount(), first.getBalance());

        System.out.printf("Account #%s has new balance of $%.2f%n",
                second.getAccount(), second.getBalance());

        second.transferFundsB(first, 10000);

        System.out.println("\nAfter \"second.transferFunds(first,10000)\" ...");
        System.out.printf("Account #%s has new balance of $%.2f%n",
                first.getAccount(), first.getBalance());

        System.out.printf("Account #%s has new balance of $%.2f%n",
                second.getAccount(), second.getBalance());
    }
}





/**************************** OR ***********************************/


public class AccountTesting implements Runnable {
        private Account acct = new Account();
        public static void main(String[] args) {
                AccountTesting r = new AccountTesting();
                Thread one = new Thread(r);
                Thread two = new Thread(r);
                one.setName("Ranjeet");
                two.setName("Reema");
                one.start();
                two.start();
        }
        @Override
        public void run() {
                for (int x = 0; x < 5; x++) {
                        makeWithdrawal(10);
                        if (acct.getBalance() < 0) {
                                System.out.println("account is overdrawn!");
                        }
                }
        }
        private void makeWithdrawal(int amt) {
                if (acct.getBalance() >= amt) {
                        System.out.println(Thread.currentThread().getName() + " is going to withdraw");
                        try {
                                Thread.sleep(100);
                        } catch (InterruptedException ex) {
                        }
                        acct.withdraw(amt);
                        System.out.println(Thread.currentThread().getName() + " completes the withdrawal");
                } else {
                        System.out.println("Not enough in account for " + Thread.currentThread().getName() + " to withdraw " + acct.getBalance());
                }
        }
}

class Account {
        private int balance = 50;
        public int getBalance() {
        return balance;
        }
        public void withdraw(int amount) {
        balance = balance - amount;
        }
}


Related Solutions

• 1. Write a java program using methods to find the information about a account holder...
• 1. Write a java program using methods to find the information about a account holder at bank. d) Perform the functionalities(Deposit, Withdraw and print) until the user selects to stop.
• 1. Write a java program using methods to find the information about a account holder...
• 1. Write a java program using methods to find the information about a account holder at bank. b) Show different functionalities of a bank account (Deposit, Withdrawal, Print)
Methods – Compute Grade Please write a complete Java program, given the following information about (a...
Methods – Compute Grade Please write a complete Java program, given the following information about (a few lines of code in) main: projectAverage = getAverage(”Project”); // returns average of 2 grades testAverage = getAverage(”Test”); // returns average of 2 grades displayGrade(projectAverage, testAverage); // test are 70% & projects 30%
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while loop To Understand what the Fibonacci series is: The Fibonacci sequence is a series of numbers where a number is the sum of the previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Q2: Write a Java program to find the Factorial of a number using a while loop. To...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and deposit. Test the methods in the main function.
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Java - Write a test program that creates an Account object with an account number of...
Java - Write a test program that creates an Account object with an account number of AC1111, a balance of $25,000, and an annual interest rate of 3.5. Use the withdraw method to withdraw $3,500, use the deposit method to deposit $3,500, and print the balance, the monthly interest, and the date when this account was created.
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT