java⼊门编程之银⾏存取款⼩程序
有趣的java小程序初学java,做课后练习遇到的题⽬。。。。
要求
1、写⼀个名为Account的类模拟账户。该类的属性和⽅法如下图所⽰。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的⽅法:访问器⽅法(getter和setter⽅法),取款⽅法withdraw(),存款⽅法deposit()。
Account
private int id
private double balance
private double annualInterestRate
public Account (int id, double balance, double
annualInterestRate )
public int getId()
public double getBalance()
public double getAnnualInterestRate()
public void setId( int id)
public void setBalance(double balance)
public void setAnnualInterestRate(double
annualInterestRate)
public void withdraw (double amount)
public void deposit (double amount)
提⽰:在提款⽅法withdraw中,需要判断⽤户余额是否能够满⾜提款数额的要求,如果不能,应给出提⽰。
2. 创建Customer类。
Customer
private String firstName
private String lastName
private Account account
public Customer(String f,String l)
public String getFirstName()
public String getLastName()
public Account getAccount()
public void setAccount(Account account)
a. 声明三个私有对象属性:firstName、lastName和account。
b. 声明⼀个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)
c. 声明两个公有存取器来访问该对象属性,⽅法getFirstName和getLastName返回相应的属性。
d. 声明setAccount ⽅法来对account属性赋值。
e. 声明getAccount ⽅法以获取account属性。
3.写⼀个测试程序。
1. 创建⼀个Customer ,名字叫 Jane Smith, 他有⼀个账号为1000,余额为2000元,年利率为 1.23% 的账户。
2. 对Jane Smith操作。
存⼊ 100 元,再取出960元。再取出2000元。
打印出Jane Smith 的基本信息
成功存⼊ :100.0
成功取出:960.0
余额不⾜,取款失败
Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%, balance is 1140.0
代码:
public class Customer {
public static void main(String[] args) {
Customer JaneSmith  =new Customer("Jane","Smith");//新建⼀个Customer类并命名
JaneSmith.setAccount(new Account(1000,2000,1.23));//给新建的类初始化Account信息
}
private String firstName;  //理解为姓⽒
private String lastName;
private Account account;  //每⼀个customer类都有account类,每个客户都有⾃⼰的账号
public Customer(String f,String l) { //带参数的构造器,直接给名字。
firstName=f;
lastName=l;
}
//get、set⽅法
public Account getAccount() {
return account;
}
public void setAccount(Account account) {  //在初始化⾃⼰的账户信息时,调⽤了account
/
/类中的⽅法,也让⾃⼰在account类中赋予其customer属性
this.account = account;
account.setCustomer(this);
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
}
class Account{
private Customer customer;        //注明这个账号是属于谁的
private int id;                  //id唯⼀标识符
private double balance;            //余额
private double annualInterestRate;  //利率,貌似没什么卵⽤在这个题⽬中
public void setCustomer(Customer customer) {
this.customer=customer;    // 给customer初始化
}
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 Account (int id, double balance, double annualInterestRate ) {
setId(id);
setBalance(balance);
setAnnualInterestRate(annualInterestRate);
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public void withdraw (double amount) {//取钱先验证,够了才能取,不够显⽰个⼈信息
if(this.balance>=amount) {
System.out.println("成功取出:"+amount);
this.balance-=amount;
}
else {
System.out.println("余额不⾜,取款失败");
show(this.customer);
}
}
public void deposit (double amount) {  //存钱任性存,存多少都不管,只管++
System.out.println("成功存⼊:"+amount);
this.balance+=amount;
}
public void show(Customer customer) {  //显⽰个⼈信息⽅法(告诉你没钱别取了)
System.out.println("Customer ["+FirstName()+","+LastName()+"]"+"id is "+Account().getId()+",annualInterestRate is"+cust  }
}
运⾏结果:
成功存⼊:100.0
成功取出:960.0
余额不⾜,取款失败
Customer [Jane,Smith]id is 1000,annualInterestRate is1.23%, balance is 1140.0⽬前没想到更好的⽅式,望指教

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。