Java基础 创建一个账户Account类,该类有id:账户号码(长整数),password:账户密码,name:真实姓名

题目:
创建一个账户Account类,该类有id:账户号码(长整数),password:账户密码,name:真实姓名,personId:身份证号码 字符串类型,email:客户的电子邮箱,balance:账户余额.方法:deposit: 存款方法,参数是double型的金额;withdraw:取款方法,参数是double型的金额.构造方法:有参和无参,有参构造方法用于设置必要的属性。

账户Account类

public class Account {
	public long id;
	public String password;
	public String name;
	public String personId;
	public String email;
	public double balance;

	// 存钱
	public double deposit(double money) {
		balance += money;
		return balance;
	}

	// 取钱
	public double withdraw(double money) {
		balance -= money;
		return balance;
	}

	// 无参构造
	public Account() {}

	// 有参构造
	public Account(long id, String password, String name, String personId,
			String email, double balance) {
		this.id = id;
		this.password = password;
		this.name = name;
		this.personId = personId;
		this.email = email;
		this.balance = balance;
	}
}

测试类

class AccountTest {
	public static void main(String[] args) {
		System.out.println("----------无参构造------------");
		Account account = new Account();
		account.id = 1001;
		account.password = "123456";
		account.name = "司马懿";
		account.personId = "410648023207122014";
		account.email = "1881533@163.com";
		account.balance = 6666.00;
		testFun(account);

		System.out.println("\n----------有参构造------------");
		Account account1 = new Account(1002, "root", "诸葛亮",
				"410648023104172014", "1545151@qq.com", 1024);
		testFun(account1);

	}

	// 测试方法
	static void testFun(Account obj) {
		System.out.println("id:" + obj.id);
		System.out.println("密码:" + obj.password);
		System.out.println("名字:" + obj.name);
		System.out.println("身份证号:" + obj.personId);
		System.out.println("邮箱:" + obj.email);
		System.out.println("余额" + obj.balance);
		System.out.println("------无参构造方法调用--------");
		System.out.println("存款后账户余额为:" + obj.deposit(1024));
		System.out.println("取款后账户余额为:" + obj.withdraw(1024));
	}
}

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

  • 8
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
,一个名为balance的double型余额属性,一个名为annualInterestRate的double型年利率属性,一个带有三个参数(id、balance和annualInterestRate)的Account构造函数,以及一个名为withdraw的方法来从账户中取款,一个名为deposit的方法来向账户存款。 以下是实现代码: ```java 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 void withdraw(double amount) { if (amount > balance) { System.out.println("Insufficient balance!"); } else { balance -= amount; System.out.println("$" + amount + " has been withdrawn from account " + ID); } } public void deposit(double amount) { balance += amount; System.out.println("$" + amount + " has been deposited to account " + ID); } } ``` 使用示例: ```java Account account = new Account(123456, 1000.0, 0.01); System.out.println("Account ID: " + account.getID()); System.out.println("Account balance: $" + account.getBalance()); System.out.println("Annual interest rate: " + account.getAnnualInterestRate() * 100 + "%"); account.withdraw(200.0); account.deposit(500.0); System.out.println("Account balance: $" + account.getBalance()); ``` 输出结果: ``` Account ID: 123456 Account balance: $1000.0 Annual interest rate: 1.0% $200.0 has been withdrawn from account 123456 $500.0 has been deposited to account 123456 Account balance: $1300.0 ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值