Question

2. Write a class called ATMTransaction. This class has a variable balance and
account number.
It has three methods, checkBalance, deposit and withdraw along with constructors
getters and setters and a toString. Write a program that
creates an object of this class. I should be able to deposit and withdraw from
my account number based on my balance , but i shouldnt be able to change my
account number. Look for loopholes in your program, test it correctly, your
balance cannot be negative.

2. Write a class called ATMTransaction. This class has a variable balance and account number It has three methods, checkBalan

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class ATMTransaction {
   private int accountNumber;
   private double balance;

   public ATMTransaction(int aAccountNumber, double aBalance) {
       super();
       accountNumber = aAccountNumber;
       balance = aBalance;
   }

   public ATMTransaction(int aAccountNumber) {
       super();
       accountNumber = aAccountNumber;
   }

   public double checkBalance() {
       return balance;
   }

   public void deposit(double am) {
       balance += am;
   }

   public void withdraw(double am) {
       if (balance >= am) {
           balance -= am;
       } else {
           System.out.println("Low balance..can not withdraw");
       }
   }

   public double getBalance() {
       return balance;
   }

   public int getAccountNumber() {
       return accountNumber;
   }

   @Override
   public String toString() {
       return "AccountNumber : " + accountNumber + ", Balance=" + balance;
   }

  
}

public class TestATMTransaction {
   public static void main(String[] args) {
       ATMTransaction atm = new ATMTransaction(123, 500);
       atm.deposit(100);
       System.out.println("Balance : " + atm.checkBalance());
       atm.deposit(200);
       System.out.println("Balance : " + atm.checkBalance());
       atm.withdraw(500);
       System.out.println("Balance : " + atm.checkBalance());
       atm.withdraw(500);
       System.out.println("Balance : " + atm.checkBalance());
       System.out.println(atm);
   }
}

«terminated> TestATMTransaction [Java Application] C:\SoftPegaEclipse-win64-4.5.2.29 Balance: 600.0 Balance : 800.0 Balance 3

Add a comment
Know the answer?
Add Answer to:
2. Write a class called ATMTransaction. This class has a variable balance and account number. It...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /*Design a class BankAccount that has the following properties: * Account Number (should be static and increased every...

    /*Design a class BankAccount that has the following properties: * Account Number (should be static and increased every time an account is created. * An array of holders max 5 and a holder is a Person object * A int to rember how many holders * Balance (float) * Date opened * interest rate * Constructors * addHolder will add one more Person as a holder, if you reach 5 don't add. * Setters getters * method payInterest that add...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Look at the Account class below and write a main method in a different class to briefly experiment with some instances of the Account class.

    Look at the Account class below and write a main method in a different class to briefly experiment with some instances of the Account class.Using the Accountclass as a base class, write two derived classes called SavingsAccountand CurrentAccount.ASavingsAccountobject, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. ACurrentAccount object, in addition to the instance variables of an Account object, should have an overdraft limit variable. Ensure...

  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

  • Bank Accounts Look at the Account class Account.java and write a main method in a different...

    Bank Accounts Look at the Account class Account.java and write a main method in a different class to briefly experiment with some instances of the Account class. • Using the Account class as a base class, write two derived classes called SavingsAccount and CheckingAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. A CheckingAccount object, in addition to the attributes of an...

  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

  • Java - In NetBeans IDE: •Design an abstract class called BankAccount which stores the balance. –Implement...

    Java - In NetBeans IDE: •Design an abstract class called BankAccount which stores the balance. –Implement a constructor that takes the balance (float). –Provide abstract methods deposit(amount) and withdraw(amount) •Design a class called SavingsAccount which extends BankAccount. –Implement a constructor that takes the balance as input. –It should store a boolean flag active. If the balance at any point falls below $25, it sets the active flag to false. It should NOT allow the balance to fall below $0. –If...

  • c++ please    Define the class bankAccount to implement the basic properties of a bank account....

    c++ please    Define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member func- tions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 compo- nents of...

  • ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still...

    ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still an Account class, however it has an additional field and some additional functionality. But it will still retain all the behavior of the Account class. Write a class called GoldAccount. This class will have an additional private field called bonusPoints. This field will require no get or set methods. But it will need to be initialized to 100 in the constructor. Thereafter, after a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT