package com.jsu.bank;
//设计一个简单的银行账户管理系统,要求包括以下功能:
//
//每个账户具有账户号、账户持有人姓名、余额等属性。
//实现存款、取款、查询余额、转账等操作。
//要求能够创建新账户、关闭账户。
//实现一个银行类,包含一个账户列表,能够添加新账户、关闭账户、根据账户号查询账户、显示所有账户信息等功能。
//使用面向对象的思想设计程序结构,确保良好的封装性和扩展性。
public class BankCount {
private int bankID;
private String name;
private double money;
public BankCount() {
}
public BankCount(int bankID, String name, double money) {
this.bankID = bankID;
this.name = name;
this.money = money;
}
/**
* 获取
* @return bankID
*/
public int getBankID() {
return bankID;
}
/**
* 设置
* @param bankID
*/
public void setBankID(int bankID) {
this.bankID = bankID;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return money
*/
public double getMoney() {
return money;
}
/**
* 设置
* @param money
*/
public void setMoney(double money) {
this.money = money;
}
public String toString() {
return "账户ID:"+getBankID()+" 账户名称:"+getName()+" 账户余额:"+getMoney();
}
}
package com.jsu.bank;
import java.util.ArrayList;
//实现存款、取款、查询余额、转账等操作。
//要求能够创建新账户、关闭账户。
public class BankSystem {
ArrayList<BankCount> bankCounts = new ArrayList<>();
//添加账户
public void oddCount(BankCount count){
bankCounts.add(count);
System.out.println("账户添加成功");
}
//关闭账户
public void removeCount(BankCount count){
bankCounts.remove(count);
System.out.println("已关闭账户");
}
//实现存款
public void ToMoney(int ID,double money){
for (int i = 0; i < bankCounts.size(); i++) {
if(bankCounts.get(i).getBankID()==ID){
bankCounts.get(i).setMoney(bankCounts.get(i).getMoney()+money);
double num = bankCounts.get(i).getMoney();
System.out.println("存款成功,当前账户余额为:"+num);
}
else {
System.out.println("账户不存在");
}
}
}
//实现取款
public void RemoveMoney(int ID,double money){
for (int i = 0; i < bankCounts.size(); i++) {
if(bankCounts.get(i).getBankID()==ID){
if(bankCounts.get(i).getMoney()>=money) {
bankCounts.get(i).setMoney(bankCounts.get(i).getMoney() - money);
double num = bankCounts.get(i).getMoney();
System.out.println("取款成功,当前余额为:" + num);
}
else{
System.out.println("余额不足,请重新取款");
}
}
else {
System.out.println("账户不存在");
}
}
}
//查询余额
public void findMoney(int ID){
for (int i = 0; i < bankCounts.size(); i++) {
if(bankCounts.get(i).getBankID()==ID){
double num = bankCounts.get(i).getMoney();
System.out.println("当前余额为:"+num);
}
else {
System.out.println("账户不存在");
}
}
}
//实现转账功能
public void zhuanZhang(int ID1,int ID2,double money){
int a=0;
for (int i = 0; i < bankCounts.size(); i++) {
if(bankCounts.get(i).getBankID()==ID1){
if(bankCounts.get(i).getMoney()>=money) {
bankCounts.get(i).setMoney(bankCounts.get(i).getMoney() - money);
double num = bankCounts.get(i).getMoney();
System.out.println("转账成功,剩余余额为:"+num);
a=1;
}
else {
System.out.println("余额不足");
}
}
}
if(a==1) {
for (int i = 0; i < bankCounts.size(); i++) {
if(bankCounts.get(i).getBankID()==ID2){
bankCounts.get(i).setMoney(bankCounts.get(i).getMoney()+money);
}
else {
System.out.println("账户不存在");
}
}
}
}
//打印所有账户
public void allPoint(){
for (int i = 0; i < bankCounts.size(); i++) {
System.out.println(bankCounts.get(i).toString());
}
}
}
package com.jsu.bank;
public class Bank {
public static void main(String[] args) {
System.out.println("--------------------银行账户管理系统--------------------------");
System.out.println("1:创建新账户");
System.out.println("2:关闭账户");
System.out.println("3:存款");
System.out.println("4:取款");
System.out.println("5:查询余额");
System.out.println("6:转账");
BankCount count1=new BankCount(2001,"小明",1234);
BankCount count2=new BankCount(2002,"小军",234165);
BankSystem bankSystem = new BankSystem();
bankSystem.oddCount(count1);
bankSystem.oddCount(count2);
bankSystem.allPoint();
}
}