m0_68110536 2022-05-20 11:58 采纳率: 33.3%
浏览 46

Java抽象继承银行账户问题

信用类账户和储蓄类账户相互转账
方法transfer(Account a,double money)

  • 写回答

1条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-05-20 12:22
    关注
    
    public abstract class  AbstractAccount{
    
    
    }
    
    
    
    
    package Student;
    
    import java.util.Date;
    
    
    public class Account  extends AbstractAccount{
        private String id;
        private String name;
        private double balance;
        private Date dateCreated = new Date();
    
        public Account(String id, String name) {
            this.id = id;
            this.name = name;
        }
        // 构造对应的set和get方法
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
        public Date getDateCreated() {
            return dateCreated;
        }
    
        public void setDateCreated(Date dateCreated) {
            this.dateCreated = dateCreated;
        }
    
        void withdraw(double Moneywithdraw) {//构造取款方法
            if (balance - Moneywithdraw < 0) {//判断
                System.out.println("余额不足,不可以取款");
            } else {
                balance = balance - Moneywithdraw;
                System.out.println("账户余额:" + balance);
            }
        }
    
        void deposit(double Moneydeposit) {//构造存款方法
            balance = balance + Moneydeposit;
            System.out.println("账户余额:" + balance);
        }
    
        void transfer(Account a, double money) {//构造转账方法
            if (balance < money) {//判断
                System.out.println("余额不足,不可以转账");
            } else {
                balance = balance - money;
                System.out.println("账户余额:" + balance);
            }
        }
    
        public String toString() {
            return ("账号:" + id + "\t实名:" + name + "\t账户余额" + balance + "\t开户时间:" + dateCreated);
        }
    }
    
    

    测试类

    
    package Student;
    
    public class TestAccount {
    
        public static void main(String[] args) {
            Account p1 = new Account("200405216", "蜡笔小新");// 创建账户对象
            p1.deposit(3000);
            System.out.println(p1.toString());
            p1.withdraw(2500);
            System.out.println(p1.toString());// 创建另一个账户对象
            Account p2 = new Account("200205218", "胡图图");
            p1.transfer(p2, 100);
            System.out.println(p1.toString());
            System.out.println(p2.toString());
            // 测试取钱和转账时余额不足的情况
            p1.withdraw(2500);
            p1.transfer(p2, 100);
            System.out.println(p1.toString());
            System.out.println(p2.toString());
        }
    
    }
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月20日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。