简单给你写写,
首先是User类,存放用户信息
import java.math.BigDecimal;
public class User {
private Long cardId; //银行卡号
private String cardPwd; //密码
private String userName; //用户名
private Long call; //手机号
private BigDecimal account; //存款额
public Long getCardId() {
return cardId;
}
public void setCardId(Long cardId) {
this.cardId = cardId;
}
public String getCardPwd() {
return cardPwd;
}
public void setCardPwd(String cardPwd) {
this.cardPwd = cardPwd;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Long getCall() {
return call;
}
public void setCall(Long call) {
this.call = call;
}
public BigDecimal getAccount() {
return account;
}
public void setAccount(BigDecimal account) {
this.account = account;
}
}
接着是存储类,存放银行全部的账户信息
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DBUtil {
private HashMap<Long, User> userMap = new HashMap<>(); //key值用于存储银行卡号,value值用于存储整个账户对象
//获取单个账户信息的方法
public User queryOneUser(String key){
return userMap.get(key);
}
// 获取所有账户信息的方法
public List<User> queryAllUser(){
List<User> list = new ArrayList<>();
userMap.forEach((key, user)-> list.add(user));
return list;
}
public HashMap<Long, User> getUserMap() {
return userMap;
}
public void setUserMap(HashMap<Long, User> userMap) {
this.userMap = userMap;
}
}
最后是银行处理类:
import java.util.HashMap;
import java.util.Scanner;
public class Bank {
public DBUtil dbUtil = new DBUtil(); //存储账户信息的类
public static void main(String[] args) {
//需要增加一个注册逻辑,将用户信息存入到dbUtil的map中
new Bank().start();
}
public void start(){
System.out.println("请输入(注册:1;登录:2):");
int type = new Scanner(System.in).nextInt();
if(type == 1){
regist();
}else if (type == 2){
login();
}else{
System.out.println("无此选项,请重新输入");
start();
}
}
public void regist(){ //注册
//输入用户信息,创建并保存到User对象并存放到dbUtil的map下
}
public void login(){ //登录
Scanner s = new Scanner(System.in);
System.out.println("请输入银行卡号:");
Long cardId = s.nextLong();
System.out.println("请输入银行卡密码:");
String pwd = s.nextLine();
HashMap<Long, User> userMap = dbUtil.getUserMap();
if(!userMap.isEmpty() && userMap.get(cardId).getCardPwd().equals(pwd)){
System.out.println("登录成功");
oper(userMap.get(cardId));
}else{
System.out.println("登录失败,请重新登录");
login();
}
}
public void oper(User user){
System.out.println("请输入操作类型(存款:1;取款:2;余额:3;退出:0):");
int type = new Scanner(System.in).nextInt();
if(type == 1){
if(deposit(user)){
System.out.println("存款成功!");
}else{
System.out.println("存款失败!");
}
oper(user);
}else if(type == 2){
if(draw(user)){
System.out.println("取款成功!");
}else{
System.out.println("取款失败!");
}
oper(user);
}else if(type == 3){
query(user);
oper(user);
}else{
System.out.println("退出成功,感谢您的使用!");
}
}
public boolean deposit(User user){ //存款 成功返回true;失败返回false
//存款逻辑
//将user更新到dbUtil的map当中
return true;
}
public boolean draw(User user){ //取款 成功返回true;失败返回false
//取款逻辑
//将user更新到dbUtil的map当中
return true;
}
public boolean query(User user){ //查询余额
System.out.println("您的余额为:"+ user.getAccount());
return true;
}
}
给你写个大概吧,剩下的靠你自己发挥了!!!!
请采纳支持哈!!!!