这次我们学习了I/O流的读写操作,通过I/O操作对上一次的业务框架进行改写,使其能够将我们的用户名和密码写入文件当中,并且添加注册功能,以及查找对比功能。
一、entity层:
entity实现基础的类,person类:包含人的名字和年龄属性;Student类,包含人的名字、年龄以及学生的成绩;User类,包含人的名字和年龄属性,以及用户的用户名和密码
本次的实体类中均实现了Serialiable接口(一个对象序列化的接口,一个类只有实现了Serializable接口,它的对象才能被序列化。)
1.1、Person类
package com.ruixi.entity;
/**
* @program: st
* @ClassName Person
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:01
* @Version 1.0
**/
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
1.2、Student类
package com.ruixi.entity;
import java.io.Serializable;
/**
* @program: st
* @ClassName Student
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:02
* @Version 1.0
**/
public class Student extends Person implements Serializable {
private String id;
private double score;
public Student(String name, int age, String id, double score) {
super(name, age);
this.id = id;
this.score = score;
}
public String getId() {
return id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
1.3、User类:
package com.ruixi.entity;
import java.io.Serializable;
/**
* @program: st
* @ClassName User
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:01
* @Version 1.0
**/
public class User implements Serializable {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
二、Dao层:
dao层变化是比较大的,我们在之前的时候就了解到过dao层是调取数据库中的数据的,对数据库执行读取以及写入操作,因此改变为文件的读写后变化最大的就是dao层。
2.1、dao的Login接口
package com.ruixi.dao;
import com.ruixi.entity.User;
/**
* @program: st
* @ClassName LoginDao
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:08
* @Version 1.0
**/
public interface LoginDao {
//注册方法
public boolean register(User user);
//登录方法
public boolean loginDao(User user);
}
2.1、dao的Login类实现
package com.ruixi.dao.impl;
import com.ruixi.dao.LoginDao;
import com.ruixi.entity.User;
import com.ruixi.utils.Utils;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* @program: st
* @ClassName LoginDaoIpml
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:10
* @Version 1.0
**/
public class LoginDaoImpl implements LoginDao {
//获取文件长度
private long getFileLengthss(){
File file = new File(Utils.url);
return file.length();
}
//获取文件中的用户信息
private Map<String, User> getUserInfo(){
Map<String, User> map = new HashMap<String, User>();
//读取数据
ObjectInputStream ois = null;
try{
//构建读取流文件的对象
ois = new ObjectInputStream(new FileInputStream(new File(Utils.url)));
//进行数据读取
map = (Map<String,User>)ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}finally {
if(ois != null){
try {
ois.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return map;
}
@Override
public boolean register(User user) {
boolean flag = false;
long fileLength = getFileLengthss();
//获取文件数据
Map<String, User> map = getUserInfo();
//用户名已被注册
if (map.get(user.getUsername()) != null) {
flag = false;
}else{//用户未被注册
//创建写入流引用
ObjectOutputStream oos = null;
try {
//创建写入流对象
oos = new ObjectOutputStream(new FileOutputStream(new File(Utils.url)));
//在用户的map集合中添加该用户
map.put(user.getUsername(), user);
//写入用户数据
oos.writeObject(map);
//更改注册标识
flag = true;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return flag;
}
@Override
public boolean loginDao(User user) {
boolean flag = false;
//获取文件
Map<String, User> map = null;
//判断文件是否为空,不为空就读取数据
if (getFileLengthss() > 0) {
map = getUserInfo();
}
if (map.get(user.getUsername())!=null&& map.get(user.getUsername()).getPassword().equals(user.getPassword())) {
//合法用户
flag = true;
}
return flag;
}
}
三、service层
3.1、service的Login接口
package com.ruixi.service;
import com.ruixi.entity.User;
/**
* @program: st
* @ClassName LoginService
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:44
* @Version 1.0
**/
public interface LoginService {
public boolean myLogin(User user);
public boolean myRegister(User user);
}
3.2、service的Login实现
package com.ruixi.service.impl;
import com.ruixi.dao.LoginDao;
import com.ruixi.dao.impl.LoginDaoImpl;
import com.ruixi.entity.User;
import com.ruixi.service.LoginService;
/**
* @program: st
* @ClassName LoginServiceImpl
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:46
* @Version 1.0
**/
public class LoginServiceImpl implements LoginService {
//创建dao层引用
private LoginDao loginDao = null;
//无参构造
public LoginServiceImpl() {
//创建dao层对象
loginDao = new LoginDaoImpl();
}
@Override
public boolean myLogin(User user) {
boolean islogin = false;
islogin = loginDao.loginDao(user);
return islogin;
}
@Override
public boolean myRegister(User user) {
boolean isregister = false;
isregister = loginDao.register(user);
return isregister;
}
}
四、contrallor层
package com.ruixi.contrallor;
import com.ruixi.entity.User;
import com.ruixi.service.LoginService;
import com.ruixi.service.impl.LoginServiceImpl;
/**
@program: st
@ClassName LoginContrallor
@description:
@author: 苏芮溪
@create: 2024−10-24 10:55
@Version 1.0
**/
public class Logincontroller {
//创建service层引用
private LoginService loginService = null;
public Logincontroller() {
loginService = new LoginServiceImpl();
}
public boolean login(String username, String password) {
User user = new User(username, password);
boolean isLogin = loginService.myLogin(user);
return isLogin;
}
public boolean register(String username, String password) {
User user = new User(username, password);
boolean myRegister = loginService.myRegister(user);
return myRegister;
}
}
五、utils层
package com.ruixi.utils;
/**
* @program: st
* @ClassName Utils
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:11
* @Version 1.0
**/
public class Utils {
public static String url = "F:/Data/UserData.txt";
}
六、test层
6.1界面控制转换
package com.ruixi.test;
import com.ruixi.contrallor.Logincontroller;
import java.util.Scanner;
/**
* @program: st
* @ClassName Ui
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 11:01
* @Version 1.0
**/
public class Ui {
static Scanner scanner = new Scanner(System.in);
public static void uistart(){
boolean flag = true;
menu();
while(flag) {
System.out.println("请选择:");
int choice = scanner.nextInt();
//退出
if (choice == -1) {
flag = false;
}
//根据用户选择进行
switch (choice) {
case 0://返回菜单
menu();
break;
case 1://注册
registerui();
break;
case 2://登录
loginui();
break;
}
}
}
public static void menu(){
System.out.println("**************************************");
System.out.println("0.菜单");
System.out.println("1.注册");
System.out.println("2.登录");
System.out.println("-1.退出");
System.out.println("**************************************");
}
public static void registerui(){
System.out.println("--------------------------------------");
System.out.println("请输入用户名:");
String name = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
Logincontroller login = new Logincontroller();
boolean register = login.register(name, password);
if(register == false){
System.out.println("注册失败,该用户名已存在!");
}else {
System.out.println("### 注册成功 ###");
}
System.out.println("--------------------------------------");
System.out.println("0.返回");
System.out.println("-1.退出");
}
public static void loginui(){
System.out.println("--------------------------------------");
System.out.println("请输入用户名:");
String name = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
Logincontroller login = new Logincontroller();
boolean islogin = login.login(name, password);
if(islogin == false){
System.out.println("登录失败,用户名或密码错误");
}else{
System.out.println("》》》登录成功《《《");
}
System.out.println("--------------------------------------");
System.out.println("0.返回");
System.out.println("-1.退出");
}
}
6.2、main的主类
package com.ruixi.test;
/**
* @program: st
* @ClassName Test
* @description:
* @author: 苏芮溪
* @create: 2024−10-24 10:57
* @Version 1.0
**/
public class Test {
public static void main(String[] args) {
Ui.uistart();
}
}