In this lecture, I learned about a trick to quickly generate getter and setter methods. Once you’ve declared the variables in the class, you highlight them then go to code > generate > getter and setter. Then you highlight the fields and click ok. This automatically creates code such as the following, which saves a lot of typing:

public long getAccountNumber() {
    return accountNumber;
}

public void setAccountNumber(long accountNumber) {
    this.accountNumber = accountNumber;
}

Here is the program I wrote:

public class Main {

    public static void main(String[] args) {

    BankAccount bobsAccount = new BankAccount(1234, 0, "Bob Brown", "myemail@bob.com", 5678);
        System.out.println(bobsAccount.getPhoneNumber());
        System.out.println(bobsAccount.getBalance());


    bobsAccount.withdrawl(100.0);

    bobsAccount.deposit(50.0);
    bobsAccount.withdrawl(100.0);

    bobsAccount.deposit(51);
    bobsAccount.withdrawl(100.0);

    }
}
package com.tomburnside;

public class BankAccount {

    private long accountNumber;
    private int balance;
    private String customerName;
    private String email;
    private int phoneNumber;

    public BankAccount(){
        System.out.println("Empty constructor called");
    }

    public BankAccount(long accountNumber, int balance, String customerName, String email, int phoneNumber){
        System.out.println("Account constructor with parameters called");
        this.accountNumber = accountNumber;
        this.balance = balance;
        this.customerName = customerName;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

    public void deposit(double depositAmount){
        this.balance += depositAmount;
        System.out.println("Deposit of " + depositAmount + " made. New balance is " + this.balance);
    }

    public void withdrawl(double withdrawlAmount){
        if(balance - withdrawlAmount < 0){
            System.out.println(balance + " available. Withdrawl not processed");
        }
        else{
            balance -= withdrawlAmount;
            System.out.println("Withdrawl of " + withdrawlAmount + " processed. Remaining balance = " + balance);
        }

    }

    public long getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(long accountNumber) {
        this.accountNumber = accountNumber;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    public String getCustomerName()  {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(int phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

So there’s the main class and the BankAccount class. The bank account class has 5 variables. And getter and setter methods which I used using the generate button.

Additionally, BankAccount has methods for depositing and withdrawing from the bank account.

Finally, there is a constructor (or maybe 2). This makes it easier to declare new bank accounts in the main class?

This is exactly what I did in the main class: declare a new bank account and I was able to populate the five fields in the paratheses of the constructor?

Then I printed out values of this by referencing the get methods in the bank account class. This is done because they are public whereas the variables declared in that class were private.

Finally I test the deposit and withdrawl methods.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s