发布网友 发布时间:2024-10-23 19:46
共1个回答
热心网友 时间:2分钟前
Customer类:
package Test;
public class Customer {
private String customerId;//客户身份证号
private String custName;//客户姓名
private String custGender;//客户性别
public Customer(String customerId,String custName,String custGender){
this.custGender=custGender;
this.custName= custName;
this.customerId= customerId;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustGender() {
return custGender;
}
public void setCustGender(String custGender) {
this.custGender = custGender;
}
public void printMSG(){
System.out.println("身份证号:"+this.customerId+"姓名:"+this.custName+"性别:"+this.custGender);
}
}
Account类:
package Test;
public class Account {
private String customerId;//该账户所属客户的身份证号
private String accountNo;//账户号
private float balance;//余额
public Account(String customerId,String accountNo,float balance){
this.customerId= customerId;
this.accountNo = accountNo;
this.balance = balance;
}
public Account(String customerId,String accountNo){
this(customerId,accountNo,0);
}
public float deposit(float amount) {
System.out.println("本次存款:"+amount);
balance= balance+amount;
return amount;
}
public float withdraw (float amount){
System.out.println("本次取款:"+amount);
balance= balance-amount;
if(balance>=0){
return amount;
}else{
return 0;
}
}
public float getBalance(){
return balance;
}
}
Ex4_3类:
package Test;
public class Ex4_3 {
public static void main(String [] args){
//创建两个客户“张三”、“李四”;为张三开设2个账户
//(一个账户开户时就存款2000,另一账户开户时不存款)、
//李四开设1个账户(开户时存款3000)。
Customer customer1 = new Customer("111","张三","男");
Customer customer2 = new Customer("222","李四","女");
Account account1 = new Account(customer1.getCustomerId(),"zhangsan1",2000);
Account account2 = new Account(customer1.getCustomerId(),"zhangsan2");
Account account3 = new Account(customer2.getCustomerId(),"lisi",3000);
System.out.println("张三第一个账户取款500:");
account1.withdraw(500);
System.out.println("张三从第一个账户转账1000到第二个账户中:");
account2.deposit(account1.withdraw(1000));
System.out.println("李四取款300:");
account3.withdraw(300);
System.out.println("最后查询出张三与李四的客户信息及账户余额:");
customer1.printMSG();
System.out.println("账户1余额:"+account1.getBalance()+"账户2余额:"+account2.getBalance());
customer2.printMSG();
System.out.println("李四账户1余额"+account3.getBalance());
}
}