zoukankan      html  css  js  c++  java
  • Java 练习(super练习)

    银行账号,取钱,存钱,透支 练习

    Account.java

    package com.klvchen.exer2;
    
    public class Account {
    	
    	private int id;
    	private double balance;
    	private double annualInterestRate;
    	
    	public Account(int id, double balance, double annualInterestRate) {
    		this.id = id;
    		this.balance = balance;
    		this.annualInterestRate = annualInterestRate;
    	}
    
    	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 annualInterestRate) {
    		this.annualInterestRate = annualInterestRate;
    	}
    	
    	//返回月利率
    	public double getMonthlyInterest() {
    		return annualInterestRate / 12;
    	}
    	
    	//取钱
    	public void withdraw(double amount) {
    		if(balance >= amount ) {
    			balance -= amount;
    			return;
    		}
    		System.out.println("余额不足");
    	}
    	
    	//存钱
    	public void deposit(double amount) {
    		if(amount > 0) {
    			balance += amount;
    		}
    	}
    	
    
    }
    

    AccountTest.java

    package com.klvchen.exer2;
    
    public class AccountTest {
    	public static void main(String[] args) {
    		
    		Account acct = new Account(1122, 20000, 0.045);
    		
    		acct.withdraw(30000);
    		System.out.println("您的账户余额为: " + acct.getBalance());
    		acct.withdraw(2500);
    		System.out.println("您的账户余额为: " + acct.getBalance());
    		acct.deposit(3000);
    		System.out.println("您的账户余额为: " + acct.getBalance());
    		
    		System.out.println("月利率为: " + (acct.getMonthlyInterest() * 100 ) + "%");
    	}
    }
    

    CheckAccount.java

    package com.klvchen.exer2;
    
    public class CheckAccount extends Account {
    	private double overdraft;   //可透支限额
    
    	public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
    		super(id, balance, annualInterestRate);
    		this.overdraft = overdraft;
    		
    	}
    	
    	public double getOverdraft() {
    		return overdraft;
    	}
    
    	public void setOverdraft(double overdraft) {
    		this.overdraft = overdraft;
    	}
    
    	@Override
    	public void withdraw(double amount) {
    		if(getBalance() >= amount) {
    			super.withdraw(amount);
    		}else if(overdraft >= amount - getBalance()) { //透支额度+余额足够消费
    			
    			overdraft -= (amount - getBalance());
    			
    			//setBalance(0);
    			//或
    			super.withdraw(getBalance());
    		}else {
    			System.out.println("超过可透支限额!");
    		}
    	}
    }
    

    CheckAccountTest.java

    package com.klvchen.exer2;
    
    public class CheckAccountTest {
    	public static void main(String[] args) {
    		
    		CheckAccount acct = new CheckAccount(1122, 20000, 0.045, 5000);
    		
    		acct.withdraw(50000);
    		System.out.println("您的账号余额为: " + acct.getBalance());
    		System.out.println("您的可透支额度为: " + acct.getOverdraft());
    		
    		System.out.println("***********************************");
    		
    		acct.withdraw(18000);
    		System.out.println("您的账号余额为: " + acct.getBalance());
    		System.out.println("您的可透支额度为: " + acct.getOverdraft());
    		
    		System.out.println("***********************************");
    		
    		acct.withdraw(3000);
    		System.out.println("您的账号余额为: " + acct.getBalance());
    		System.out.println("您的可透支额度为: " + acct.getOverdraft());
    	}
    
    }
    

    运行结果:

  • 相关阅读:
    Vue3+Element-Plus-admin
    uniapp UI库推荐 uView官网/文档
    js 获取本月月初和月末时间操作
    el-dialog 无法弹出来
    Vue核心技术 Vue+Vue-Router+Vuex+SSR实战精讲
    sed 相关命令
    docker 启动 ubuntu
    vue 配置 history 模式
    typescript 相关
    fiddler 图片下载
  • 原文地址:https://www.cnblogs.com/klvchen/p/14430061.html
Copyright © 2011-2022 走看看