zoukankan      html  css  js  c++  java
  • 2021.11.24(状态模式java)

    银行账户

    Java代码模拟实现课堂上的银行账户的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。

    (1)Account.java

    package shiyan22;
    
    
    /**
    
    
     * @Description:环境类
    
    
     * @author 马荣荣
    
    
     *
    
    
     */
    
    
    public class Account {
    
    
    private AccountState state;
    
    
    private String owner;
    
    
    public Account(String a,double b){
    
    
    owner=a;
    
    
    state=new Yellowstate(this,b);
    
    
    state.stateCheck();
    
    
    }
    
    
    public void setsta(AccountState a){
    
    
    state=a;
    
    
    }
    
    
    public void deposit(double a){
    
    
    System.out.println(owner+"存"+a+"元");
    
    
    state.deposit(a);
    
    
    state.stateCheck();
    
    
    System.out.println();
    
    
    }
    
    
    public void withdraw(double a){
    
    
    System.out.println(owner+"取"+a+"元");
    
    
    state.withdraw(a);
    
    
    state.stateCheck();
    
    
    System.out.println();
    
    
    }
    
    
    }

    (2)AccountState.java

    package shiyan22;
    
    
    /**
    
    
     * @Description:抽象状态类
    
    
     * @author 马荣荣
    
    
     *
    
    
     */
    
    
    public abstract class AccountState {
    
    
    protected Account acc;
    
    
    protected double balance;
    
    
    public AccountState(Account a,double b){
    
    
    super();
    
    
    acc=a;
    
    
    balance=b;
    
    
    }
    
    
    public abstract void stateCheck();
    
    
    public void deposit(double amount) {
    
    
    balance+=amount;
    
    
    System.out.println("存款成功");
    
    
    System.out.println("余额为"+balance+"元");
    
    
    }
    
    
    public void withdraw(double amount) {
    
    
    if((balance-amount)>-1000) {
    
    
    System.out.println("取钱成功!");
    
    
    balance -= amount;
    
    
    System.out.println("当前余额:"+balance+"元");
    
    
    }else {
    
    
    System.out.println("余额不足!");
    
    
    System.out.println("当前余额:"+balance+"元");
    
    
    }
    
    
    }
    
    
    }
    

     (3)Client.java

    package shiyan22;
    
    
    public class Client {
    
    
    public static void main(String args[]){
    
    
    Account acc=new Account("账户20194021",200);
    
    
    acc.deposit(3000);
    
    
    acc.withdraw(1000);
    
    
    acc.withdraw(3500);
    
    
    }
    
    
    }

    (4)Greenstate.java

    package shiyan22;
    
    
    /**
    
    
     * @Description:正常状态类
    
    
     * @author 马荣荣
    
    
     *
    
    
     */
    
    
    public class Greenstate extends AccountState{
    
    
    public Greenstate(Account a,double b){
    
    
    super(a,b);
    
    
    }
    
    
    public void stateCheck(){
    
    
    if(balance<-1000){
    
    
    acc.setsta(new Redstate(acc,balance));
    
    
    }else if(balance>=-1000&&balance<0){
    
    
    acc.setsta(new Yellowstate(acc,balance));
    
    
    }
    
    
    }
    
    
    }

    (5)Redstate.java

    package shiyan22;
    
    
    /**
    
    
     * @Description:透支状态类
    
    
     * @author 马荣荣
    
    
     *
    
    
     */
    
    
    public class Redstate extends AccountState {
    
    
     
    
    
    public Redstate(Account acc, double balance) {
    
    
    super(acc, balance);
    
    
    }
    
    
     
    
    
    public void stateCheck() {
    
    
    if(balance >= -1000 && balance < 0){
    
    
    acc.setsta(new Yellowstate(acc, balance));
    
    
    }else if(balance >= 0){
    
    
    acc.setsta(new Greenstate(acc,balance));
    
    
    }
    
    
    }
    
    
    public void withdraw(double amount) {
    
    
    System.out.println("余额不足!");
    
    
    System.out.println("当前余额:"+balance);
    
    
    }
    
    
    }

    (6)Yellowstate.java

    package shiyan22;
    
    
    /**
    
    
     * @Description:欠费状态类
    
    
     * @author 马荣荣
    
    
     *
    
    
     */
    
    
    public class Yellowstate extends AccountState {
    
    
     
    
    
    public Yellowstate(Account acc, double balance) {
    
    
    super(acc, balance);
    
    
    }
    
    
     
    
    
    public void stateCheck() {
    
    
    if(balance >=0 ){
    
    
    acc.setsta(new Greenstate(acc,balance));
    
    
    }else if(balance < -1000){
    
    
    acc.setsta(new Redstate(acc,balance));
    
    
    }
    
    
    }
    
    
    }

    实现截图:

  • 相关阅读:
    快速掌握GIT
    Codeigniter+PHPExcel导出Excel文件
    git结构拓扑图
    (转)MVC新手指南
    (转)jQuery插件开发 其实很简单
    (转)Asp.net缓存简介
    (转)让ASP.NET MVC页面返回不同类型的内容
    (转)2010 .NET面试题整理之基础篇
    (转) .NET对象的XML序列化和反序列化
    (转)2010面试攻略
  • 原文地址:https://www.cnblogs.com/marr/p/15598872.html
Copyright © 2011-2022 走看看