package BankManagementSystem;
/*
}
package BankManagementSystem;
/*
}
package BankManagementSystem;
/*
public class UI {
public static void showIndex(){
System.out.println(“欢迎使用GEEK银行账户管理系统**”);
System.out.println(“");
System.out.println("\t1.管理员登陆\t2.用户登录\t\t3.退出系统");
System.out.println("”);
System.out.print(“请输入您想执行的操作:”);
}
public static void showUserLogin(){
System.out.println("******************************************************");
System.out.println("********************** 用户登录系统 *******************");
System.out.println("******************************************************");
}
public static void show2() {
System.out.println("*****************欢迎使用GEEK银行账户管理系统*****************");
System.out.println("******************************************************");
System.out.println("1.查询余额\t\t2.存款\t\t3.取款\t\t4.退出");
System.out.println("*****************欢迎使用GEEK银行账户管理系统*****************");
System.out.print("请选择需要执行的操作:");
}
public static void show3() {
System.out.println("******************************************************");
System.out.println("************************ 取款 ***********************");
System.out.println("******************************************************");
System.out.println("请输入取款金额:");
}
public static void show4() {
System.out.println("******************************************************");
System.out.println("*********************** 存款 ************************");
System.out.println("******************************************************");
}
public static void show5() {
System.out.println("******************************************************");
System.out.println("********************** 余额查询 *********************");
System.out.println("******************************************************");
}
public static void show6() {
System.out.println("******************************************************");
System.out.println("******************** 管理员登录系统 *******************");
System.out.println("******************************************************");
}
public static void show7() {
System.out.println("*****************欢迎使用GEEK银行账户管理系统*****************");
System.out.println("******************************************************");
System.out.println("\t1.添加用户 \t\t2.查询用户\t\t3.退出系统");
System.out.println("******************************************************");
System.out.print("请选择想要执行的操作:");
}
public static void show8() {
System.out.println("******************************************************");
System.out.println("********************* 查询用户 ***********************");
System.out.println("\t1.查询全部\t\t2.按卡号查询\t3.返回");
System.out.println("******************************************************");
System.out.print("请选择想要执行的操作:");
}
public static void show9() {
System.out.println("******************************************************");
System.out.println("********************** 卡号查询 **********************");
System.out.println("******************************************************");
}
public static void show10() {
System.out.println("******************************************************");
System.out.println("********************** 富豪排行榜 *********************");
System.out.println("******************************************************");
}
public static void show11() {
System.out.println("******************************************************");
System.out.println("********************** 添加用户 *********************");
System.out.println("******************************************************");
}
}
package BankManagementSystem;
/*
public class Tools {
public static ArrayList readFile() throws IOException{
BufferedReader br=new BufferedReader(new FileReader(“users.txt”));
String st;
ArrayList userList=new ArrayList<>();
while((st=br.readLine())!=null){
String[] ss = st.split("&");
User user=new User(ss[0], ss[1], ss[2], ss[3], Double.parseDouble(ss[4]));
userList.add(user);
}
br.close();
return userList;
}
}
package BankManagementSystem;
/*
public class Service {
/*
* 主页
*/
public void index() {
while(true){
UI.showIndex();
@SuppressWarnings(“resource”)
Scanner sc=new Scanner(System.in);
int choice=Integer.parseInt(sc.nextLine());
switch (choice) {
case 1:
try {
adminLogin();
} catch (IOException e) {
e.printStackTrace();
}
break;
case 2:
try {
userLogin();
} catch (IOException e) {
e.printStackTrace();
}
break;
case 3:
System.exit(0);
break;
default:
System.out.println("输入错误!");
break;
}
}
}
/*
* 用户登录
*/
public void userLogin() throws IOException {
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
UI.showUserLogin();
System.out.println("请输入用户名:");
String username=sc.nextLine();
System.out.println("请输入密码:");
String password=sc.nextLine();
boolean flag=checkUser(username,password);
if(flag){
while(true){
UI.show2();
int choice=Integer.parseInt(sc.nextLine());
switch (choice) {
case 1:
queryBalance(username,password);//查询余额
break;
case 2:
addBalance(username,password);//存款
break;
case 3:
reduceBalance(username,password);//取款
break;
case 4:
System.exit(0);
break;
default:
System.out.println("输入错误!");
break;
}
}
}
}
/*
* 取款
*/
public void reduceBalance(String username, String password) throws IOException {
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
UI.show3();
double balance=Double.parseDouble(sc.nextLine());
BufferedReader br=new BufferedReader(new FileReader("users.txt"));
String st;
ArrayList userList=new ArrayList<>();
while((st=br.readLine())!=null){
String[] ss = st.split("&");
User user=new User(ss[0], ss[1], ss[2], ss[3], Double.parseDouble(ss[4]));
userList.add(user);
}
br.close();
for (int i = 0; i < userList.size(); i++) {
if(userList.get(i).getUsername().equals(username)&&userList.get(i).getPassword().equals(password)){
if(userList.get(i).getBalance() userList = Tools.readFile();
for (int i = 0; i < userList.size(); i++) {
if(userList.get(i).getUsername().equals(username)&&userList.get(i).getPassword().equals(password)){
User user=new User(username, userList.get(i).getUserId(), userList.get(i).getCardId(), password, userList.get(i).getBalance()+balance);
userList.set(i, user);
}
}
BufferedWriter bw=new BufferedWriter(new FileWriter("users.txt"));
for (int i = 0; i < userList.size(); i++) {
bw.write(userList.get(i).getUsername()+"&"+userList.get(i).getUserId()+"&"+userList.get(i).getCardId()+"&"+userList.get(i).getPassword()+"&"+userList.get(i).getBalance());
bw.newLine();
bw.flush();
}
System.out.println("存款成功!");
bw.close();
}
/*
* 用户查询余额
*/
public void queryBalance(String username, String password) throws IOException {
UI.show5();
ArrayList userList = Tools.readFile();
for (int i = 0; i < userList.size(); i++) {
if(userList.get(i).getUsername().equals(username)&&userList.get(i).getPassword().equals(password)){
System.out.println("卡号\t\t用户名\t余额");
System.out.println(userList.get(i).getCardId()+"\t"+userList.get(i).getUsername()+"\t"+userList.get(i).getBalance());
}
}
}
/*
* 校验用户信息
*/
public boolean checkUser(String username, String password) throws IOException {
ArrayList userList = Tools.readFile();
for (int i = 0; i < userList.size(); i++) {
if(userList.get(i).getUsername().equals(username)&&userList.get(i).getPassword().equals(password)){
return true;
}
if(userList.get(i).getUsername().equals(username)&&!userList.get(i).getPassword().equals(password)){
System.out.println("用户名或密码错误!");
return false;
}
}
System.out.println("该用户不存在!");
return false;
}
/*
* 管理员登录
*/
public void adminLogin() throws IOException {
UI.show6();
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.println("请输入账号:");
String id=sc.nextLine();
System.out.println("请输入密码:");
String pwd=sc.nextLine();
boolean flag=checkAdmin(id,pwd);
if(flag){
while(true){
UI.show7();
int choice=Integer.parseInt(sc.nextLine());
switch (choice) {
case 1:
addUser();
break;
case 2:
queryUser();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("输入错误!");
break;
}
}
}else{
System.out.println("该管理员不存在!");
}
}
/*
* 查询用户
*/
public void queryUser() throws IOException {
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
while(true){
UI.show8();
int choice=Integer.parseInt(sc.nextLine());
switch (choice) {
case 1:
queryAll();
break;
case 2:
queryByCardId();
break;
case 3:
return;
default:
System.out.println("输入错误!");
break;
}
}
}
/*
* 按卡号查询用户
*/
public void queryByCardId() throws IOException {
UI.show9();
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.println("请输入卡号:");
String cardId=sc.nextLine();
ArrayList userList = Tools.readFile();
for (int i = 0; i < userList.size(); i++) {
if(userList.get(i).getCardId().equals(cardId)){
System.out.println("卡号\t\t用户名\t余额");
System.out.println(userList.get(i).getCardId()+"\t"+userList.get(i).getUsername()+"\t"+userList.get(i).getBalance());
return;
}
}
System.out.println("没有该账户!");
}
/*
* 查询所有用户
*/
public void queryAll() throws IOException {
UI.show10();
ArrayList userList = Tools.readFile();
Collections.sort(userList,new Comparator() {//按存款金额降序排序
@Override
public int compare(User o1, User o2) {
return (int) (o2.getBalance()-o1.getBalance());
}
});
System.out.println("卡号\t\t用户名\t\t余额");
for (int i = 0; i < userList.size(); i++) {
System.out.println(userList.get(i).getCardId()+"\t"+userList.get(i).getUsername()+"\t\t"+userList.get(i).getBalance());
}
}
/*
* 管理员添加用户
*/
public void addUser() throws IOException {
UI.show11();
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.println("请输入姓名:");
String username=sc.nextLine();
System.out.println("请输入身份证号码:");
String userId=sc.nextLine();
System.out.println("请输入卡号:");
String cardId=sc.nextLine();
System.out.println("请输入密码:");
String password=sc.nextLine();
System.out.println("请输入余额:");
double balance=Double.parseDouble(sc.nextLine());
HashMap userMap=new LinkedHashMap<>();
BufferedReader br=new BufferedReader(new FileReader("users.txt"));
String st;
while((st=br.readLine())!=null){//读取用户表信息
String[] ss = st.split("&");
User user=new User(ss[0], ss[1], ss[2], ss[3], Double.parseDouble(ss[4]));
userMap.put(ss[2], user);
}
if(userMap.get(cardId)!=null){//判断添加的卡号是否已存在
System.out.println("该卡号已存在,无法再次添加!");
br.close();
return;
}
br.close();
BufferedWriter bw=new BufferedWriter(new FileWriter("users.txt",true));
bw.write(username+"&"+userId+"&"+cardId+"&"+password+"&"+balance);
bw.newLine();
System.out.println("添加成功!");
bw.close();
}
/*
* 校验管理员
*/
public boolean checkAdmin(String id, String pwd) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("admin.txt"));
String st;
ArrayList adminList=new ArrayList<>();
while((st=br.readLine())!=null){
Admin a=new Admin();
String[] ss = st.split("&");
a.setId(ss[0]);
a.setPassword(ss[1]);
a.setName(ss[2]);
adminList.add(a);
}
br.close();
for (int i = 0; i < adminList.size(); i++) {
if(adminList.get(i).getId().equals(id)&&adminList.get(i).getPassword().equals(pwd)){
return true;
}
}
return false;
}
}
package BankManagementSystem;
/*