public class SavingsAccount {
int balance;
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
public void checkBalance(){
System.out.println("Hello!");
System.out.println("Your balance is "+balance);
}
public void deposit(int amountToDeposit){
balance = amountToDeposit + balance;
System.out.println("You just deposited " + amountToDeposit);
}
public int withdraw(int amountToWithdraw){
balance = balance - amountToWithdraw;
System.out.println("You just withdrew " + amountToWithdraw);
return amountToWithdraw;
}
public String toString(){
return "This is a savings account with " + balance + " saved.";
}
public static void main(String[] args){
SavingsAccount savings = new SavingsAccount(2000);
//Check balance:
savings.checkBalance();
//Withdrawing:
savings.withdraw(300);
//Check balance:
savings.checkBalance();
//Deposit:
savings.deposit(600);
//Check balance:
savings.checkBalance();
//Deposit:
savings.deposit(600);
//Check balance:
savings.checkBalance();
System.out.println(savings);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter