package com.cx.bank.dao;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.UserNullException;
import java.io.*;
import java.util.Properties;
public class BankDaoImpl implements BankDaoInterface {
public void saveMoney(String name, MoneyBean mon) {
Properties pro = new Properties();
try {
FileInputStream fis = new FileInputStream(name + ".properties");
pro.load(fis);
fis.close();
pro.setProperty("Money", Double.toString(mon.getMoney()));//向文件中存入Money
FileOutputStream fos = new FileOutputStream(name + ".properties");
pro.store(fos, name + ".properties");//存入
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void insetUser(String name, String password) {
String fis1 = "src/com/cx/bank/properties";
Properties props = new Properties();
try {
FileInputStream fis = new FileInputStream(fis1);
props.load(fis);
fis.close();
//进行内容的初始化
props.setProperty("userName", name);
props.setProperty("password", password);
props.setProperty("Money", "10");
FileOutputStream fos = new FileOutputStream(name + ".properties");
props.store(fos, name + ".properties");
fos.close();
System.out.println("注册成功");
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean updateMoney(String name, MoneyBean moneyBean, double money) {
boolean flag;
Properties pro = new Properties();
try {
FileInputStream fis = new FileInputStream(name + ".properties");
pro.load(fis);
fis.close();
double pmoney = Double.parseDouble(pro.getProperty("Money"));//要转入账户的余额
pro.setProperty("Money", Double.toString(pmoney + money));//存入对方账户
FileOutputStream fos = new FileOutputStream(name + ".properties");
pro.store(fos, name + ".properties");
fos.close();
flag = true;
System.out.println("转账成功");
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
return flag;
}
public boolean findByName(String name) {
File f = new File(name + ".properties");
return f.exists();
}
@Override
public boolean findUser(UserBean user, MoneyBean mb) throws UserNullException {
Properties pro = new Properties();
String name = user.getUsername();
String password = user.getPassword();
//判断在文件中该用户是否存在,不存在则抛出异常
if (!findByName(name)) {
throw new UserNullException("该用户未注册,请先进行注册");
}
try {
FileInputStream fis = new FileInputStream(name + ".properties");
pro.load(fis);//进行资源的读取
fis.close();
//与文件中的用户名密码尽心比较
if (pro.getProperty("userName").equals(name) && pro.getProperty("password").equals(password)) {
mb.setMoney(Double.parseDouble(pro.getProperty("Money")));//将money类型进行转换
System.out.println("登录成功");
return true;
} else {
System.out.println("账户或者密码错误,请重新输入");
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
package com.cx.bank.dao;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.UserNullException;
public interface BankDaoInterface {
void saveMoney(String name, MoneyBean money);
void insetUser(String name,String password);
boolean updateMoney(String name,MoneyBean moneyBean,double money) throws UserNullException;
boolean findByName(String name);
boolean findUser(UserBean user, MoneyBean mb) throws UserNullException;
}
package com.cx.bank.manager;
import com.cx.bank.dao.BankDaoImpl;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.InvalidDepositException;
import com.cx.bank.util.NameOrPswNullException;
import com.cx.bank.util.UserNullException;
/**
* 包名:com.cx.bank.manager (业务层)
* ManagerImpl业务类
*/
public class ManagerImpl implements ManagerInterface {
//引入dao层
BankDaoImpl bi=new BankDaoImpl();
UserBean u=new UserBean();
private static ManagerImpl instance = null; // 单例对象
private final MoneyBean moneyBean = new MoneyBean();
// 构造函数私有化,防止外部创建对象
private ManagerImpl() {
}
// 获取单例对象的静态方法
public static ManagerImpl getInstance() {
if (instance == null) {
instance = new ManagerImpl();
}
return instance;
}
public double inquiry() {
return moneyBean.getMoney();
}
public boolean withdrawals(double money) throws AccountOverDrawnException {
if (money <= 0) {
throw new AccountOverDrawnException("对不起,您的取款金额需大于0元!");
}
double leave = moneyBean.getMoney();
if (money > leave) {
System.out.println("对不起,您的存款金额不够!");
return false;
}
moneyBean.setMoney(leave - money);
System.out.println("取款成功!");
return true;
}
public boolean deposit(double money) throws InvalidDepositException {
if (money <= 0) {
throw new InvalidDepositException("对不起,您的存款金额需大于0元!");
}
double leave = moneyBean.getMoney();
moneyBean.setMoney(leave + money);
System.out.println("存款成功!");
return true;
}
public void exitSystem() {
System.out.println("系统已经退出");
System.exit(1);
}
//注册
public void register(String name,String password) throws NameOrPswNullException {
if("".equals(name)||"".equals(password)){
throw new NameOrPswNullException("用户名或者密码为空,请重新输入");
}
//进行注册
bi.insetUser(name,password);
}
/**
* 完成登录
*
*/
public boolean login(String name,String password) throws UserNullException , NameOrPswNullException{
u.setPassword(password);
boolean f;
u.setUsername(name);
if("".equals(name)||"".equals(password)){
throw new NameOrPswNullException("用户名或者密码为空,请重新输入");
}
//判断用户是否存在
if(!bi.findByName(name)){
throw new UserNullException("用户不存在");
}else {
f= bi.findUser(u,moneyBean);
}
return f;
}
/**
* 用来完成转账功能
*/
public void updateMoney(String name,double money) throws InvalidDepositException,AccountOverDrawnException,UserNullException{
try {
if(money<=0){
throw new InvalidDepositException("所要转账的金额不能小于0");
}
if(money>moneyBean.getMoney()){
throw new AccountOverDrawnException("余额不足");
}
if(!bi.findByName(name)) {
throw new UserNullException("用户不存在");
}
bi.updateMoney(name,moneyBean,money);
double balance=moneyBean.getMoney();
moneyBean.setMoney(balance-money);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.cx.bank.manager;
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.InvalidDepositException;
public interface ManagerInterface {
double inquiry();//余额查询功能
boolean withdrawals(double money) throws AccountOverDrawnException; //取款功能
boolean deposit(double money) throws InvalidDepositException;//存款功能
void exitSystem(); //退出系统功能
}
package com.cx.bank.model;
/*
* 模型层
* 对请求和结果数据的封装
* 包名:com.cx.bank.model(模型层)
* MoneyBean 实体类(该类有一个money属性)
*/
public class MoneyBean {
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public MoneyBean() {
super();
}
}
package com.cx.bank.model;
public class UserBean { private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
}
package com.cx.bank.util;
public class AccountOverDrawnException extends Exception{
public void AccountOverDrawnException() {
}
public AccountOverDrawnException(String s) {
super(s);
}
}
package com.cx.bank.util;
public class InvalidDepositException extends Exception{
public void InvalidDepositException(){
}
public InvalidDepositException(String s) {
super(s);
}
}
package com.cx.bank.util;
public class NameOrPswNullException extends Exception{
public NameOrPswNullException() {
super();
}
public NameOrPswNullException(String s) {
super(s);
}
}
package com.cx.bank.util;
public class UserNullException extends Exception{
public UserNullException() {
super();
}
public UserNullException(String s) {
super(s);
}
}
package com.cx.bank.test;
import com.cx.bank.dao.BankDaoImpl;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.InvalidDepositException;
import com.cx.bank.util.NameOrPswNullException;
import com.cx.bank.util.UserNullException;
import java.util.Scanner;
/*
* 测试层
*/
public class TestBank {
//创建一个正在登录的用户名,表式该用户正在使用系统
public static String logname = null;
BankDaoImpl bd = new BankDaoImpl();
//写入对象
Scanner sc = new Scanner(System.in);
//获取唯一可用的对象
ManagerImpl manager = ManagerImpl.getInstance();
public static void printLogin() {//登录界面
System.out.println("---------请输入操作---------");
System.out.println(" 1.注 册 ");
System.out.println(" 2.登 录 ");
System.out.println(" 3.退出系统 ");
System.out.println("--------------------------");
}
private static void printMenu() {
System.out.println("-----------银行系统1.4-----------");
System.out.println(" 1.存款 ");
System.out.println(" 2.取款 ");
System.out.println(" 3.转账 ");
System.out.println(" 4.查询 ");
System.out.println(" 5.退出系统 ");
}
//存款再进行封装
public void testDeposit() {
System.out.println("输入要存入的余额");
int money = sc.nextInt();
try {
manager.deposit(money);
} catch (InvalidDepositException e) {
e.printStackTrace(); //打印异常信息
System.out.println(e.getMessage());
}
}
//取款再进行封装
public void testWithdrawals() {
System.out.println("输入要取出的余额");
double money = sc.nextDouble();
try {
manager.withdrawals(money);
} catch (AccountOverDrawnException e) {
e.printStackTrace();//获取异常信息
System.out.println("请重新输入");
}
}
//对转账进行封装
public void testUpdateMoney() {
System.out.println("请输入要转账的用户名:");
String name = sc.next();
boolean f = bd.findByName(name);
System.out.println("请输入转账金额");
int money = sc.nextInt();
try {
manager.updateMoney(name, money);
} catch (InvalidDepositException | AccountOverDrawnException | UserNullException e) {
e.printStackTrace();
}
}
//登录再进行封装
public boolean testLogin() {
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
boolean f = false;
try {
f = manager.login(name, password);
if (f) {
logname = name;
}
} catch (UserNullException | NameOrPswNullException e) {
e.printStackTrace();
}
return f;
}
//注册再封装
public void tesRegister() {
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
try {
manager.register(name, password);
} catch (NameOrPswNullException e) {
e.printStackTrace();
}
}
//查询封装
public void testInquiry() {
manager.inquiry();
}
public static void main(String[] args) {
ManagerImpl manager = ManagerImpl.getInstance();
Scanner scanner = new Scanner(System.in);
TestBank tb = new TestBank();
while (true) {
printLogin();
System.out.println("请输入接下来的操作");
int a = scanner.nextInt();
if (a == 1) {
tb.tesRegister();
System.out.println("注册成功即可登录");
} else if (a == 2) {
boolean f = tb.testLogin();
if (f) {
break;
}
} else {
manager.exitSystem();
}
}
while (true) {
printMenu();
System.out.println("输入您接下来的操作");
int num = scanner.nextInt();
if (num == 1) {
tb.testDeposit();
System.out.println("您当前余额为:" + manager.inquiry() + "");
} else if (num == 2) {
tb.testWithdrawals();
System.out.println("您当前余额为:" + manager.inquiry() + "");
} else if (num == 4) {
tb.testInquiry();
System.out.println("您当前余额为:" + manager.inquiry() + "");
} else if (num == 3) {
tb.testUpdateMoney();
System.out.println("您当前余额为:" + manager.inquiry() + "");
} else {
manager.exitSystem();
}
}
}
}