1. 源代码;
// Account.java
public class Account {
private String owner;
private AccountState state;
private double balance;
public Account(String owner, double initBalance) {
this.owner = owner;
this.balance = initBalance;
this.state = new GreenState(this, balance);
}
public void setState(AccountState state) {
this.state = state;
}
public AccountState getState() {
return state;
}
public String getOwner() {
return owner;
}
public void deposit(double amount) {
state.deposit(amount);
balance += amount;
state.stateCheck();
}
public void withdraw(double amount) {
state.withdraw(amount);
balance -= amount;
state.stateCheck();
}
public double getBalance() {
return balance;
}
}
// AccountState.java
abstract class AccountState {
protected Account acc;
protected double balance;
protected String stateName;
public AccountState(Account acc, double balance) {
this.acc = acc;
this.balance = balance;
}
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
public abstract void stateCheck();
}
// GreenState.java
class GreenState extends AccountState {
public GreenState(Account acc, double balance) {
super(acc, balance);
this.stateName = "Green State";
}
@Override
public void deposit(double amount) {
System.out.println("Deposit of $" + amount + " into " + acc.getOwner() + "'s account in Green State.");
}
@Override
public void withdraw(double amount) {
if (amount <= balance) {
System.out.println("Withdrawal of $" + amount + " from " + acc.getOwner() + "'s account in Green State.");
} else {
System.out.println("Withdrawal of $" + amount + " failed. Insufficient funds!");
acc.setState(new RedState(acc));
}
}
@Override
public void stateCheck() {
if (balance < 0) {
acc.setState(new RedState(acc));
}
}
}
// RedState.java
class RedState extends AccountState {
public RedState(Account acc) {
super(acc, acc.getBalance());
this.stateName = "Red State";
}
@Override
public void deposit(double amount) {
System.out.println("Deposit of $" + amount + " into " + acc.getOwner() + "'s account in Red State.");
balance += amount;
stateCheck();
}
@Override
public void withdraw(double amount) {
System.out.println("Withdrawal of $" + amount + " failed in Red State. Account is overdrawn!");
}
@Override
public void stateCheck() {
if (balance >= 0) {
acc.setState(new GreenState(acc, balance));
}
}
}
// YellowState.java (placeholder)
class YellowState extends AccountState {
public YellowState(Account acc) {
super(acc, acc.getBalance());
this.stateName = "Yellow State";
}
@Override
public void deposit(double amount) {
// Implement behavior for Yellow State
}
@Override
public void withdraw(double amount) {
// Implement behavior for Yellow State
}
@Override
public void stateCheck() {
// Implement state transition checks if necessary
}
}
// BankClient.java
public class BankClient {
public static void main(String[] args) {
Account account = new Account("John Doe", 100.0);
System.out.println("Account created for: " + account.getOwner() + " with initial balance: $" + account.getBalance());
// Test deposits
account.deposit(50.0); // Should succeed
System.out.println("Current balance: $" + account.getBalance());
// Test withdrawals
account.withdraw(30.0); // Should succeed
System.out.println("Current balance: $" + account.getBalance());
// Test withdrawal that overloads
account.withdraw(150.0); // Should fail and switch to RedState
System.out.println("Current balance: $" + account.getBalance());
// Test a deposit after overdraft
account.deposit(50.0); // Should succeed and move to GreenState
System.out.println("Current balance: $" + account.getBalance());
}
}