前言
用到了IO流,集合和JavaSwing的实训小项目
一、内容介绍
基本内容有:
-
登陆
-
注册
-
首页
-
个人信息显示
-
修改密码
-
添加银行卡
-
银行卡查看
-
充值
-
提现
-
转账
-
用户管理
主要用到的知识如下:
集合知识:用到List集合,来运输暂存数据 。
接口:创建了BankController接口和UserController接口方便方法的调用和创建,分别用BankControllerimpl和UserControllerimpl类实现了它们。
创建实体类:实用了创建方法的快捷方式。
IO流:运用对象流的序列化和反序列化,将集合多次存入取出文件,方便数据更新的同时,能有效的保存用户和银行卡的信息。实现的此代码的类有AdminDao,BanksDao,UserDao。
JavaSwing:把GUI组件合理的搭配和设计,添加到底层容器中,展现到窗口中,然后添加监听器,处理事件,完成每个界面的功能。此部分技术的运用都在com.baizhi.view包中
二、实现原理
登录页面
首先设计的是登录页面,设置了手机号和密码两个可填项,同时设置两个按钮并且对其设置监听器,点击等录能够进入主页面,点击注册则跳转注册页面。
注册页面
注册页面设置了一些用户的属性,没有格式要求,填写完整即可点击提交,把数据通过AdminDao对象流存入文件Admin.data,注册完成自动关闭页面,再次登录就可以使用该账号登录,点击取消关闭页面。
首页
主页面包含左侧导航栏功能列表,显示功能,而功能这可以在界面中间部分展示出来,点击首页,返回首页,对应的界面是MainView。
用户信息
有用户的信息展示,包括金额,修改金额后此处的金额也会随之变动,文字的前面都有图片标签。此页面的类为ShowUserView。
修改密码
此功能是给登录的用户从新设置密码,通过UserDao类,获取所有用户,然后通过list集合存取用户,通过对list的遍历筛选,得到本用户的密码,然后调用setPwd对用户密码进行修改。
添加银行卡
与注册用户相似,也是把得到的银行卡信息存到list中,如果已有银行卡,则先把以前银行卡的内容存到list中,然后再把新增的信息存入,这样就可以一次性存完,不用再考虑信息存入文件的内容覆盖的问题了。点击提交,存入数据。此页面的类为AddBanksView。
银行卡查看
在此页面可以观察到此账户拥有的银行卡的信息,同样是通过BankDao获取此手机号拥有的银行卡信息list集合,然后通过遍历打印出来。此类的为ShowBanksView。
充值
充值是从银行卡充钱到账户上,实现金额交换,将文本框获取的mony属性通过构造方法传入BanksServiceImpl类,完成后端的文件中的集合,然后通过遍历寻找,修改,然后点击充值,便会实现金额交换。此类为AddUsersMoneyView。
提现
提现就是把账户上的钱提到银行卡上。
提现和充值相同,把二者的方法,交换重写一遍就可以实现次功能。此类为AddBanksMoneyView。
银行卡转账
首先要选择转入转出的对象是银行卡还是账户,然后分别输入银行卡或是账户的卡号和手机号,然后遍历所给的号是否存在,不存在会有弹框提示
确定存在后,会对转出和转入方两个方法同时调用。实现金钱交换。
此类为MoveMoneyView。
用户管理
在这里可以显示注册过的所有用户,与银行卡查看同理,都是先得到所有用户信息的list,然后遍历打印出来。此类为ShowUserView。
代码展示
BanksController
package com.baizhi.controller;
import com.baizhi.entity.Banks;
import com.baizhi.service.BanksService;
import com.baizhi.service.impl.BanksServiceImpl;
import java.util.List;
public class BanksController {
private BanksServiceImpl service =new BanksServiceImpl();
public boolean addBanks(Banks banks){
return service.addBanks(banks);
}
public List<Banks> showBanksByTel(String tel){
return service.showBanksByTel(tel);
}
public boolean deleteMoney(String banksNum,double money){
return service.deleteMoney(banksNum,money);
}
public boolean addMoney(String banksNum,double money){
return service.addMoney(banksNum,money);
}
public boolean moveMoney(String banksNum1,String banksNum2,double money){
return service.moveMoney(banksNum1,banksNum2,money);
}
public boolean chickNUm( String num) {
return service.chickNUm(num);
}
}
UserController
package com.baizhi.controller;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.service.impl.UsersServiceImpl;
import java.util.List;
// 用户管理
public class UserController {
private UserService userService =new UsersServiceImpl();
public User userLog(String tel,String pwd) {
return userService.findUSerByTelAdnpwd(tel,pwd);
}
// 注册
//String name, String cardNum, String tel, String pwd, String email, double money, String relName
public boolean addUser(String name, String cardNum, String tel, String pwd, String email, String relName){
User u =new User(name,cardNum,tel,pwd,email,0,relName);
return userService.addUsers(u);
}
public boolean isExistTel(String tel) {
return userService.isExistTel(tel);
}
public boolean isExistEmail(String email){
return userService.isExistEmail(email);
}
public boolean updatePwd(String tel,String odlPwd,String pwd){
return userService.updatePwd(tel,pwd);
}
public boolean addMoney(String tel,double money){
return userService.addMoney(tel,money);
}
public boolean deleteMoney(String tel,double money){
return userService.deleteMoney(tel,money);
}
public List<User> showUserTel(String tel){
return userService.showUserTel(tel);
}
public List<User> showUser(){
return userService.showUser();
}
public boolean chickNUm(String num) {
return userService.chickNUm(num);}
}
AdminDao
package com.baizhi.dao;
import com.baizhi.entity.Admin;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class AdminDao {
public List<Admin> read(){
List<Admin> list =new ArrayList<>();
try{
ObjectInputStream IS=new ObjectInputStream(new FileInputStream("admin.data"));
list = (List<Admin>) IS.readObject();
IS.close();
}catch (Exception e){
e.printStackTrace();
}
return list;
}
public boolean write(List<Admin> list){
try {
ObjectOutputStream OS = new ObjectOutputStream(new FileOutputStream("admin.data"));
OS.writeObject(list);
OS.close();
return true;
}catch (Exception e){
e.printStackTrace();
System.out.println("写入失败");
return false;
}
}
public static void main(String[] args) {
List<Admin> list =new ArrayList<>();
list.add(new Admin("admin","123"));
AdminDao dao =new AdminDao();
dao.write(list);
List<Admin> lists =dao.read();
for (Admin ad : lists){
System.out.println(ad);
}
}
}
BanksDao
package com.baizhi.dao;
import com.baizhi.entity.Banks;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class BanksDao {
public List<Banks> read(){
List<Banks> list =new ArrayList<>();
try {
FileInputStream file =new FileInputStream("Banks.data");
ObjectInputStream IS =new ObjectInputStream(file);
list = (List<Banks>) IS.readObject();
IS.close();
}catch (Exception e){
e.printStackTrace();
}
return list;
}
public boolean write(List<Banks> list){
try {
FileOutputStream file=new FileOutputStream("Banks.data");
ObjectOutputStream OS =new ObjectOutputStream(file);
OS.writeObject(list);
OS.close();
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
}
UserDao
package com.baizhi.dao;
import com.baizhi.entity.Admin;
import com.baizhi.entity.User;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public List<User> read(){
List<User> list =new ArrayList<>();
try{
ObjectInputStream IS=new ObjectInputStream(new FileInputStream("User.data"));
list = (List<User>) IS.readObject();
IS.close();
}catch (Exception e){
e.printStackTrace();
}
return list;
}
public boolean write(List<User> list){
try {
ObjectOutputStream OS = new ObjectOutputStream(new FileOutputStream("User.data"));
OS.writeObject(list);
OS.close();
return true;
}catch (Exception e){
e.printStackTrace();
System.out.println("写入失败");
return false;
}
}
}
Admin
package com.baizhi.entity;
import java.io.Serializable;
public class Admin implements Serializable {
private String account;
private String pwd;
public Admin(String account, String pwd) {
this.account = account;
this.pwd = pwd;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "Admin{" +
"account='" + account + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
Banks
package com.baizhi.entity;
import javax.swing.*;
import java.io.Serializable;
public class Banks implements Serializable {
private String bankNumber;
private double money;
private String bankName;
private String tel;
public Banks(String bankNumber, double money, String bankName, String tel) {
this.bankNumber = bankNumber;
this.money = money;
this.bankName = bankName;
this.tel = tel;
}
public String getBankNumber() {
return bankNumber;
}
public void setBankNumber(String bankNumber) {
this.bankNumber = bankNumber;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "Banks{" +
"bankNumber='" + bankNumber + '\'' +
", money=" + money +
", bankName='" + bankName + '\'' +
", tel='" + tel + '\'' +
'}';
}
}
User
package com.baizhi.entity;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String cardNum;
private String tel;
private String pwd;
private String email;
private double money;
private String relName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getRelName() {
return relName;
}
public void setRelName(String relName) {
this.relName = relName;
}
public User(String name, String cardNum, String tel, String pwd, String email, double money, String relName) {
this.name = name;
this.cardNum = cardNum;
this.tel = tel;
this.pwd = pwd;
this.email = email;
this.money = money;
this.relName = relName;
}
public String toString() {
return "User{" +
"name='" + name + '\'' +
", cardNum='" + cardNum + '\'' +
", tel='" + tel + '\'' +
", pwd='" + pwd + '\'' +
", email='" + email + '\'' +
", money=" + money +
", relName='" + relName + '\'' +
'}';
}
}
BanksServiceImpl
package com.baizhi.service.impl;
import com.baizhi.dao.BanksDao;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import com.baizhi.service.BanksService;
import java.util.ArrayList;
import java.util.List;
public class BanksServiceImpl implements BanksService {
private BanksDao dao =new BanksDao();
private List<Banks> list;
@Override
public boolean addBanks(Banks banks) {
list =new ArrayList<>();
list= dao.read();
list.add(banks);
return dao.write(list);
}
@Override
public List<Banks> showBanksByTel(String tel) {
List<Banks> list=dao.read();
List<Banks> uList=new ArrayList<>();
for (Banks b:list){
if (b.getTel().equals(tel)) {
uList.add(b);
}
}
return uList;
}
@Override
public boolean deleteMoney(String banksNum, double money) {
List<Banks> list=dao.read();
for (Banks b:list){
System.out.println("长度"+list.size());
if (banksNum.equals(b.getBankNumber())){
double mo =b.getMoney();
if (mo-money<0){
return false;
}else {
b.setMoney(mo-money);
System.out.println("银行卡减后"+b.getMoney());
}
}
}
return dao.write(list);
}
@Override
public boolean moveMoney(String banksNum1, String banksNum2, double money) {
List<Banks> list =dao.read();
//System.out.println("出账户"+banksNum1+",入账户"+banksNum2);
for (Banks b: list) {
if (b.getBankNumber().equals(banksNum1) ) {
if (b.getMoney()-money>=0)
b.setMoney(b.getMoney() - money);
else return false;
}
}
for (Banks b:list){
if (b.getBankNumber().equals(banksNum2)){