1、项目描述
银行管理系统:实现登录、注册,登录后的用户可以进行存款、取款、转账、查询余额操作
2、分析
创建银行类Bank:用于存储银行卡信息,并创建银行系统的主窗体
创建银行卡类BankCard:初始化银行卡信息
创建用户操作类User:进行存款、取款、转账、查询余额操作
创建测试类Main
3、代码
银行类Bank
import java.util.HashMap;
import java.util.Scanner;
//银行
public class Bank {
//使用哈希列表存储银行的银行卡号信息
public static HashMap<Integer, BankCard> bankCards = new HashMap<>();
//注册账户方法
public void register(int cardNo, int password) {
//判断该卡号是否存在
if (bankCards.containsKey(cardNo)) {
System.out.println("该账户已经存在,麻烦重新选择业务");
return;
}
BankCard bankCard = new BankCard(cardNo,password);
bankCards.put(cardNo, bankCard);
System.out.println("注册成功!");
}
//登录
public BankCard login(int cardNo, int password) {
if (bankCards.size() == 0) {
System.out.println("当前不存在任何银行卡信息,建议及时注册!");
return null;
}
//判断该账户是否存在
if (bankCards.containsKey(cardNo)){
if (bankCards.get(cardNo).getPassword() == password) {
//如果密码正确,则登录成功
return bankCards.get(cardNo);
} else {
System.out.println("登录失败,该密码错误!");
//否则就返回null
return null;
}
}else {
System.out.println("登录失败,该账户不存在!");
}
return null;
}
//进入银行界面
public void manager(User user) {
//输入密码次数不能超过5次,如果输入错误次数超过5次,则退出系统
int number = 5;
while (number > 0) {
System.out.println("***欢迎来到本银行,请选择您的业务:***");
System.out.println("1.登录账户");
System.out.println("2.注册账户");
System.out.println("3.退出");
System.out.println("请输入您的选择:");
Scanner scanner = new Scanner(System.in);
int key = scanner.nextInt();
int cardNo;
int password;
switch (key) {
case 1:
System.out.println("请输入您的银行卡账号:");
cardNo = scanner.nextInt();
System.out.println("请输入您的银行卡密码:");
password = scanner.nextInt();
BankCard card = login(cardNo, password);
if (card != null) {
System.out.printf("***卡号:%d登陆成功***", cardNo);
while (true) {
System.out.println("");
System.out.println("***请输入您需要的业务***");
System.out.println("1.存钱");
System.out.println("2.取钱");
System.out.println("3.转账");
System.out.println("4.查询余额");
System.out.println("5.退卡");
int num = scanner.nextInt();
if (num == 5) break;
operate(user, num, card);
}
} else {
number--;
System.out.println("登陆失败!您还有"+number+"次机会");
}
break;
case 2:
System.out.println("请输入您的银行卡账号:");
cardNo = scanner.nextInt();
System.out.println("请输入您的银行卡密码:");
password = scanner.nextInt();
register(cardNo, password);
break;
case 3:
System.out.println("尊敬的用户,您已成功退出银行系统!欢迎下次光临~");
return;
default:
System.out.println("请重新输入正确选项!");
}
}
}
private void operate(User user, int num, BankCard card) {
switch (num) {
case 1:
user.deposit(card);
break;
case 2:
user.withdraw(card);
break;
case 3:
user.transfer(card);
break;
case 4:
user.select(card);
break;
default:
break;
}
}
}
银行卡类BankCard
//银行卡
public class BankCard {
private int cardNo;//卡号
private int password;//密码
private double money;//余额
//初始化
public BankCard(int cardNo, int password) {
this.cardNo = cardNo;
this.password = password;
//默认该账户初始值为100
this.money = 100;
}
public void setPassword(int password) {
this.password = password;
}
public void setMoney(double money) {
this.money = money;
}
public int getPassword() {
return password;
}
public double getMoney() {
return money;
}
public int getCardNo() {
return cardNo;
}
public void setCardNo(int cardNo) {
this.cardNo = cardNo;
}
}
用户操作类User
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Scanner;
//操作
public class User {
Scanner scanner = new Scanner(System.in);
//存款
public void deposit(BankCard bankCard) {
System.out.println("请输入您的存入金额:");
double money = scanner.nextDouble();
if (money > 0) {
bankCard.setMoney(bankCard.getMoney() + money);
System.out.println("存款成功,本次存款金额为:"+money);
select(bankCard);
} else {
System.out.println("请输入正确金额");
}
}
//取钱
public void withdraw(BankCard bankCard) {
System.out.println("请输入您要取出的金额:");
double money = scanner.nextDouble();
//取款金额大于0.且大于余额
if (money > 0 && money <= bankCard.getMoney()) {
//修改账户金额
bankCard.setMoney(bankCard.getMoney() - money);
System.out.println("取款成功,本次取款金额为:"+money);
select(bankCard);
} else if (money > bankCard.getMoney()) {
System.out.println("很抱歉,您的余额不足!");
} else {
System.out.println("请输入正确取款金额!");
}
}
//查询余额
public void select(BankCard card) {
DecimalFormat df = new DecimalFormat("#.00");
System.out.println("您的余额为: " + df.format(card.getMoney()));
}
//转账
public void transfer(BankCard bankCard) {
//限制输出账号的次数为3次
int number = 3;
while (number > 0) {
System.out.println("请输入对方的卡号:");
int destID = scanner.nextInt();//目的账号
Map<Integer, BankCard> bankCards = Bank.bankCards;
if (bankCards.containsKey(destID)) {
//查找目的账户存在,再进行转账操作
BankCard transferAccounts = bankCards.get(destID);
System.out.println("请输入您要转账的金额:");
double money = scanner.nextDouble();
if (money <= bankCard.getMoney()) {
//修改账户余额
bankCard.setMoney(bankCard.getMoney() - money);
//修改对方账户余额
transferAccounts.setMoney(transferAccounts.getMoney() + money);
System.out.println("转账成功,本次转账金额为:"+money);
select(bankCard);
return;
} else {
System.out.println("很抱歉,您的余额不足!");
}
}
System.out.println("您还有" + (number - 1) + "次机会,请重新输入转账账户");
number--;
}
}
}
测试类Main文章来源:https://www.toymoban.com/news/detail-510743.html
public class Main {
public static void main(String[] args) {
Bank bank = new Bank();
User user = new User();
//打开银行的系统界面
bank.manager(user);
}
}
4、效果
文章来源地址https://www.toymoban.com/news/detail-510743.html
到了这里,关于JAVA项目--银行管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!