|
|
Summary: Simon continues his exploration of the Java language... with sample code.
The code of the class BankAccount.java to be used with MyBank.
import java.io.*;
public class BankAccount {
// data fields ================================
protected String name;
protected int balance = 0;
// methods ====================================
public String toString () {
return "Class BankAccount: " + name;
}
public void deposit (int value) {
if (value >= 0)
balance = balance + value;
else {
System.out.print("You cannot deposit" +
" a negative sum of money: ");
System.out.println(value);
}
}
public void withdraw (int value) {
if (value <= balance && value >= 0)
balance = balance - value;
else {
System.out.print("You cannot withdraw" +
" that sum of money: ");
System.out.println(value);
}
}
public String name () {
return name;
}
public int balance () {
return balance;
}
public void name (String newName) {
name = newName;
}
// constructors ===============================
public BankAccount () {
super();
}
public BankAccount (String n) {
name = n;
}
public BankAccount (int amount) {
balance = amount;
}
public BankAccount (String n, int amount) {
name = n;
balance = amount;
}
}
|
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
// 11
// 12
// 13
// 14
// 15
// 16
// 17
// 18
// 19
// 20
// 21
// 22
// 23
// 24
// 25
// 26
// 27
// 28
// 29
// 30
// 31
// 32
// 33
// 34
// 35
// 36
// 37
// 38
// 39
// 40
// 41
// 42
// 43
// 44
// 45
// 46
// 47
|
|