Java入门学习笔记(账户存取款设计)
程序员文章站
2022-07-10 21:07:37
题目:编写一个名为Account的类模拟账户给类的属性和方法如下所示,该类包含的属性:账户ID,余额balance,年利率annualInterestRate;包含的方法:各属性的set和get方法,取款方法withdraw(),存款方法deposit()写一个测试程序:1、创建一个Customer,名字叫james,他有一个账号为1000,余额为2000.年利率为1.23%,存入100元,再取出960元,再取出2000元。打印James的基本信息信息如下显示:成功存入:100成功取出:96...
题目:编写一个名为Account的类模拟账户
给类的属性和方法如下所示,该类包含的属性:账户ID,余额balance,年利率annualInterestRate;
包含的方法:各属性的set和get方法,取款方法withdraw(),存款方法deposit()
写一个测试程序:
1、创建一个Customer,名字叫james,他有一个账号为1000,余额为2000.年利率为1.23%,存入100元,再取出960元,再取出2000元。
打印James的基本信息
信息如下显示:
成功存入:100
成功取出:960
余额不足,取钱失败
题目来源:动力节点
解答
public class testcount{
public static void main(String[] args){
Account c = new Account("1000",2000,"1.23%");
Customer a = new Customer("james",c);
a.getAct().deposit(100);
a.getAct().withdraw(960);
a.getAct().withdraw(2000);
}
}
class Customer
{
private String name;
private 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;
}
}
class Account
{
private String Id;
private int balance;
private String annualInterestRate;
public Account()
{
}
public Account(String Id,int balance,String 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(int balance)
{
this.balance=balance;
}
public int Getbalance()
{
return balance;
}
public void SetannualInterestRate(String annualInterestRate)
{
this.annualInterestRate=annualInterestRate;
}
public String GetannualInterestRate()
{
return annualInterestRate;
}
public void withdraw(int i)//取款
{ if(balance<i)
{
System.out.println("余额不足,取钱失败");
}
else
{
balance=balance-i;
System.out.println("成功取出:"+i);
}
}
public void deposit(int i)//存款
{
balance=balance+i;
System.out.println("成功存入:"+i);
}
}
输出结果:
本文地址:https://blog.csdn.net/Zzzzybfly/article/details/107370739