1.图书类用户类按照文件进行定义,里面数据用io流进行读写
package main; public class Book { private String b_number; private String b_name; private String b_author; private String b_press; private String b_date; private int b_quantity; private String b_price; public Book() { } public String getB_number() { return b_number; } public void setB_number(String b_number) { this.b_number = b_number; } public String getB_name() { return b_name; } public void setB_name(String b_name) { this.b_name = b_name; } public String getB_author() { return b_author; } public void setB_author(String b_author) { this.b_author = b_author; } public String getB_press() { return b_press; } public void setB_press(String b_press) { this.b_press = b_press; } public String getB_date() { return b_date; } public void setB_date(String b_date) { this.b_date = b_date; } public int getB_quantity() { return b_quantity; } public void setB_quantity(int b_quantity) { this.b_quantity = b_quantity; } public String getB_price() { return b_price; } public void setB_price(String b_price) { this.b_price = b_price; } public void printbook(){ System.out.println("The number of book: "+b_number); System.out.println("The name of book: "+b_name); System.out.println("The author of book: "+b_author); System.out.println("The press of book: "+b_press); System.out.println("The date of book: "+b_date); System.out.println("The quantity of book: "+b_quantity); System.out.println("The price of book: "+b_price); } }
再是User类
package main; public class User { public String id; public String name; public String college; public String major; public String cla; public String sex; public User(String id, String name, String college, String major, String cla, String sex) { this.id = id; this.name = name; this.college = college; this.major = major; this.cla = cla; this.sex = sex; } public User() { } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCollege() { return college; } public void setCollege(String college) { this.college = college; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getCla() { return cla; } public void setCla(String cla) { this.cla = cla; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void printuser(){ System.out.println("The id of user: "+id); System.out.println("The name of user: "+name); System.out.println("The college of user: "+college); System.out.println("The major of user: "+major); System.out.println("The class of user: "+cla); System.out.println("The sex of user: "+sex); } }
2.之后再在主方法里进行增删改查等工作。
package main; import java.io.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.Collections; import java.util.Comparator; public class Main { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new FileReader("User.txt")); List<User> uarr = new ArrayList<>(); String line; while((line=br.readLine())!=null){ String[] Strarr = line.split(","); User u=new User(); u.setId(Strarr[0]); u.setName(Strarr[1]); u.setCollege(Strarr[2]); u.setMajor(Strarr[4]); u.setCla(Strarr[3]); u.setSex(Strarr[5]); uarr.add(u); } br.close(); List<Book> barr = new ArrayList<>(); BufferedReader br2=new BufferedReader(new FileReader("Book.txt")); String line2; while((line2=br2.readLine())!=null){ String[] Strarr2 = line2.split(","); Book b=new Book(); b.setB_number(Strarr2[0]); b.setB_name(Strarr2[1]); b.setB_author(Strarr2[2]); b.setB_press(Strarr2[3]); b.setB_date(Strarr2[4]); b.setB_quantity(Integer.valueOf(Strarr2[5])); b.setB_price(Strarr2[6]); barr.add(b); } br2.close(); List<Borrow> larr = new ArrayList<>(); BufferedReader br3=new BufferedReader(new FileReader("Borrow.txt")); String line3; while((line3=br3.readLine())!=null){ String[] Starr3 = line3.split(","); Borrow l=new Borrow(); l.setUid(Starr3[0]); l.setBid(Starr3[1]); l.setBtime(Starr3[2]); larr.add(l); } br3.close(); Menu(barr, uarr,larr); } public static void menu() { System.out.println("-----------------------------------------------------------------"); System.out.println("-------Welcome to the library management system of sdau!---------"); System.out.println("-------Please select the object you want to operate on-----------"); System.out.println("-------1.Book information table -------------------------"); System.out.println("-------2.User information table -------------------------"); System.out.println("-------3.Borrowing information form -------------------------"); System.out.println("-------4.Exit the system -------------------------"); System.out.println("-----------------------------------------------------------------"); } public static void usersmenu() { System.out.println("-----------------------------------------------------------------"); System.out.println("-------Welcome to the library management system of sdau!---------"); System.out.println("-------Please select the object you want to operate on-----------"); System.out.println("-------1.Reader information addition -------------------------"); System.out.println("-------2.Reader information query -------------------------"); System.out.println("-------3.Reader information ranking -------------------------"); System.out.println("-------4.Reader information modification-------------------------"); System.out.println("-------5.Reader information delete -------------------------"); System.out.println("-------6.Reader information show -------------------------"); System.out.println("-------7.Return to the previous level -------------------------"); System.out.println("-----------------------------------------------------------------"); } public static void bookmenu() { System.out.println("-----------------------------------------------------------------"); System.out.println("-------Welcome to the library management system of sdau!---------"); System.out.println("-------Please select the object you want to operate on-----------"); System.out.println("-------1.Book information addition ---------------------------"); System.out.println("-------2.Book information query ---------------------------"); System.out.println("-------3.Book information ranking ---------------------------"); System.out.println("-------4.Book information modification---------------------------"); System.out.println("-------5.Book information delete ---------------------------"); System.out.println("-------6.Book information show ---------------------------"); System.out.println("-------7.Return to the previous level -------------------------"); System.out.println("-----------------------------------------------------------------"); } public static void borrowmenu(){ System.out.println("-----------------------------------------------------------------"); System.out.println("-------Welcome to the library management system of sdau!---------"); System.out.println("-------Please select the object you want to operate on-----------"); System.out.println("-------1.Borrow book ---------------------------"); System.out.println("-------2.Return book ---------------------------"); System.out.println("-------3.Return to the previous level ---------------------------"); System.out.println("-----------------------------------------------------------------"); } public static void Menu(List<Book> barr, List<User> uarr,List<Borrow> larr) throws IOException { Scanner sc = new Scanner(System.in); while (true) { menu(); int n = sc.nextInt(); switch (n) { case 1: Bookmenu(barr, uarr,larr); break; case 2: Usersmenu(barr, uarr,larr); break; case 3: Borrowmenu( barr, uarr, larr); break; case 4: BufferedWriter bw = new BufferedWriter(new FileWriter("User.txt")); for (User ur : uarr) { StringBuilder ub = new StringBuilder(); ub.append(ur.getId()).append(",").append(ur.getName()).append(",").append(ur.getCollege()).append(",").append(ur.getCla()).append(",").append(ur.getMajor()).append(",").append(ur.getSex()); bw.write(ub.toString()); bw.newLine(); bw.flush(); } bw.close(); BufferedWriter bw2 = new BufferedWriter(new FileWriter("Book.txt")); for (Book br : barr) { StringBuilder ub = new StringBuilder(); ub.append(br.getB_number()).append(",").append(br.getB_name()).append(",").append(br.getB_author()).append(",").append(br.getB_press()).append(",").append(br.getB_date()).append(",").append(br.getB_quantity()).append(",").append(br.getB_price()); bw2.write(ub.toString()); bw2.newLine(); bw2.flush(); } bw2.close(); BufferedWriter bw3 = new BufferedWriter(new FileWriter("Borrow.txt")); for (Borrow lr : larr) { StringBuilder ub = new StringBuilder(); ub.append(lr.getUid()).append(",").append(lr.getBid()).append(",").append(lr.getBtime()).append(",").append(lr.getRtime()); bw3.write(ub.toString()); bw3.newLine(); bw3.flush(); } bw3.close(); System.exit(0); default: System.out.println("Input error,please re-input!"); } } } public static void Usersmenu(List<Book> barr, List<User> uarr,List<Borrow> larr) throws IOException { while (true) { usersmenu(); Scanner sc = new Scanner(System.in); int n2 = sc.nextInt(); switch (n2) { case 1: Adduser(uarr); break; case 2: Inquiryuser(uarr); break; case 3: RankUser(uarr); break; case 4: ModificationUsers(uarr); break; case 5: DeleteUsers(uarr); break; case 6: ShowUsers(uarr); break; case 7: Menu(barr, uarr,larr); break; default: System.out.println("1Input error,please re-input!"); } } } public static void Borrowmenu(List<Book> barr, List<User> uarr,List<Borrow> larr) throws IOException { while (true) { borrowmenu(); Scanner sc = new Scanner(System.in); int n3 = sc.nextInt(); switch (n3) { case 1: Addbcode(barr,uarr,larr); break; case 2: Addrcode(barr,uarr,larr); break; case 3: Menu(barr, uarr,larr); break; default: System.out.println("1Input error,please re-input!"); } } } public static void Bookmenu(List<Book> barr, List<User> uarr,List<Borrow> larr) throws IOException { while (true) { bookmenu(); Scanner sc = new Scanner(System.in); int n1 = sc.nextInt(); switch (n1) { case 1: Addbook(barr); break; case 2: Inquirybook(barr); break; case 3: Rankbook(barr); break; case 4: ModificationBooks(barr); break; case 5: Deletebook(barr); break; case 6: ShowBooks(barr); break; case 7: Menu(barr, uarr,larr); break; default: System.out.println("2Input error,please re-input!"); } } } public static void Adduser(List<User> urr) throws IOException { Scanner sc = new Scanner(System.in); System.out.println("Please enter your id: "); String i = sc.nextLine(); System.out.println("Please enter your name: "); String n = sc.nextLine(); System.out.println("Please enter your college: "); String c1 = sc.nextLine(); System.out.println("Please enter your class: "); String c2 = sc.nextLine(); System.out.println("Please enter your major: "); String m = sc.nextLine(); System.out.println("Please enter your sex: "); String s = sc.nextLine(); User u = new User(); u.setCollege(c1); u.setId(i); u.setName(n); u.setCla(c2); u.setMajor(m); u.setSex(s); urr.add(u); // BufferedWriter bw = new BufferedWriter(new FileWriter("User.txt")); // for (User ur : urr) { // StringBuilder ub = new StringBuilder(); // ub.append(ur.getId()).append(",").append(ur.getName()).append(",").append(ur.getCollege()).append(",").append(ur.getCla()).append(",").append(ur.getMajor()).append(",").append(ur.getSex()); // bw.write(ub.toString()); // bw.newLine(); // bw.flush(); // } // // bw.close(); System.out.println("Congratulation!Added successfully!"); } public static void Addbook(List<Book> brr) throws IOException { Scanner sc = new Scanner(System.in); System.out.println("Please enter the number of books: "); String n2 = sc.nextLine(); System.out.println("Please enter the name of books: "); String n1 = sc.nextLine(); System.out.println("Please enter the author of books: "); String a = sc.nextLine(); System.out.println("Please enter the press of books: "); String p1 = sc.nextLine(); System.out.println("Please enter the publication date of books: "); String d = sc.nextLine(); System.out.println("Please enter the quantity of the book: "); int q = Integer.parseInt(sc.nextLine()); System.out.println("Please enter the price of the book: "); String p2 = sc.nextLine(); Book b = new Book(); b.setB_name(n1); b.setB_number(n2); b.setB_press(p1); b.setB_date(d); b.setB_quantity(q); b.setB_price(p2); b.setB_author(a); brr.add(b); // BufferedWriter bw = new BufferedWriter(new FileWriter("Book.txt")); // for (Book br : brr) { // StringBuilder ub = new StringBuilder(); // ub.append(br.getB_number()).append(",").append(br.getB_name()).append(",").append(br.getB_author()).append(",").append(br.getB_press()).append(",").append(br.getB_date()).append(",").append(br.getB_quantity()).append(",").append(br.getB_price()); // bw.write(ub.toString()); // bw.newLine(); // bw.flush(); // } // // bw.close(); System.out.println("Congratulation!Added successfully!"); } public static void Addbcode(List<Book> barr, List<User> uarr,List<Borrow> larr){ Scanner sc = new Scanner(System.in); System.out.println("Please enter id of books: "); String b1 = sc.nextLine(); System.out.println("Please enter your id: "); String u = sc.nextLine(); Borrow b=new Borrow(); Date d=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String t=sdf.format(d); int n=0; int m=0; for (Book br : barr) { if (b1.equals(br.getB_number())) { n++; } } for(User ur:uarr) if (u.equals(ur.getId())) m++; if(n!=0&&m!=0){ b.setBtime(t); b.setBid(b1); for (Book br : barr) { if (b1.equals(br.getB_number())) { br.setB_quantity(br.getB_quantity()-1); } } b.setUid(u); larr.add(b); System.out.println("Borrow successfully!"); } if(n==0&&m!=0) System.out.println("There is no such book yet!"); if(n!=0&&m==0) System.out.println("Please add user first!"); if(n==0&&m==0) System.out.println("Please add user first!There is no such book yet!"); } public static void Addrcode(List<Book> barr, List<User> uarr,List<Borrow> larr){ Scanner sc = new Scanner(System.in); System.out.println("Please enter id of books: "); String b1 = sc.nextLine(); System.out.println("Please enter your id: "); String u = sc.nextLine(); int n=0; int m=0; for (Borrow lr : larr) { if (b1.equals(lr.getBid())) n++; if (u.equals(lr.getUid())) m++; if(n!=0&&m!=0){ Date d=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String t=sdf.format(d); lr.setRtime(t); System.out.println(lr.getRtime()); for (Book br : barr) { if (b1.equals(br.getB_number())) { br.setB_quantity(br.getB_quantity()+1); } } System.out.println("Return successfully!"); break; } } if(n==0&&m!=0){ System.out.println("Please add user first!"); } if(n!=0&&m==0){ System.out.println("Please add user first!"); } } public static void Inquirybook(List<Book> brr) { Scanner sc = new Scanner(System.in); int n = 0; if (brr.size() > 0) { System.out.println("Please enter the information you want to search!"); String s = sc.next(); for (Book br : brr) { // if(s.equals(br.getB_author()))n++; //if(s.equals(br.getB_date()))n++; if (s.equals(br.getB_name())) n++; if (s.equals(br.getB_number())) n++; // if(s.equals(br.getB_press()))n++; // if(s.equals(br.getB_price()))n++; // if(s.equals(br.getB_quantity()))n++; // if (n != 0) { // br.printbook(); // } else { // System.out.println("The book was not found!"); // } if (n != 0) { br.printbook(); } } } else { System.out.println("Please add book first!"); } if (n == 0) { System.out.println("The book was not found!"); } } public static void Inquiryuser(List<User> urr) { Scanner sc = new Scanner(System.in); int n = 0; if (urr.size() > 0) { System.out.println("Please enter the information you want to search!"); String s = sc.next(); for (User ur : urr) { // if(s.equals(ur.getB_author()))n++; //if(s.equals(ur.getB_date()))n++; if (s.equals(ur.name)) n++; if (s.equals(ur.id)) n++; // if(s.equals(ur.getB_press()))n++; // if(s.equals(ur.getB_price()))n++; // if(s.equals(ur.getB_quantity()))n++; if (n != 0) { ur.printuser(); } } } else { System.out.println("Please add user first!"); } if(n==0){ System.out.println("The user was not found!"); } } public static void Rankbook(List<Book> brr) { if (brr.size() > 1) { for (int i = 0; i < brr.size() - 1; i++) { for (int j = i; j < brr.size() - 1 - i; j++) { if (Integer.valueOf(brr.get(j).getB_number()) > Integer.valueOf(brr.get(j + 1).getB_number())) { Collections.swap(brr, j, j + 1); } } } // brr.sort(new Comparator<Book>() { // @Override // public int compare(Book brr1, Book brr2) { // return Integer.valueOf(brr1.getB_number())-Integer.valueOf(brr2.getB_number()); // } // }); System.out.println("Sorting succeeded!"); } else { System.out.println("There are so few books that they can't be sorted!"); } } public static void RankUser(List<User> urr) { //System.out.println(urr.size()); if (urr.size() > 1) { for (int i = 0; i < urr.size() - 1; i++) { for (int j = 0; j < urr.size() - 1 - i; j++) { if (Integer.valueOf(urr.get(j).getId()) > Integer.valueOf(urr.get(j + 1).getId())) { Collections.swap(urr, j, j + 1); } } } // urr.sort(new Comparator<User>() { // @Override // public int compare(User urr1, User urr2) { // return Integer.valueOf(urr1.getId())-Integer.valueOf(urr2.getId()); // } // }); // Collections.sort(urr); // for (User ur : urr) { // ur.printuser(); // } System.out.println("Sorting succeeded!"); } else { System.out.println("There are so few users that they can't be sorted!"); } } // private static List<User> getUrr(List<User> urr) { // return urr; // } // // private static Book getBook(ArrayList<Book> brr, int j) { // return brr.get(j); // } public static void ShowUsers(List<User> urr) { if (urr.size() > 0) { for (User ur : urr) { ur.printuser(); } } else { System.out.println("There are so few users that they can't be sorted!"); } } public static void ShowBooks(List<Book> brr) { if (brr.size() > 0) { for (Book br : brr) { br.printbook(); } } else { System.out.println("There are so few users that they can't be sorted!"); } } public static void DeleteUsers(List<User> urr){ Scanner sc=new Scanner(System.in); int n = 0; if (urr.size() > 0) { System.out.println("Please enter the student information you want to delete!"); String s = sc.next(); for (int i=0;i<urr.size();i++) { // if(s.equals(ur.getB_author()))n++; //if(s.equals(ur.getB_date()))n++; if (s.equals(urr.get(i).name)) n++; if (s.equals(urr.get(i).id)) n++; // if(s.equals(ur.getB_press()))n++; // if(s.equals(ur.getB_price()))n++; // if(s.equals(ur.getB_quantity()))n++; if (n != 0) { urr.remove(i); System.out.println("Delete successfully!"); break; } } } else { System.out.println("Please add user first!"); } if(n==0){ System.out.println("The user was not found!"); } } public static void Deletebook(List<Book> brr) { Scanner sc = new Scanner(System.in); int n = 0; if (brr.size() > 0) { System.out.println("Please enter the book information you want to delete!"); String s = sc.next(); for (int i=0;i<brr.size();i++) { // if(s.equals(br.getB_author()))n++; //if(s.equals(br.getB_date()))n++; if (s.equals(brr.get(i).getB_name())) n++; if (s.equals(brr.get(i).getB_number())) n++; // if(s.equals(br.getB_press()))n++; // if(s.equals(br.getB_price()))n++; // if(s.equals(br.getB_quantity()))n++; // if (n != 0) { // br.printbook(); // } else { // System.out.println("The book was not found!"); // } if (n != 0) { brr.remove(i); System.out.println("Delete successfully!"); break; } } } else { System.out.println("Please add book first!"); } if (n == 0) { System.out.println("The book was not found!"); } } public static void ModificationUsers(List<User> urr){ Scanner sc = new Scanner(System.in); System.out.println("Please enter the users information you want modify:"); String a=sc.next(); System.out.println("Please enter your id: "); String i = sc.next(); System.out.println("Please enter your name: "); String n = sc.next(); System.out.println("Please enter your college: "); String c1 = sc.next(); System.out.println("Please enter your class: "); String c2 = sc.next(); System.out.println("Please enter your major: "); String m = sc.next(); System.out.println("Please enter your sex: "); String s = sc.next(); User u = new User(); u.setCollege(c1); u.setId(i); u.setName(n); u.setCla(c2); u.setMajor(m); u.setSex(s); int b = 0; if (urr.size() > 0) { // System.out.println("Please enter the student information you want to delete!"); for (int j=0;j<urr.size();j++) { // if(s.equals(ur.getB_author()))n++; //if(c.equals(ur.getB_date()))n++; if (a.equals(urr.get(j).name)) b++; if (a.equals(urr.get(j).id)) b++; // if(s.equals(ur.getB_press()))n++; // if(s.equals(ur.getB_price()))n++; // if(s.equals(ur.getB_quantity()))n++; if (b != 0) { urr.set(j,u); System.out.println("Modify successfully!"); break; } } } else { System.out.println("Please add user first!"); } if(b==0){ System.out.println("The user was not found!"); } } public static void ModificationBooks(List<Book> brr){ Scanner sc = new Scanner(System.in); System.out.println("Please enter the books information you want modify:"); String e=sc.next(); System.out.println("Please enter the number of books: "); String n2 = sc.next(); System.out.println("Please enter the name of books: "); String n1 = sc.next(); System.out.println("Please enter the author of books: "); String a = sc.next(); System.out.println("Please enter the press of books: "); String p1 = sc.next(); System.out.println("Please enter the publication date of books: "); String d = sc.next(); System.out.println("Please enter the quantity of the book: "); String q = sc.next(); System.out.println("Please enter the price of the book: "); String p2 = sc.next(); Book b = new Book(); b.setB_name(n1); b.setB_number(n2); b.setB_press(p1); b.setB_date(d); b.setB_quantity(Integer.parseInt(q)); b.setB_price(p2); b.setB_author(a); int f = 0; if (brr.size() > 0) { System.out.println("Please enter the book information you want to delete!"); String s = sc.next(); for (int i=0;i<brr.size();i++) { // if(s.equals(br.getB_author()))n++; //if(s.equals(br.getB_date()))n++; if (e.equals(brr.get(i).getB_number())) f++; if (e.equals(brr.get(i).getB_name())) f++; // if(s.equals(br.getB_press()))n++; // if(s.equals(br.getB_price()))n++; // if(s.equals(br.getB_quantity()))n++; // if (n != 0) { // br.printbook(); // } else { // System.out.println("The book was not found!"); // } if (f != 0) { brr.set(i,b); System.out.println("Modify successfully!"); break; } } } else { System.out.println("Please add book first!"); } if (f == 0) { System.out.println("The book was not found!"); } // int b = 0; // if (urr.size() > 0) { // // System.out.println("Please enter the student information you want to delete!"); // for (int j=0;j<urr.size();j++) { // // if(s.equals(ur.getB_author()))n++; // //if(c.equals(ur.getB_date()))n++; // if (a.equals(urr.get(j).name)) b++; // if (a.equals(urr.get(j).id)) b++; // // if(s.equals(ur.getB_press()))n++; // // if(s.equals(ur.getB_price()))n++; // // if(s.equals(ur.getB_quantity()))n++; // if (b != 0) { // urr.set(j,u); // System.out.println("Modify successfully!"); // break; // } // } // } else { // System.out.println("Please add user first!"); // } // if(b==0){ // System.out.println("The user was not found!"); // } } }
总结
历经一周的时间,图书管理系统虽然还有一些缺陷但也算是该告一段落了。最初写图书管理系统的时候本以为很简单,但随着思考的深入,功能的完善,我渐渐发现这个系统中的每个功能,每个操作类与数据类之间都是有联系的。如果弄不清楚各个数据类与操作类之间的关联,不明白它们应该在哪使用,那写出来的代码会混乱不堪,在功能的实现上也会出现各种问题;反之,如果我们理解了它们之间内在的关联,那便会使我们的思路更加清晰,更加有利于功能的实现。
当然,在写系统的过程中除了思路上的混乱之外还有很多知识上的漏洞暴露了出来。就拿文件的读取与写入来说,有时是代码的错误,有时是读取与写入的文件格式有着差异,这都会造成程序在该读取时读不出,该写入时写不进去,成为我们眼前的一大难题。
一个周的时间,随着思路的清晰,知识点的熟练,代码也在一点点简化。回顾编写的过程,我认为知识点的运用很关键。系统的每一次修改都让我对所学知识的理解与运用更为深刻与熟练