研马原 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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分