利用面向对象思想,写一个名为Account的类模拟账户,模拟存取钱系统。

有时候,不是因为你没有能力,也不是因为你缺少勇气,只是因为你付出的努力还太少,所以,成功便不会走向你。而你所需要做的,就是坚定你的梦想,你的目标,你的未来,然后以不达目的誓不罢休的那股劲,去付出你的努力,成功就会慢慢向你靠近。

导读:本篇文章讲解 利用面向对象思想,写一个名为Account的类模拟账户,模拟存取钱系统。,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

编写Java程序

该类的属性和方法如下所示。
该类包括的属性:账户id,余额balance,年利率annualInterestRate;
包含的方法:各属性的set和get方法。取款方法withdraw(),存款方法deposit()

写一个测试程序
(1)创建一个Customer,名字叫Jane Smith,他有一个账号为1000,余额为2000,年利率为1.23%的账户
(2)对Jane Smith操作:
存入100元,再取出960元,再取出2000。
打印Jane Smith的基本信息
信息如下显示:
成功存入:100
成功取出:960
余额不足,取钱失败
Customer [Smith,Jane] has a account :id is 1000 annualInterestRate is 1.23% balance is 1140.0

//账户类
class Account{
	private String id;
	private double balance;
	private double annualInterestRate;
	//无参构造
	public Account(){
	
	}
	//有参构造
	public Account(String id,double balance,double annualInterestRate){
		this.id=id;
		this.balance=balance;
		this.annualInterestRate=annualInterestRate;
	
	}
	public void setId(String id){
		this.id=id;

	}
	public String getId(){
		return id;
	}
	public void setBalance(double balance){
		this.balance=balance;

	}
	public double getBalance(){
		return balance;
	}
	public void setAnnualInterestRat(double annualInterestRate){
		this.annualInterestRate=annualInterestRate;

	}
	public double getAnnualInterestRate(){
		return annualInterestRate;
	}
	//取款
	public void withdraw(double money){
		if(money > getBalance()){
			System.out.println("余额不足,取钱失败");
			return;
		}
		this.setBalance(this.getBalance() - money);
		System.out.println("成功存入:"+money);
	
	}
	//存款
	public void deposit(double money){
		
		this.setBalance(this.getBalance()+ money);

		System.out.println("成功存入:"+money);

	
	}

}
//客户
class Customer{
	//名字
	private String name;
	//客户有一个账户才能存取钱
	Account act;

	public Customer(){
	
	}
	public Customer(String name,Account act){
		this.name=name;
		this.act=act;
	
	}
	public void setName(String name){
		this.name=name;

	}
	public String getName(){
		return name;

	}
	public void setAct(Account act){
		this.act=act;

	}
	public Account getAct(){
		return act;

	}

}
public class Text{
	public static void main(String[] args){
	Account a=new Account("1000",2000,1.23);

	Customer c1=new Customer("Jane Smith",a);
	c1.getAct().deposit(100);  //人有一个账户存取钱
	c1.getAct().withdraw(960);
	c1.getAct().withdraw(2000);
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/144947.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!