import java.util.Scanner;
class Account{//创建一个账号
String id;//账号id
double balance;//账号余额
public void save(double money){ //存款
if (money>0){
balance += money;
}else {
System.out.println("参数有误");
}
}
public void withdraw(double money){ //取款
if (money<0){
System.out.println("参数有误");
}else if(money>balance){
System.out.println("余额不足");
}else{
balance -= money;
}
}
public void transfer(double money){ //转账
if (money>balance){
System.out.println("余额不足");
}else {
balance -= money;
}
}
public void accept(double money){ //接款
balance +=money;
}
}
class Customer{//客户类
String name;//姓名
String tel;//手机号
String cid;//身份证 X 10
Account acc;//客户的账号
}
class BankClerk{ //银行类
//Customer c 客户类的对象 具体的某个人
// Account a 创建了一个账号 a 账号类的对象 具体的某一个账户
public void open(Customer c, Account a){
//客户和账号进行绑定
c.acc = a;
}
}
public class Day14Task02 {
public static void main(String[] args) {
//创建一个客户对象
Scanner input = new Scanner(System.in);
Customer c1 = new Customer();
c1.name ="顺团";
c1.tel = "10086";
c1.cid = "1010";
//创建一个账户
Account a1 = new Account();
a1.id = "001";
a1.balance = 200.0;
//创建一个银行对象
BankClerk b1 = new BankClerk();
b1.open(c1,a1);//客户和账户进行绑定 c1.acc = a1
System.out.println("客户名:"+c1.name+ ",电话:" + c1.tel +
",身份证号:" + c1.cid + ",账号id是:"+c1.acc.id+
",余额:" + c1.acc.balance);
//第二个客户
Customer c2 = new Customer();
c2.name ="顺团2号";
c2.tel = "10010";
c2.cid = "0101";
//创建一个账户
Account a2 = new Account();
a2.id = "002";
a2.balance = 100.0;
//创建一个银行对象
BankClerk b2 = new BankClerk();
b2.open(c2,a2);//客户和账户进行绑定 c1.acc = a1
System.out.println("客户名:"+c2.name+ ",电话:" + c2.tel +
",身份证号:" + c2.cid + ",账号id是:"+c2.acc.id+
",余额:" + c2.acc.balance);
c1.acc.withdraw(500); //取钱
System.out.println("客户名:"+c1.name+"余额:"+ c1.acc.balance);
c1.acc.save(500); //存钱
System.out.println("客户名:"+c1.name+"余额:"+ c1.acc.balance);
System.out.println("请输入转账金额:");
double a = input.nextDouble();
System.out.println("客户1转账给客户2请选择 1");
System.out.println("客户2转账给客户1请选择 2");
int b = input.nextInt();
switch (b){
case 1:
if (a<= a1.balance) {
c1.acc.transfer(a);
c2.acc.accept(a);
}else{
System.out.println("余额不足");
}break;
case 2:
if (a<=a2.balance){
c2.acc.transfer(a);
c1.acc.accept(a);
}else {
System.out.println("余额不足");
}break;
}
//最后打印客户相关信息
System.out.println("客户名:"+c1.name+ ",电话:" + c1.tel +
",身份证号:" + c1.cid + ",账号id是:"+c1.acc.id+
",余额:" + c1.acc.balance);
System.out.println("客户名:"+c2.name+ ",电话:" + c2.tel +
",身份证号:" + c2.cid + ",账号id是:"+c2.acc.id+
",余额:" + c2.acc.balance);
}
}
Java基础:用类方法实现两个客户转账
最新推荐文章于 2024-05-11 14:53:44 发布