类成员的使用,模拟银行账户功能。属性:账号、储户姓名、地址、存款余额、最小余额;方法:存款、取款、查询。

根据用户操作 显示储户相关信息。

如存款操作后,显示储户原有余额、今日存款数额和最终存款余额;

取款时,若最后余额小于最小余额,拒绝取款,并显示“至少保留余额XXX”。

public class Account {
	 private String account;//私有类变量
     String name;
     String address;
     double balance;
     static double minBalance=10;//静态常量
  
     public String getAccount() {//获取
             return account;
     }

     public void setAccount(String account) {
             this.account = account;
     }
     
     public Account(String account,String name){//带两个参数的构造方法
                       this.account=account;
                       this.name=name;
                       //this.address=address;
                       //this.balance=balance;
             }
     
     public Account(String account,String name,String address,double balance){//带四个参数的构造方法
               this(account,name);//调用带两个参数的构造方法,只能放在第一语句位置
               this.address=address;
               this.balance=balance;
     }
     //定义存款操作函数
     public void deposit(double x){
             System.out.println("原有余额是:"+balance);
             System.out.println("今日存款为:"+x);
             balance+=x;
             System.out.println("最新余额为:"+balance);
     }
     //定义取款操作函数
     public void  withDraw(double x){
             System.out.println("您当下取款的额度为:"+x);
             double y=balance-x;
             if(y<minBalance){
                     System.out.println("拒绝取款,至少保留余额为:"+minBalance);
             }else {
                     balance=y;
             }
     }
     //定义余额输出函数
     public void  query(){
             System.out.println("您的余额是:"+balance);
     }
}

测试程序 

public class AccountTest {

	public static void main(String[] args) {
		 System.out.println("调用带2个参数的构造方法的结果为:");
         Account ac1=new Account("123456","lisi");//对象的声明创建
         ac1.address = "安宁街";//成员变量名的调用
         ac1.balance = 50;
         ac1.deposit(200);//成员方法的调用
         ac1.withDraw(240);
         ac1.query();
         System.out.println("调用带4个参数构造方法的结果为:");
         Account ac=new Account("654321","zhangsan","文苑街",100);
         ac.deposit(200);
         ac.withDraw(290);
         ac.query();

	}

}

 

 

  • 6
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一阶原点矩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值