top of page
Click here to go to the home page of AskTheCode.

OOP example in Java - Java Programming Solution | AskTheCode

Team ATC

A simple example of OOP in Java | Object-Oriented Programming Solution | Ask The Code

 

Sample Problem:

Create a new file called Account.java and construct a class called Account, which models a simple bank account. It contains the following members:

  1. Two private instance variables: accountNumber (int), and balance (double), which maintains the current account balance.

  2. Constructors (overloaded): Implement an overloaded constructor that takes in as input the account number and simply sets the balance to 3000.00. This prints "First Overloaded Constructor" as well.

  3. Implement another overloaded constructor that takes in an account number and a balance as arguments. This prints "Second Overloaded Constructor" as well.

  4. Getters for the instance variables.

  5. public methods: public void credit(double) - adds the given amount to the balance. public void debit(double) - subtracts the given amount from the balance (obviously, one cannot subtract more from what is available - prints "Insufficient Funds" if this is the case). public String toString() - returns "Account Number: xxx, Balance: Php xxx.xx", with balance rounded to two decimal places.

 

Input Format:

The first input is either a 1 or 2.

If it is a 1, it means to create an Account via the first overloaded constructor and an account number follows.

If it is a 2, 2 values follow, an account number and an amount in pesos. This is then followed by a number m representing the number of operations to be performed followed by the operations themselves as specified below:

1 : credit
2 : debit
3 : getAccountNumber
4 : getBalance
5 : toString

For both operations 1 and 2, an amount in pesos follows.

The other operations do not have inputs to them.



Input Sample:

1 1001 5
3
4
1 2000
5
2 7000
 

Output Format:

For the getter operations (3 - 4), including toString(), the values they return must be printed.

For debit(), when the amount to be deducted is bigger than the balance, print "Insufficient Funds", while credit() will not have any output.



Output Sample:

First Overloaded Constructor
1001
3000.0
Account Number: 1001, Balance: Php 5000.00
Insufficient Funds
 

Code:

import java.util.Scanner;

public class Account{
    private int accountNumber;
    private double balance;
    
    public Account(int accountNumber){
        this.accountNumber = accountNumber;
        this.balance = 3000.00;
        System.out.println("First Overloaded Constructor");
    }
    
    public Account(int accountNumber, double balance){
        this.accountNumber = accountNumber;
        this.balance = balance;
        System.out.println("Second Overloaded Constructor");
    }
    
    public void credit(double balance){
        // adds the given amount to the balance
        this.balance += balance;
    }
    public void debit(double balance){
        if (balance > this.balance)
            //if user want to debit balance > current balance
            System.out.println("Insufficient Funds");
            
        else
            // subtracts the given amount from the balance
            this.balance -= balance;
    }
    public int getAccountNumber(){
        return this.accountNumber;
    }
    public double getBalance(){
        return this.balance;
    }
    public String toString(){
        // returns "Account Number: xxx, Balance: Php xxx.xx", with balance rounded to two decimal places.
        return String.format("Account Number: %d, Balance: Php %.2f", this.accountNumber,
        this.balance);
    }

    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        
        Account acc = null;
        
        int acc_creation_method = in.nextInt();
        if (acc_creation_method == 1)
            acc = new Account(in.nextInt());
        else if (acc_creation_method == 2)
            acc = new Account(in.nextInt(), in.nextInt());
        
        int m = in.nextInt();
        for (int i=1; i<=m; i++){
            int choice = in.nextInt();
            
            if (choice == 1)
                acc.credit(in.nextDouble());
            else if (choice == 2)
                acc.debit(in.nextDouble());
                
            else if (choice == 3)
                System.out.println(acc.getAccountNumber());
            else if(choice == 4)
                System.out.println(acc.getBalance());
            else if (choice ==5)
                System.out.println(acc.toString());
        }
    }
}

Recent Posts

See All

Comments


bottom of page