Last active
February 2, 2018 15:14
-
-
Save krismuniz/e968077f862ec543c667922bc1795573 to your computer and use it in GitHub Desktop.
Example: simple Java classes for a class I took
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Bank { | |
public static void main (String[] args) { | |
BankAccount myAccount = new BankAccount(0, "Kristian"); | |
myAccount.deposit(10.20); | |
myAccount.withdraw(5.10); | |
System.out.println("Status: " + (myAccount.getIsActive() ? "Active" : "Inactve")); | |
System.out.println("Owner: " + myAccount.getOwner()); | |
System.out.println("Balance: " + myAccount.getBalance()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BankAccount { | |
int number; | |
String owner; | |
double balance = 0; | |
int points = 0; | |
boolean isActive = true; | |
/* CONSTRUCTOR */ | |
public BankAccount (int number, String owner) { | |
this.number = number; | |
this.owner = owner; | |
} | |
public BankAccount (int number, String owner, double balance, int points, boolean isActive) { | |
this.number = number; | |
this.owner = owner; | |
this.balance = balance; | |
this.points = points; | |
this.isActive = isActive; | |
} | |
/* TRANSACTIONAL METHODS */ | |
public void deposit (double amount) { | |
balance += amount; | |
} | |
public void withdraw (double amount) { | |
if (amount <= balance) { | |
balance -= amount; | |
} | |
} | |
/* GETTERS */ | |
public double getBalance () { | |
return balance; | |
} | |
public int getNumber () { | |
return number; | |
} | |
public String getOwner () { | |
return owner; | |
} | |
public int getPoints () { | |
return points; | |
} | |
public boolean getIsActive () { | |
return isActive; | |
} | |
/* SETTERS */ | |
public void setNumber (int number) { | |
this.number = number; | |
} | |
public void setOwner (String owner) { | |
this.owner = owner; | |
} | |
public void setPoints (int points) { | |
this.points = points; | |
} | |
public void setIsActive (boolean isActive) { | |
this.isActive = isActive; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment