简易银行系统
面向对象模拟银行管理系统: 一般银行账户分为:借记卡账户 和 信用卡账户
概念:
借记卡账户 DebitAccount :是指先存款后消费(或取现),没有透支功能的银行卡。即存储卡账户,余额必须大于0。
信用卡账户 CreditAccount :信用卡是一种非现金交易付款的方式, 是简单的信贷服务。在借记卡功能的基础上可以透支,但是有透支额度,即透支只能在一定的金额范围内透支。
信用额度是指银行在批准你的信用卡的时候给于你信用卡的一个最高透支的限额, 你只能在这个额度内刷卡消费,超过了这个额度就无法正常刷卡消费。
用JAVA代码描述银行的账户,以及账户的操作。
(1) 所有银行卡(Account)都具有以下的属性:卡号(id)、卡持有者姓名(name)、密码(password)、身份证号(personId)、邮箱(email)、余额(balance)、
(2) 所有银行卡(Account)都具有的行为:
存款: deposit(double money),所有卡都一样
取款: withdraw(double money),各种卡不一样
(3) 信用卡(CreditAccount)账户专有的属性:信用额度(ceiling)
行为: 对不同的卡来说 取款方法不一样
(4) 借记卡账户 DebitAccount:没有专有属性,与所有卡一样
行为: 对不同的卡来说 取款方法不一样
(5) 银行 Bank.java。银行中管理所有银行卡,包括借记卡和信用卡。
要求:保存在ArrayList集合中。即:银行(Bank)类中只有一个属性,即Account类型的ArrayList。
银行对外提供各种相关方法:
(1) 开户 即注册。包含除卡号之外的所有属性
(2) 登录 public Account login(long id, String password),凭卡号和密码登录
(3) 存款 public Account deposit(long id, double money) //传入卡号和金额,返回账户信息
(4) 取款 public Account withdraw(long id, double money)//传入卡号和金额,返回账户信息
(5) 设置某个账户的信用额度,
(6) 查询某个账户的余额
(7) 查询该行所有账户的信用额度的和(只有信用卡账户有信用额度)
(8) 查询该行所有账户的余额的和(所有账户都有余额)
(9) 按照用户的卡号查找账户
com.banksys.account
Account.java
package com.banksys.account;
/**
* 所有银行卡(Account)都具有以下的属性: 卡号(id)、 卡持有者姓名(name)、 密码(password)、 身份证号(personId)、
* 邮箱(email)、 余额(balance)
* */
public class Account {
static long id = 622202050;
private String name;
private String password1;
private String password2;
private long personId;
private String email;
private int balance;
private int ceiling = 1000;//信用卡专有属性,信用额度,默认1000
public int getCeiling() {
return ceiling;
}
public void setCeiling(int ceiling) {
this.ceiling = ceiling;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public long getPersonId() {
return personId;
}
public void setPersonId(long personId) {
this.personId = personId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
CreditAccount.java
package com.banksys.account;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.banksys.bank.Bank;
/**
* 信用卡模块 信用卡(CreditAccount)账户专有的属性:信用额度(ceiling)
* */
public class CreditAccount {
static Scanner input = new Scanner(System.in);
Account account = new Account();
Bank bank = new Bank();
public void creditAccount() {
System.out.println("请输入姓名:");
account.setName(input.next());
p: while (true) {
System.out.println("请输入密码:");
account.setPassword1(input.next());
System.out.println("请再次输入密码:");
account.setPassword2(input.next());
if (account.getPassword1().equals(account.getPassword2())) {
System.out.println("两次密码一致!");
break;
} else {
System.out.println("两次密码不一致,请重新输入!");
continue p;// 密码不一致,接着重新输入
}
}
System.out.println("请输入身份证号:");
account.setPersonId(input.nextLong());
q: while (true) {
System.out.println("请输入邮箱:");
account.setEmail(input.next());
String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(account.getEmail());
boolean isMatched = matcher.matches();
if (isMatched) {// 邮箱正则表达式匹配
System.out.println("邮箱格式正确!");
break;
} else {
System.out.println("邮箱不格式正确,请重新输入!");
continue q;
}
}
w: while (true) {
System.out.println("请输入预存款金额:");
account.setBalance(input.nextInt());
if (account.getBalance() > 0) {
break;
} else {
continue w;
}
}
account.getCeiling();
account.id++;
account.setId(account.getId());
System.out.println("恭喜您,注册成功!");
System.out.println("卡号:" + account.getId() + "\n" + "姓名:"
+ account.getName() + "\n" + "密码:" + account.getPassword1()
+ "\n" + "身份证号:" + account.getPersonId() + "\n" + "邮箱:"
+ account.getEmail() + "\n" + "预存金额:" + account.getBalance()
+ "\n" + "信用额度:" + account.getCeiling());
bank.AllCreditCards(account);
return;
}
}
DebitAccount.java
package com.banksys.account;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.banksys.bank.Bank;
/**
* 借记卡模块
* */
public class DebitAccount {
static Scanner input = new Scanner(System.in);
Account account = new Account();
Bank bank = new Bank();
public DebitAccount() {
// TODO Auto-generated constructor stub
}
public void debitAccount() {
System.out.println("请输入姓名:");
account.setName(input.next());
p: while (true) {
System.out.println("请输入密码:");
account.setPassword1(input.next());
System.out.println("请再次输入密码:");
account.setPassword2(input.next());
if (account.getPassword1().equals(account.getPassword2())) {
System.out.println("两次密码一致!");
break;
} else {
System.out.println("两次密码不一致,请重新输入!");
continue p;// 密码不一致,接着重新输入
}
}
System.out.println("请输入身份证号:");
account.setPersonId(input.nextLong());
q: while (true) {
System.out.println("请输入邮箱:");
account.setEmail(input.next());
String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(account.getEmail());
boolean isMatched = matcher.matches();
if (isMatched) {// 邮箱正则表达式匹配
System.out.println("邮箱格式正确!");
break;
} else {
System.out.println("邮箱不格式正确,请重新输入!");
continue q;
}
}
w: while (true) {
System.out.println("请输入预存款金额:");
account.setBalance(input.nextInt());
if (account.getBalance() > 0) {
break;
} else {
continue w;
}
}
account.id++;
account.setId(account.getId());
System.out.println("恭喜您,注册成功!");
System.out.println("卡号:" + account.getId()
+ "\n" + "姓名:" + account.getName()
+ "\n" + "密码:" + account.getPassword1()
+ "\n" + "身份证号:"+ account.getPersonId()
+ "\n" + "邮箱:" + account.getEmail()
+ "\n" + "预存金额:"+ account.getBalance());
bank.AllDebitCards(account);
return;
}
}
com.banksys.bank
Bank.java
package com.banksys.bank;
import java.util.ArrayList;
import com.banksys.account.Account;
/**
* 银行管理所有银行卡,包括借记卡和信用卡。
* 要求:保存在ArrayList集合中。即:银行(Bank)类中只有一个属性,即Account类型的ArrayList。
*/
public class Bank {
public Bank() {
}
// 保存信用卡信息
public static ArrayList<Account> allCreditCards = new ArrayList<Account>();
// 保存借记卡信息
public static ArrayList<Account> allDebitCards = new ArrayList<Account>();
// 将信用卡信息添加到银行系统
public void AllCreditCards(Account account) {
allCreditCards.add(account);
return;
}
// 将借记卡信息添加到银行系统
public void AllDebitCards(Account account) {
allDebitCards.add(account);
return;
}
}
LoginFail.java
package com.banksys.bank;
/**
* 登录失败
* @author Administrator
*
*/
public class LoginFail {
public LoginFail() {
// TODO Auto-generated constructor stub
}
public void loginFail(){
System.out.println("登录失败!");
}
}
LoginSuccess.java
package com.banksys.bank;
import java.util.Scanner;
import com.banksys.account.Account;
import com.banksys.function.Deposit;
import com.banksys.function.Search;
import com.banksys.function.Withdraw;
/**
* 登陆成功
*
* @author Administrator
*
*/
public class LoginSuccess {
public LoginSuccess() {
// TODO Auto-generated constructor stub
}
static Scanner input = new Scanner(System.in);
public Account account = new Account();
public void loginSuccess(Account account) {
while (true) {
System.out.println("请输入指令 1:取款 2:存款 3:查询余额 4:退出");
String select = input.next();
if(select.equals("1")){
new Withdraw().withdraw(account);
}else if(select.equals("2")){
new Deposit().deposit(account);
}else if(select.equals("3")){
new Search().searchUser(account);
}else if(select.equals("4")){
System.out.println("感谢使用!");
System.exit(0);
}else{
System.out.println("输入错误,请重新输入!");
}
}
}
}
Main.java
package com.banksys.bank;
import com.banksys.ui.MainUI;
/**
* 主函数
*
*/
public class Main {
public static void main(String[] args) {
MainUI mainUI = new MainUI();
mainUI.showMainUI();
}
}
com.banksys.function
Deposit.java
package com.banksys.function;
import java.util.Scanner;
import com.banksys.account.Account;
/**
* 存款模块
* */
public class Deposit {
public Deposit() {
// TODO Auto-generated constructor stub
}
static Scanner input = new Scanner(System.in);
public void deposit(Account account){
int money;
while (true) {
System.out.println("请输入存款金额!");
money = input.nextInt();
if (money > 0) {
account.setBalance(account.getBalance() + money);
System.out.println("您的余额为:" + account.getBalance());
return;
} else {
System.out.println("输入有误,请重新输入!");
}
}
}
}
EstablishAccount.java
package com.banksys.function;
import java.util.Scanner;
import com.banksys.account.CreditAccount;
import com.banksys.account.DebitAccount;
/**
* 开户模块
* */
public class EstablishAccount {
static Scanner input = new Scanner(System.in);
public EstablishAccount() {
// TODO Auto-generated constructor stub
}
public void establicAccount(){
while(true){
System.out.println("欢迎注册,请选择您要开户的类型 1:借记卡 2:信用卡:");
String userChoice = input.next();
if(userChoice.equals("1")){
new DebitAccount().debitAccount();
return;
}else if(userChoice.equals("2")){
new CreditAccount().creditAccount();
return;
}else{
System.out.println("输入错误,请重新输入!");
}
}
}
}
Login.java
package com.banksys.function;
import java.security.AllPermission;
import java.util.Scanner;
import com.banksys.account.Account;
import com.banksys.bank.Bank;
import com.banksys.bank.LoginFail;
import com.banksys.bank.LoginSuccess;
/**
* 登陆模块
* */
public class Login {
public Account account = new Account();
Bank bank = new Bank();
static Scanner input = new Scanner(System.in);
public Login() {
}
public void login() {
System.out.println("请输入卡号:");
long cardNum = input.nextLong();
System.out.println("请输入密码:");
String mima = input.next();
for (int i = 0; i < bank.allDebitCards.size(); i++) {
if ((cardNum == bank.allDebitCards.get(i).getId())
&& mima.equals(bank.allDebitCards.get(i).getPassword1())) {
account = bank.allDebitCards.get(i);
System.out.println("登陆成功:");
System.out.println("您的卡片类型为:借记卡");
new LoginSuccess().loginSuccess(account);
}
}
for (int i = 0; i < bank.allCreditCards.size(); i++) {
if ((cardNum == bank.allCreditCards.get(i).getId())
&& mima.equals(bank.allCreditCards.get(i).getPassword1())) {
account = bank.allCreditCards.get(i);
System.out.println("登陆成功:");
System.out.println("您的卡片类型为:信用卡");
new LoginSuccess().loginSuccess(account);
}
}
new LoginFail().loginFail();
}
}
Search.java
package com.banksys.function;
import java.util.Scanner;
import com.banksys.account.Account;
import com.banksys.bank.Bank;
import com.banksys.bank.LoginSuccess;
/**
* 查询模块
* 查询某个账户的余额
* 查询该行所有账户的信用额度的和(只有信用卡账户有信用额度)
* 查询该行所有账户的余额的和(所有账户都有余额)
*/
public class Search {
public Search() {
// TODO Auto-generated constructor stub
}
static Scanner input = new Scanner(System.in);
public Bank bank = new Bank();
public Account account = new Account();
public void search() {
while (true) {
System.out.println("请输入要查询的内容:1.所有账户信用额度的和\t 2.所有账户的余额的和");
String choice = input.next();
if (choice.equals("1")) {
searchAllCeiling();
return;
} else if (choice.equals("2")) {
searchAllBalance();
return;
} else {
System.out.println("输入有误,请重新输入!");
}
}
}
//查询个人账户
public void searchUser(Account account){
System.out.println("您的余额为:" + account.getBalance());
return;
}
//查询所有信用额度
public void searchAllCeiling(){
double allCeiling = 0;
for (int i = 0; i < bank.allCreditCards.size(); i++) {
allCeiling = allCeiling + bank.allCreditCards.get(i).getCeiling();
}
System.out.println("所有账户信用额度:" + allCeiling);
return;
}
//查询所有账户余额
public void searchAllBalance(){
double allBalance = 0;
for (int i = 0; i < bank.allCreditCards.size(); i++) {
allBalance = allBalance + bank.allCreditCards.get(i).getBalance();
}
for (int i = 0; i < bank.allDebitCards.size(); i++) {
allBalance = allBalance + bank.allDebitCards.get(i).getBalance();
}
System.out.println("所有账户余额:" + allBalance);
return;
}
}
Withdraw.java
package com.banksys.function;
import java.util.Scanner;
import com.banksys.account.Account;
/**
* 取款模块
* */
public class Withdraw {
public Withdraw() {
// TODO Auto-generated constructor stub
}
static Scanner input = new Scanner(System.in);
public void withdraw(Account account) {
int money;
while (true) {
System.out.println("请输入取款金额!");
money = input.nextInt();
if (money > 0 && account.getBalance() >= money) {
account.setBalance(account.getBalance() - money);
System.out.println("您的余额为:" + account.getBalance());
return;
} else {
System.out.println("余额不足,请重新输入!");
}
}
}
}
com.banksys.ui
MainUI.java
package com.banksys.ui;
import java.util.Scanner;
import com.banksys.function.EstablishAccount;
import com.banksys.function.Login;
import com.banksys.function.Search;
/**
* 主界面模块
* */
public class MainUI {
public MainUI() {
}
public void showMainUI() {
while (true) {
System.out
.println("*************************************************");
System.out.println("| |");
System.out.println("| 欢迎使用银行管理系统 |");
System.out.println("| |");
System.out
.println("*************************************************");
System.out.println("请输入您要进行的操作:");
System.out.println("1.开户" + "\t" + "2.登陆" + "\t" + "3.查询" + "\t"
+ "4.退出");
Scanner input = new Scanner(System.in);
String userChoice = input.next();
if (userChoice.equals("1")) {
new EstablishAccount().establicAccount();// 开户方法
} else if (userChoice.equals("2")) {
new Login().login();// 登陆
} else if (userChoice.equals("3")) {
new Search().search();// 查询
} else if (userChoice.equals("4")) {
System.out.println("谢谢使用!");
System.exit(0);
} else {
System.out.println("输入错误,请重新输入!");
}
}
}
}