研马原 2021-10-27 16:19 采纳率: 57.1%
浏览 72
已结题

Java面向对象多态性设计account账户类

img

img

  • 写回答

2条回答 默认 最新

  • Abzz 2021-10-27 17:05
    关注

    看看这个是不是你想要的。

    public class Account {
    
        // id
        private int id = 0;
    
        // balance
        private double balance = 0;
    
        // 年利率
        private double annualInterestRate = 0;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
        public double getannualInterestRate() {
            return annualInterestRate;
        }
    
        public void setannualInterestRate(double annualInterest) {
            this.annualInterestRate = annualInterest;
        }
    
        // 无参构造方法
        public Account() {}
    
        // id和初始余额的构造方法
        public Account(int id, double balance) {
            this.id = id;
            this.balance = balance;
        }
    
        // 返回月利率方法
        public double getMonthlyInterestRate(){
            return this.annualInterestRate / 12;
        }
    
        // 取款方法
        public void withDraw(double num){
            if(num > balance){
                System.out.println("金额不足,请修改金额");
                return;
            }
            this.balance = this.balance - num;
        }
    
        // 存款方法
        public void deposit(double num){
            if(num < 0){
                System.out.println("输入金额错误,请重新输入金额");
                return;
            }
            this.balance = this.balance + num;
        }
    
        public static void main(String[] args) {
            Account account = new Account(1122, 2000);
            account.setannualInterestRate(0.045);
            // 取款2500
            account.withDraw(2500);
            // 存款3000
            account.deposit(3000);
            System.out.println("月利率:" + account.getMonthlyInterestRate() + ",余额:" + account.getBalance());
            // 再取款25000
            account.withDraw(25000);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月5日
  • 已采纳回答 10月28日
  • 创建了问题 10月27日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测