新手编程 初次使用面向对象编程,对象、构造方法、实例方法

1.写一个名为Account的类模拟账户。
该类的属性和方法如下所示。
该类包括的属性:账户id,余额balance,年利率annualInterestRate;
包含的方法:各属性的set和get方法。取款方法withdraw(),存款方法deposit()

2.写一个测试程序
(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

Customer.java

public class Customer{
	//姓名
	private String name;

	//......

	//Account ..

	public Customer(){
	
	}

	public Customer(String n){
		name = n;
	}

	public String getName(){
		return name;
	}

	public void setName(String n){
		name = n;
	}
}

Account.java

public class Account{
	//id
	private String id;
		
	//balance
	private double balance;

	//年利率
	private String annualInterestRate;

	public Account(){
	
	}

	public Account(String i,double b,String a){
		id = i;
		balance = b;
		annualInterestRate = a;
	}

	public String getId(){
		return id;
	}

	public double getBalance(){
		return balance;
	}

	public String getAnnualInterestRate(){
		return annualInterestRate;
	}

	public void setId(String i){
		id = i;
	}

	public void setBalance(double b){
		balance = b;
	}

	public void setAnnualInterestRate(String a){
		annualInterestRate = a;
	}

	public void withdraw(double b){
		if(balance - b < 0){
			System.out.println("余额不足,取钱失败");
			return;
		}
		balance -= b;
		System.out.println("成功取出:" + b);
	}

	public void deposit(double b){
		balance += b;
		System.out.println("成功存入:" + b);
	}

	//public void sendImfomation(){
		
	//}
}

AccountTest.java

public class AccountTest02{
	public static void main(String[] args){
		Customer c1 = new Customer("Jane Smith");
		//System.out.println(c1.getName());
		Account a2 = new Account("1000",2000,"1.23%");

		a2.deposit(100);
		a2.withdraw(960);
		a2.withdraw(2000);

		System.out.println("Customer[" + c1.getName() + "] has a account : id is " + a2.getId() + ", annualInterestRate is " + a2.getAnnualInterestRate() + ", balance is " + a2.getBalance());
	}
}

运行结果:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值