diff --git a/src/edu/tyut/selaba2/student/DAO/impl/DAO.java b/src/edu/tyut/selaba2/student/DAO/impl/DAO.java index 7a6a583e60267be5c7920ad805d3fc3b72fb2c46..43e6abc7c049e09d9da67c3d03cc8191644b4d6b 100644 --- a/src/edu/tyut/selaba2/student/DAO/impl/DAO.java +++ b/src/edu/tyut/selaba2/student/DAO/impl/DAO.java @@ -1,16 +1,44 @@ package edu.tyut.selaba2.student.DAO.impl; import edu.tyut.selaba2.student.DAO.BaseDAO; +import edu.tyut.selaba2.student.entity.Student; import edu.tyut.selaba2.student.entity.StudentMap; +import edu.tyut.selaba2.student.util.FileUtil; +import edu.tyut.selaba2.student.util.JSONUtil; + +import java.io.FileWriter; +import java.io.IOException; public class DAO implements BaseDAO { @Override public StudentMap getStudentMap(String simpleName) { - return null; + StudentMap sm = null; + try { + String s = FileUtil.readInFile(simpleName); + sm = JSONUtil.JSON2Entity(s,StudentMap.class); + return sm; + } catch (IOException e) { + e.printStackTrace(); + return sm; + } + } @Override public boolean saveData(StudentMap studentMap, String simpleName) { - return false; + String s = JSONUtil.entity2JSON(studentMap); + try { + FileWriter fileWriter = new FileWriter("simpleName"); + fileWriter.write(s); + fileWriter.flush(); + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } } + + + + } diff --git a/src/edu/tyut/selaba2/student/repository/impl/StudentRepositoryImpl.java b/src/edu/tyut/selaba2/student/repository/impl/StudentRepositoryImpl.java index cb58c8627e07c6d09f969779be4c08e5cc3910f3..d174564b9dcfdf4c16eb9877abf6db4ec50c7f72 100644 --- a/src/edu/tyut/selaba2/student/repository/impl/StudentRepositoryImpl.java +++ b/src/edu/tyut/selaba2/student/repository/impl/StudentRepositoryImpl.java @@ -1,29 +1,70 @@ package edu.tyut.selaba2.student.repository.impl; +import edu.tyut.selaba2.student.DAO.impl.DAO; import edu.tyut.selaba2.student.entity.Student; +import edu.tyut.selaba2.student.entity.StudentMap; import edu.tyut.selaba2.student.repository.StudentRepository; +//import jdk.internal.icu.text.UnicodeSet; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; public class StudentRepositoryImpl implements StudentRepository { + @Override public void save(Student student) { + DAO database=new DAO(); + StudentMap studentMap = database.getStudentMap("student.txt"); + if(studentMap.containsKey(student.getUid())){ + update(student); + }else{ + studentMap.put(student.getUid(),student); + database.saveData(studentMap,"student.txt"); + } } + @Override public boolean insert(Student student) { - return false; + DAO database=new DAO(); + StudentMap studentMap = database.getStudentMap("student.txt"); + if(studentMap.containsKey(student.getUid())){ + return false; + }else{ + studentMap.put(student.getUid(),student); + database.saveData(studentMap,"student.txt"); + return true; + } + } @Override public boolean delete(Long uid) { - return false; + DAO database=new DAO(); + StudentMap studentMap = database.getStudentMap("student.txt"); + if(studentMap.containsKey(uid)){ + studentMap.remove(uid); + database.saveData(studentMap,"student.txt"); +// if(studentMap.containsKey(uid)==false){ + return true; +// } + }else{ + return false; + } + } @Override public boolean update(Student student) { - return false; + DAO database=new DAO(); + StudentMap studentMap = database.getStudentMap("student.txt"); + if(studentMap.containsKey(student.getUid())){ + studentMap.put(student.getUid(),student); + return true; + }else{ + return false; + } } @Override @@ -33,7 +74,26 @@ public class StudentRepositoryImpl implements StudentRepository { @Override public List findStudentByExample(Student student) { - return null; + DAO database=new DAO(); + StudentMap studentMap = database.getStudentMap("student.txt"); + Collection studentCollection = studentMap.values(); + List selectedStudentList = studentCollection.stream().filter(e -> + (e.getUid().equals(student.getUid() == null ? e.getUid() : student.getUid()) + && e.getName().equals(student.getName() == null ? e.getName() : student.getName()) + && e.getClassNumber().equals(student.getClassNumber() == 0 ? e.getClassNumber() : student.getClassNumber()) + && e.getDepartment().equals(student.getDepartment() == null ? e.getDepartment() : student.getDepartment()) + && e.getSex().equals(student.getSex() == null ? e.getSex() : student.getSex()) + && e.getAge().equals(student.getAge() == null ? e.getAge() : student.getAge()) + ) + ).collect(Collectors.toList()); + if(selectedStudentList.size()!=0){ + return selectedStudentList; + }else{ + List nullStudentList=new ArrayList<>(); + return nullStudentList; + } + + } @Override @@ -41,3 +101,4 @@ public class StudentRepositoryImpl implements StudentRepository { return null; } } + diff --git a/src/edu/tyut/selaba2/student/util/FileUtil.java b/src/edu/tyut/selaba2/student/util/FileUtil.java index 52c29954c581805651dd96bc075dc41d0931385f..abb31273425217ed057fcdfc37d21caf4f5013a8 100644 --- a/src/edu/tyut/selaba2/student/util/FileUtil.java +++ b/src/edu/tyut/selaba2/student/util/FileUtil.java @@ -18,7 +18,14 @@ public class FileUtil { * @return true 如果写入成功 * false 如果写入失败 */ - public static boolean writeInFile(String content,String simpleName){ + public static boolean writeInFile(String content,String simpleName) throws IOException { + + String sourcePath = "data/"+ simpleName.toLowerCase() + ".txt"; + File sourceFile = new File(sourcePath); + byte[] a=content.getBytes("UTF-8"); + FileOutputStream o=new FileOutputStream(sourceFile); + o.write(a); + o.close(); return false; } @@ -31,7 +38,17 @@ public class FileUtil { * @return String 如果成功,返回数据内容 * null 如果失败返回,null */ - public static String readInFile(String simpleName){ + public static String readInFile(String simpleName) throws IOException { + String sourcePath = "data/"+ simpleName.toLowerCase() + ".txt"; + File sourceFile = new File(sourcePath); + FileInputStream o=new FileInputStream(sourceFile); + InputStreamReader fr=new InputStreamReader(o,"gbk"); + BufferedReader br=new BufferedReader(fr); + String s=new String(); + int i=0; + if ((s=br.readLine())!=null){ + return s; + } return null; }