Skip to content

Instantly share code, notes, and snippets.

@krismuniz
Last active February 2, 2018 15:14
Show Gist options
  • Save krismuniz/e968077f862ec543c667922bc1795573 to your computer and use it in GitHub Desktop.
Save krismuniz/e968077f862ec543c667922bc1795573 to your computer and use it in GitHub Desktop.
Example: simple Java classes for a class I took
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());
}
}
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