package day14;
import java.util.Scanner;
class Account{ // 创建一个账号
String id; // 账号id
double balance; // 账户余额
public void save(double money){ // 存钱方法
if(money > 0){ // 输入的参数 需要大于0
balance += money;
}else{
System.out.println("参数有误");
}
}
public void withdran(double money){ // 取钱方法
if(money < 0){
System.out.println("参数有误");
} else if (money > balance) {
System.out.println("余额不足");
}else {
balance -= money;
}
}
public void rollout(double money){
if(money > balance){
System.out.println("余额不足");
}else{
balance -= money;
}
}
public void collection(double money){
balance += money;
}
}
class Customer{ // 客户类
String name; // 姓名
String tel; // 手机号
String cid; // 身份证号 x 10
Account account; // 客户的账户
}
class BankClerk{
// Customer c 客户类的对象 具体的某个人
// Account a 创建了一个账户a 账户类的对象 具体的某一个账户
public void open(Customer c, Account a, Customer c2, Account a2){
// 客户和账户进行绑定
c.account = a;
c2.account = a2;
}
}
public class Day14_Method_Exer06 {
public static void main(String[] args) {
java.util.Scanner input = new Scanner(System.in);
// 创建一个客户对象
Customer c1 = new Customer();
Customer c2 = new Customer();
c1.name = "Lynn";
c2.name = "You";
c1.tel = "119"; // 电话
c1.cid = "21410"; // 身份证号
// 创建一个账户
Account a1 = new Account();
Account a2 = new Account();
a1.id = "007";
a1.balance = 0;
a2.id = "001";
a2.balance = 0;
// 创建一个银行对象 让他们进行绑定
BankClerk b1 = new BankClerk();
b1.open(c1, a1, c2, a2); // 客户和账户进行绑定 c1.account = a1
System.out.println("客户名:" + c1.name + "; 账户id是:" + c1.account.id);
System.out.println("客户名:" + c2.name + "; 账户id是:" + c2.account.id);
// 取钱
c1.account.withdran(500);
// 存钱
c1.account.save(1000);
System.out.println(c1.account.balance);
// 取钱
c1.account.withdran(800);
System.out.println(c1.account.balance);
// 转账
System.out.println("请输入你要转账的金额:");
double a = input.nextDouble();
if(a < c1.account.balance){
c1.account.rollout(a);
c2.account.collection(a);
}else {
System.out.println("余额不足");
}
System.out.println("客户名:" + c1.name + "; 账户id是:" + c1.account.id + "; 余额是:" + c1.account.balance);
System.out.println("客户名:" + c2.name + "; 账户id是:" + c2.account.id + "; 余额是:" + c2.account.balance);
}
}
打印银行类,创建银行,实现存钱,取钱,转账
于 2022-09-22 23:35:07 首次发布
本文介绍了如何使用Java实现一个简单的银行系统,涉及账户类的存钱、取款、转账功能,以及客户与账户的绑定。通过实例展示了如何操作账户并进行转账操作。
1327

被折叠的 条评论
为什么被折叠?



