说明
在该项目中entity建了四个实体分别是
1.Collection实体
2.User实体
3.Note实体
4.Qestions实体
Dao包只用了缓冲流与文件进行交互
代码
接口
// ICollectionDao import studio.lyl.questionbankmanagementsystem.entity.Collection; import java.util.List; /** * @Author: Re * @Date: 2020/12/7 17:32 */ public interface ICollectionDao { /** * 添加到收藏 * @param collection * @return boolean */ boolean insetCollection(Collection collection); /** *根据用户id删除收藏夹 * @param id * @return boolean */ boolean deleteCollection(Integer id) throws Exception; /** * 根据用户id更改收藏 * @param id * @param collection * @return */ boolean updateCollection(Integer id,Collection collection) throws Exception; /** * 根据用户id查看收藏夹 * @param id * @return List */ List<Collection> selectCollectionById(Integer id) throws Exception; /** *查看所有的收藏 * @return List */ List<Collection> selectCollection() throws Exception; } // INoteDao import studio.lyl.questionbankmanagementsystem.entity.Note; import java.util.List; /** * @Author: Re * @Date: 2020/12/7 18:13 */ public interface INoteDao { /** * 增加笔记 * @param note * @return boolean */ boolean insetNote(Note note) throws Exception; /** * 根据用户id删除笔记 * @param id * @return boolean */ boolean deleteNote(Integer id) throws Exception; /** * 根据用户id更改笔记 * @param id * @param note * @return */ boolean updateNote(Integer id,Note note) throws Exception; /** * 根据用户id查看笔记 * @param id * @return */ List<Note> selectNoteById(Integer id) throws Exception; /** * 查看所有的笔记 * @return */ List<Note> selectNote() throws Exception; } // IQuestionDao import studio.lyl.questionbankmanagementsystem.entity.Questions; import java.util.List; /** * @Author: Re * @Date: 2020/12/7 17:15 */ public interface IQuestionDao{ /** * 增加题目 * @param questions * @return boolean */ boolean insetQuestion(Questions questions) throws Exception; /** * 根据序号删除题目 * @param sign * @param type * @return boolean */ boolean deleteQuestion(int sign,String type) throws Exception; /** * 根据序号删除题目 * @param sign * @param questions * @param type * @return */ boolean updateQuestion(int sign ,String type ,Questions questions) throws Exception; /** * 根据序号查看题目 * @param sign * @param type * @return String */ List<Questions> selectQuestion(int sign, String type) throws Exception; /** * 查看一个类型里所有的题目 * @param type * @return List * @throws Exception */ List<Questions> selectQuestions(String type) throws Exception; } // IUserDao import studio.lyl.questionbankmanagementsystem.entity.User; import java.util.List; /** * @Author: Re * @Date: 2020/12/7 16:23 */ public interface IUserDao { /** * 创建用户 * @param user * @return boolean */ boolean insertUser(User user) throws Exception; /** * 根据id删除用户 * @param id * @return boolean */ boolean deleteUserById(Integer id) throws Exception; /** * 修改用户信息 * @param id * @return boolean */ boolean updateUser(Integer id,User user) throws Exception; /** * 根据id查看用户信息 * @param id * @return List */ List<User> selectUserById(Integer id) throws Exception; /** * 查看所有用户信息 * @return * @throws Exception */ List<User> selectUser() throws Exception; }
实现
// CollectionDaoImpl implements ICollectionDao import studio.lyl.questionbankmanagementsystem.dao.ICollectionDao; import studio.lyl.questionbankmanagementsystem.entity.Collection; import java.io.*; import java.util.ArrayList; import java.util.List; /** * @Author: Re * @Date: 2020/12/7 19:36 */ public class CollectionDaoImpl implements ICollectionDao { @Override public boolean insetCollection(Collection collection) { try { BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\Collections.txt",true)); bufferedWriter.write( collection.getTime()+","+collection.getId()+"," + collection.getUserName() + ","+ collection.getCount()+"," + collection.getSign()); bufferedWriter.newLine(); bufferedWriter.flush(); bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } return false; } @Override public boolean deleteCollection(Integer id) throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\Collections.txt")); List<Collection> collectionList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Collection collection=new Collection(strings[0],Integer.parseInt(strings[1]),strings[2],Integer.parseInt(strings[3]),Integer.parseInt(strings[4])); collectionList.add(collection); } for (int i = 0; i < collectionList.size(); i++) { Collection collection = collectionList.get(i); if (collection.getId().equals(id)) { collectionList.remove(i); } } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\Collections.txt")); for (int j = 0; j < collectionList.size(); j++) { Collection collection=collectionList.get(j); String returnFile=String.format("%s,%s,%s,%s,%s",collection.getTime(),collection.getId(),collection.getUserName(),collection.getCount(),collection.getSign()); bufferedWriter.write(returnFile); bufferedWriter.newLine(); bufferedWriter.flush(); } bufferedReader.close(); bufferedWriter.close(); return false; } @Override public boolean updateCollection(Integer id, Collection collection) throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\Collections.txt")); List<Collection> collectionList=new ArrayList<>(); String s=null; while((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Collection returnCollection = new Collection(strings[0],Integer.parseInt(strings[1]),strings[2],Integer.parseInt(strings[3]),Integer.parseInt(strings[4])); collectionList.add(returnCollection); } for (int i = 0; i < collectionList.size(); i++) { Collection returnCollection=collectionList.get(i); if(returnCollection.getId().equals(id)){ collectionList.set(i,collection); } } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\Collections.txt")); for (int i = 0; i < collectionList.size(); i++) { Collection returnCollection = collectionList.get(i); String returnString = String.format("%s,%s,%s,%s,%s",returnCollection.getTime(),returnCollection.getId(),returnCollection.getUserName(),returnCollection.getCount(),returnCollection.getSign()); bufferedWriter.write(returnString); bufferedWriter.newLine(); bufferedWriter.flush(); } bufferedReader.close(); bufferedWriter.close(); return false; } @Override public List<Collection> selectCollectionById(Integer id) throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\Collections.txt")); List<Collection> collectionList=new ArrayList<>(); String s=null; while((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Collection returnCollection = new Collection(strings[0],Integer.parseInt(strings[1]),strings[2],Integer.parseInt(strings[3]),Integer.parseInt(strings[4])); collectionList.add(returnCollection); } for (int i = 0; i < collectionList.size(); i++) { Collection collection=collectionList.get(i); if(collection.getId().equals(id)){ System.out.println(collection.toString()); } } return null; } @Override public List<Collection> selectCollection() throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\Collections.txt")); List<Collection> collectionList=new ArrayList<>(); String s=null; while((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Collection returnCollection = new Collection(strings[0],Integer.parseInt(strings[1]),strings[2],Integer.parseInt(strings[3]),Integer.parseInt(strings[4])); collectionList.add(returnCollection); } for (int i = 0; i < collectionList.size(); i++) { Collection collection=collectionList.get(i); System.out.println(collection.toString()); } return null; } } // NoteDaoImpl implements INoteDao import studio.lyl.questionbankmanagementsystem.dao.INoteDao; import studio.lyl.questionbankmanagementsystem.entity.Note; import java.io.*; import java.util.ArrayList; import java.util.List; /** * @Author: Re * @Date: 2020/12/8 8:37 */ public class NoteDaoImpl implements INoteDao { @Override public boolean insetNote(Note note) throws Exception{ BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\Note.txt",true)); bufferedWriter.write(note.getMaster()+","+note.getId()+","+note.getCount()+","+note.getSign()+","+note.getContent()); bufferedWriter.newLine(); bufferedWriter.flush(); bufferedWriter.close(); return false; } @Override public boolean deleteNote(Integer id) throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\Note.txt")); List<Note> noteList=new ArrayList<>(); String s; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Note note=new Note(strings[0],Integer.parseInt(strings[1]),Integer.parseInt(strings[2]),Integer.parseInt(strings[3]),strings[4]); System.out.println(note); noteList.add(note); } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\Note.txt")); for (int i = 0; i < noteList.size(); i++) { Note note=noteList.get(i); if(note.getId().equals(id)){ noteList.remove(i); } } for (int i = 0; i < noteList.size(); i++) { Note note=noteList.get(i); String returnString=String.format("%s,%s,%s,%s,%s",note.getMaster(),note.getId(),note.getCount(),note.getSign(),note.getContent()); bufferedWriter.write(returnString); } bufferedReader.close(); bufferedWriter.flush(); bufferedWriter.close(); return false; } @Override public boolean updateNote(Integer id, Note note) throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\Note.txt")); List<Note> noteList=new ArrayList<>(); String s; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Note noteGet=new Note(strings[0],Integer.parseInt(strings[1]),Integer.parseInt(strings[2]),Integer.parseInt(strings[3]),strings[4]); System.out.println(noteGet); noteList.add(noteGet); } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\Note.txt")); for (int i = 0; i < noteList.size(); i++) { Note noteSet=noteList.get(i); if(noteSet.getId().equals(id)) { noteList.add(note.getSign(), note); } } for (int i = 0; i < noteList.size(); i++) { Note returnNote=noteList.get(i); String returnString=String.format("%s,%s,%s,%s,%s",returnNote.getMaster(),returnNote.getId(),returnNote.getCount(),returnNote.getSign(),returnNote.getContent()); bufferedWriter.write(returnString); } bufferedReader.close(); bufferedWriter.flush(); bufferedWriter.close(); return false; } @Override public List<Note> selectNoteById(Integer id) throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\Note.txt")); List<Note> noteList=new ArrayList<>(); String s; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Note note=new Note(strings[0],Integer.parseInt(strings[1]),Integer.parseInt(strings[2]),Integer.parseInt(strings[3]),strings[4]); noteList.add(note); } for (int i = 0; i < noteList.size(); i++) { Note note=noteList.get(i); if(note.getId().equals(id)){ System.out.println(note); } } bufferedReader.close(); return null; } @Override public List<Note> selectNote() throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\Note.txt")); List<Note> noteList=new ArrayList<>(); String s; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Note note=new Note(strings[0],Integer.parseInt(strings[1]),Integer.parseInt(strings[2]),Integer.parseInt(strings[3]),strings[4]); noteList.add(note); } for (int i = 0; i < noteList.size(); i++) { Note note=noteList.get(i); System.out.println(note); } return null; } } // QuestionDaoImpl implements IQuestionDao import studio.lyl.questionbankmanagementsystem.dao.IQuestionDao; import studio.lyl.questionbankmanagementsystem.entity.Questions; import java.io.*; import java.util.ArrayList; import java.util.List; /** * @Author: Re * @Date: 2020/12/8 20:18 */ public class QuestionDaoImpl implements IQuestionDao { @Override public boolean insetQuestion(Questions questions) throws Exception{ //这里不想把路径写死就做了如下处理 BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(("QuestionBankManagementSystem\\Questions\\".endsWith("/") ? "QuestionBankManagementSystem\\Questions\\":"QuestionBankManagementSystem\\Questions\\"+"/")+questions.getType()+".txt",true)); bufferedWriter.write(questions.getContent()+","+questions.getType()+","+questions.getSign()+","+questions.getComplexity()+","+questions.getAnswer()); bufferedWriter.newLine(); bufferedWriter.flush(); bufferedWriter.close(); return false; } @Override public boolean deleteQuestion(int sign,String type) throws Exception { BufferedReader bufferedReader=new BufferedReader(new FileReader(("QuestionBankManagementSystem\\Questions\\".endsWith("/") ? "QuestionBankManagementSystem\\Questions\\":"QuestionBankManagementSystem\\Questions\\"+"/")+type+".txt")); List<Questions> questionsList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Questions questions=new Questions(strings[0],strings[1],Integer.parseInt(strings[2]),strings[3],strings[4]); questionsList.add(questions); } for (int i = 0; i < questionsList.size(); i++) { Questions questions=questionsList.get(i); if(questions.getSign()==sign){ questionsList.remove(i); } } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(("QuestionBankManagementSystem\\Questions\\".endsWith("/") ? "QuestionBankManagementSystem\\Questions\\":"QuestionBankManagementSystem\\Questions\\"+"/")+type+".txt")); for (int i = 0; i < questionsList.size(); i++) { Questions questions=questionsList.get(i); String returnString=String.format("%s,%s,%s,%s,%s",questions.getContent(),questions.getType(),questions.getSign(),questions.getComplexity(),questions.getAnswer()); bufferedWriter.write(returnString); bufferedWriter.newLine(); } bufferedReader.close(); bufferedWriter.flush(); bufferedWriter.close(); return false; } @Override public boolean updateQuestion(int sign,String type,Questions questions) throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader(("QuestionBankManagementSystem\\Questions\\".endsWith("/") ? "QuestionBankManagementSystem\\Questions\\":"QuestionBankManagementSystem\\Questions\\"+"/")+type+".txt")); List<Questions> questionsList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Questions questionsSet=new Questions(strings[0],strings[1],Integer.parseInt(strings[2]),strings[3],strings[4]); questionsList.add(questionsSet); } for (int i = 0; i < questionsList.size(); i++) { Questions questionsSet=questionsList.get(i); if(questionsSet.getSign()==sign){ questionsList.set(i,questions); } } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(("QuestionBankManagementSystem\\Questions\\".endsWith("/") ? "QuestionBankManagementSystem\\Questions\\":"QuestionBankManagementSystem\\Questions\\"+"/")+type+".txt")); for (int i = 0; i < questionsList.size(); i++) { Questions questionsSet=questionsList.get(i); String returnString=String.format("%s,%s,%s,%s,%s",questionsSet.getContent(),questionsSet.getType(),questionsSet.getSign(),questionsSet.getComplexity(),questionsSet.getAnswer()); bufferedWriter.write(returnString); bufferedWriter.newLine(); } bufferedReader.close(); bufferedWriter.flush(); bufferedWriter.close(); return false; } @Override public List<Questions> selectQuestion(int sign,String type) throws Exception { BufferedReader bufferedReader=new BufferedReader(new FileReader(("QuestionBankManagementSystem\\Questions\\".endsWith("/") ? "QuestionBankManagementSystem\\Questions\\":"QuestionBankManagementSystem\\Questions\\"+"/")+type+".txt")); List<Questions> questionsList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Questions questions=new Questions(strings[0],strings[1],Integer.parseInt(strings[2]),strings[3],strings[4]); questionsList.add(questions); } for (int i = 0; i < questionsList.size(); i++) { Questions questions=questionsList.get(i); if(questions.getSign()==sign){ System.out.println(questions.getContent()); } } return null; } @Override public List<Questions> selectQuestions(String type) throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader(("QuestionBankManagementSystem\\Questions\\".endsWith("/") ? "QuestionBankManagementSystem\\Questions\\":"QuestionBankManagementSystem\\Questions\\"+"/")+type+".txt")); List<Questions> questionsList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); Questions questions=new Questions(strings[0],strings[1],Integer.parseInt(strings[2]),strings[3],strings[4]); questionsList.add(questions); } for (int i = 0; i < questionsList.size(); i++) { Questions questions=questionsList.get(i); System.out.println(questions.getContent()); } return null; } } // UserDaoImpl implements IUserDao import studio.lyl.questionbankmanagementsystem.dao.IUserDao; import studio.lyl.questionbankmanagementsystem.entity.User; import java.io.*; import java.util.ArrayList; import java.util.List; /** * @Author: Re * @Date: 2020/12/8 22:12 */ public class UserDaoImpl implements IUserDao { @Override public boolean insertUser(User user) throws Exception { BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\User.txt")); List<User> userList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); User userGet=new User(Integer.parseInt(strings[0]),strings[1],strings[2]); userList.add(userGet); } for (int i = 0; i < userList.size(); i++) { User userGet=userList.get(i); if(userGet.getId()==user.getId()){ System.out.println("用户已存在,请重新输入"); }else { userList.add(user); } } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\User.txt",true)); for (int i = 0; i < userList.size(); i++) { User userGet=userList.get(i); String returnString=String.format("%s,%s,%s",userGet.getId(),userGet.getPassword(),userGet.getUserName()); bufferedWriter.write(returnString); bufferedWriter.newLine(); } bufferedReader.close(); bufferedWriter.flush(); bufferedReader.close(); return false; } @Override public boolean deleteUserById(Integer id) throws Exception { BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\User.txt")); List<User> userList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); User user=new User(Integer.parseInt(strings[0]),strings[1],strings[2]); userList.add(user); } for (int i = 0; i < userList.size(); i++) { User userGet=userList.get(i); if(userGet.getId()==id) { userList.remove(i); } } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\User.txt")); for (int i = 0; i < userList.size(); i++) { User user=userList.get(i); String returnString=String.format("%s,%s,%s",user.getId(),user.getPassword(),user.getUserName()); bufferedWriter.write(returnString); bufferedWriter.newLine(); } bufferedReader.close(); bufferedWriter.flush(); bufferedReader.close(); return false; } @Override public boolean updateUser(Integer id,User user) throws Exception { BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\User.txt")); List<User> userList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); User userGet=new User(Integer.parseInt(strings[0]),strings[1],strings[2]); userList.add(userGet); } for (int i = 0; i < userList.size(); i++) { User userGet=userList.get(i); if(userGet.getId()==id) { userList.set(i,user); } } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QuestionBankManagementSystem\\User.txt")); for (int i = 0; i < userList.size(); i++) { User userGet=userList.get(i); String returnString=String.format("%s,%s,%s",userGet.getId(),userGet.getPassword(),userGet.getUserName()); bufferedWriter.write(returnString); bufferedWriter.newLine(); } bufferedReader.close(); bufferedWriter.flush(); bufferedReader.close(); return false; } @Override public List<User> selectUserById(Integer id) throws Exception { BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\User.txt")); List<User> userList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); User user=new User(Integer.parseInt(strings[0]),strings[1],strings[2]); userList.add(user); } for (int i = 0; i < userList.size(); i++) { User user=userList.get(i); if(user.getId()==id){ System.out.println(user); } } return null; } @Override public List<User> selectUser() throws Exception{ BufferedReader bufferedReader=new BufferedReader(new FileReader("QuestionBankManagementSystem\\User.txt")); List<User> userList=new ArrayList<>(); String s=null; while ((s=bufferedReader.readLine())!=null){ String[] strings=s.split(","); User user=new User(Integer.parseInt(strings[0]),strings[1],strings[2]); userList.add(user); } for (int i = 0; i < userList.size(); i++) { User user=userList.get(i); System.out.println(user); } return null; } }