当前位置:   article > 正文

Java图书馆_javalibrary

javalibrary

io流用的不是很熟练,还有Book类的应用出了点问题,越改越错,从2个错误改到102个QAQ,孩子想哭,问了好多人也没改成,最后勉强成型,而且上个星期内分泌系统出了点小问题,天天往医院跑,开始敲的太晚了,现在要备战期末考,等期末考结束再改改Book类吧。

主函数和界面

io流

操作

  1.   添加
  2. 修改
  3. 查找
  4. 排序
  5. 删除

基本类

  1. Book
  2. Borrow
  3. Reader

主函数和界面

  1. package view;
  2. import functions.Libraryborrow;
  3. import functions.LibraryinformationManagement;
  4. import functions.LibraryreaderManagement;
  5. import java.io.IOException;
  6. import java.text.ParseException;
  7. public class Main {
  8. public static void main(String[] args) throws IOException, ClassNotFoundException, ParseException {
  9. boolean b = true;
  10. while (b) {
  11. System.out.println("*********************");
  12. System.out.println("1、进入图书管理系统");
  13. System.out.println("2、进入读者管理系统");
  14. System.out.println("3、借阅、归还书籍");
  15. System.out.println("4、退出系统");
  16. System.out.println("*********************");
  17. char c = Tool.tools.readMenuSelection();
  18. switch (c) {
  19. case '1':
  20. LibraryinformationManagement.bookmew();
  21. break;
  22. case '2':
  23. LibraryreaderManagement.readermew();
  24. break;
  25. case '3':
  26. Libraryborrow.sentaku();
  27. break;
  28. case '4':
  29. System.out.println("谢谢使用");
  30. System.exit(0);
  31. break;
  32. }
  33. }
  34. }
  35. }
  1. package functions;
  2. import EntityClass.Book;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6. import static functions.Add.AddBook;
  7. import static functions.Find.findbook;
  8. public class LibraryinformationManagement {
  9. public static void bookmew() throws IOException, ClassNotFoundException {
  10. ArrayList<Book> array=new ArrayList<Book>();
  11. while (true) {
  12. System.out.println("1.添加图书");
  13. System.out.println("2.查询图书");
  14. System.out.println("3.图书排序");
  15. System.out.println("4.修改或删除图书");
  16. System.out.println("5.返回上一级");
  17. char c = Tool.tools.readMenuSelection();
  18. switch (c) {
  19. case '1':
  20. AddBook(array);
  21. break;
  22. case '2':
  23. findbook(array);
  24. break;
  25. case '3':
  26. sort.sentakusort();
  27. break;
  28. case '4':
  29. sentaku1();
  30. Change.change1();
  31. break;
  32. case '5':
  33. return;
  34. }
  35. }
  36. }
  37. public static void sentaku1() throws IOException{
  38. System.out.println("您要进行修改还是删除?:"+"\n"+"1、修改\n2、删除");
  39. int s;
  40. Scanner sc = new Scanner(System.in);
  41. while (true)
  42. {
  43. s = sc.nextInt();
  44. if(s!=1&&s!=2)
  45. System.out.println("输入有误,请重新输入");
  46. else break;
  47. }
  48. switch (s){
  49. case 1:
  50. Change.sentakuchange1();
  51. break;
  52. case 2:
  53. Delete.sentakudelete1();
  54. break;
  55. }
  56. }
  57. }
  1. package functions;
  2. import EntityClass.Reader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6. public class LibraryreaderManagement {
  7. public static void readermew() throws IOException, ClassNotFoundException {
  8. ArrayList<Reader> array=new ArrayList<Reader>();
  9. while (true) {
  10. System.out.println("1.添加读者信息");
  11. System.out.println("2.查询读者信息");
  12. System.out.println("3.读者信息排序");
  13. System.out.println("4.修改或删除读者信息");
  14. System.out.println("5.返回上一级");
  15. Scanner sc = new Scanner(System.in);
  16. String line = sc.nextLine();
  17. switch (line) {
  18. case "1":
  19. Add.AddReader(array);
  20. break;
  21. case "2":
  22. Find.findreader(array);
  23. break;
  24. case "3":
  25. sort.sentakusortreader();
  26. break;
  27. case "4":
  28. sentaku1();
  29. break;
  30. case "5":
  31. return;
  32. }
  33. }
  34. }
  35. public static void sentaku1() throws IOException{
  36. System.out.println("您要进行修改还是删除?:"+"\n"+"1、修改\n2、删除");
  37. int s;
  38. Scanner sc = new Scanner(System.in);
  39. while (true)
  40. {
  41. s = sc.nextInt();
  42. if(s!=1&&s!=2)
  43. System.out.println("输入有误,请重新输入");
  44. else break;
  45. }
  46. switch (s){
  47. case 1:
  48. Change.change3();
  49. break;
  50. case 2:
  51. Delete.detelebook3();
  52. break;
  53. }
  54. }
  55. }

io流

  1. package functions;
  2. import EntityClass.Book;
  3. import EntityClass.Borrow;
  4. import EntityClass.Reader;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Scanner;
  10. import static java.lang.Integer.parseInt;
  11. public class io {
  12. public static ArrayList inbook() throws IOException {
  13. FileReader fr = new FileReader("myFile\\book.txt");
  14. BufferedReader br = new BufferedReader(fr);
  15. ArrayList<Book> array = new ArrayList<Book>();
  16. String line;
  17. while ((line = br.readLine()) != null) {
  18. String[] strArray = line.split(",");
  19. Book book = new Book();
  20. book.setNum(parseInt(strArray[0]));
  21. book.setName(strArray[1]);
  22. book.setAuthor(strArray[2]);
  23. book.setPrice(strArray[3]);
  24. book.setQuantity(parseInt(strArray[4]));
  25. book.setDate(strArray[5]);
  26. book.setPress(strArray[6]);
  27. array.add(book);
  28. }
  29. br.close();
  30. fr.close();
  31. return array;
  32. }
  33. public static ArrayList inreader() throws IOException {
  34. FileReader fr = new FileReader("myFile\\reader.txt");
  35. BufferedReader br = new BufferedReader(fr);
  36. ArrayList<Reader> array = new ArrayList<Reader>();
  37. String line;
  38. while ((line = br.readLine()) != null) {
  39. String[] strArray = line.split(",");
  40. Reader reader = new Reader();
  41. reader.setNum(Integer.parseInt(strArray[0]));
  42. reader.setName(strArray[1]);
  43. reader.setCollege(strArray[2]);
  44. reader.setClass(strArray[3]);
  45. array.add(reader);
  46. }
  47. br.close();
  48. fr.close();
  49. return array;
  50. }
  51. public static ArrayList inborrow() throws IOException {
  52. FileReader fr = new FileReader("myFile\\borrow.txt");
  53. BufferedReader br = new BufferedReader(fr);
  54. ArrayList<Borrow> array = new ArrayList<Borrow>();
  55. String line;
  56. while ((line = br.readLine()) != null) {
  57. String[] strArray = line.split(",");
  58. Borrow borrow = new Borrow();
  59. borrow.setNum1(parseInt(strArray[0]));
  60. borrow.setName(strArray[1]);
  61. borrow.setNum2(parseInt(strArray[2]));
  62. borrow.setBookname(strArray[3]);
  63. borrow.setBorrowdate(strArray[4]);
  64. borrow.setShouldreturndate(strArray[5]);
  65. borrow.setReturndate(strArray[6]);
  66. array.add(borrow);
  67. }
  68. br.close();
  69. fr.close();
  70. return array;
  71. }
  72. }

操作

1.添加

  1. package functions;
  2. import EntityClass.Book;
  3. import EntityClass.Reader;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9. public class Add {
  10. //图书信息添加
  11. public static void AddBook(ArrayList<Book>arrayList) throws IOException,ClassNotFoundException{
  12. ArrayList<Book> array1=new ArrayList<Book>();
  13. File file =new File("myFile");
  14. file.mkdirs();
  15. FileWriter fw=new FileWriter("myFile\\book.txt",true);
  16. Scanner sc=new Scanner(System.in);
  17. System.out.println("请输入书名:");
  18. String name=sc.nextLine();
  19. fw.write(name);
  20. System.out.println("请输入书号:");
  21. String n = sc.nextLine();
  22. int num=Integer.parseInt(n);
  23. fw.write(num);
  24. System.out.println("请输入作者:");
  25. String author=sc.nextLine();
  26. fw.write(author);
  27. System.out.println("请输入出版社:");
  28. String press=sc.nextLine();
  29. fw.write(press);
  30. System.out.println("请输入出版时间:");
  31. String date=sc.nextLine();
  32. fw.write(date);
  33. System.out.println("请输入数量:");
  34. int quantity=sc.nextInt();
  35. fw.write(date);
  36. System.out.println("请输入价格:");
  37. sc.nextLine();
  38. String price=sc.nextLine();
  39. fw.write(price);
  40. Book s=new Book();
  41. s.setName(name);
  42. s.setAuthor(author);
  43. s.setDate(date);
  44. s.setNum(num);
  45. s.setPress(press);
  46. s.setPrice(price);
  47. s.setQuantity(quantity);
  48. array1.add(s);
  49. for(int i=0;i<array1.size();i++) {
  50. Book a=new Book();
  51. a=array1.get(i);
  52. fw.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
  53. }
  54. System.out.println("添加图书成功!");
  55. fw.close();
  56. }
  57. //读者信息添加
  58. public static void AddReader(ArrayList<Reader>arrayList) throws IOException,ClassNotFoundException{
  59. ArrayList<Reader> array1=new ArrayList<Reader>();
  60. File file =new File("myFile");
  61. file.mkdirs();
  62. FileWriter fw=new FileWriter("myFile\\reader.txt",true);
  63. Scanner sc=new Scanner(System.in);
  64. System.out.println("请输入姓名:");
  65. String name=sc.nextLine();
  66. System.out.println("请输入学号:");
  67. String n = sc.nextLine();
  68. int num=Integer.parseInt(n);
  69. System.out.println("请输入学院:");
  70. String college=sc.nextLine();
  71. System.out.println("请输入专业班级:");
  72. String Classs=sc.nextLine();
  73. Reader s=new Reader();
  74. s.setName(name);
  75. s.setNum(num);
  76. s.setCollege(college);
  77. s.setClass(Classs);
  78. array1.add(s);
  79. for(int i=0;i<array1.size();i++) {
  80. Reader a=new Reader();
  81. a=array1.get(i);
  82. fw.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
  83. }
  84. System.out.println("添加读者信息成功!");
  85. fw.close();
  86. }
  87. }

2.修改

  1. package functions;
  2. import EntityClass.Book;
  3. import EntityClass.Reader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9. public class Change {
  10. public static void sentakuchange1() throws IOException{
  11. System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用书名\n2、使用书号");
  12. int s;
  13. Scanner sc = new Scanner(System.in);
  14. while (true)
  15. {
  16. s = sc.nextInt();
  17. if(s!=1&&s!=2)
  18. System.out.println("输入有误,请重新输入");
  19. else break;
  20. }
  21. switch (s){
  22. case 1:
  23. change1();
  24. break;
  25. case 2:
  26. change2();
  27. break;
  28. }
  29. }
  30. public static void change1() throws IOException{
  31. //按照书籍名修改书籍
  32. System.out.println("请输入要修改的书名:");
  33. ArrayList<Book> array1=io.inbook();
  34. ArrayList<Book> array2 = null;
  35. boolean bool=false;
  36. int n=0;
  37. Scanner sc=new Scanner(System.in);
  38. String line=sc.nextLine();
  39. for(int i=0;i<array1.size();i++)
  40. {
  41. Book a=new Book();
  42. a=array1.get(i);
  43. if(a.getName().equals(line)){
  44. System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
  45. n=i;
  46. bool=true;
  47. }
  48. }
  49. if(bool==false){
  50. System.out.println("您要查找的书籍不存在!");
  51. return;
  52. }
  53. System.out.println("请输入修改后的书名:");
  54. String name=sc.nextLine();
  55. System.out.println("请输入修改后的书号(仅能输入数字):");
  56. String h = sc.nextLine();
  57. int num=Integer.parseInt(h);
  58. System.out.println("请输入修改后的作者:");
  59. String author=sc.nextLine();
  60. System.out.println("请输入修改后的出版社:");
  61. String press=sc.nextLine();
  62. System.out.println("请输入修改后的出版时间:");
  63. String date=sc.nextLine();
  64. System.out.println("请输入修改后的数量:");
  65. int quantity=sc.nextInt();
  66. System.out.println("请输入修改后的价格:");
  67. String price=sc.nextLine();
  68. Book s=new Book();
  69. s.setName(name);
  70. s.setAuthor(author);
  71. s.setDate(date);
  72. s.setNum(num);
  73. s.setPress(press);
  74. s.setPrice(price);
  75. s.setQuantity(quantity);
  76. array1.set(n,s);
  77. FileWriter fr=new FileWriter("myFile\\book.txt");
  78. BufferedWriter br=new BufferedWriter(fr);
  79. for(int i=0;i<array1.size();i++) {
  80. Book a=new Book();
  81. a=array1.get(i);
  82. br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
  83. }
  84. System.out.println("修改图书信息成功!");
  85. System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
  86. br.close();
  87. fr.close();
  88. }
  89. public static void change2() throws IOException{
  90. //按照书号修改书籍
  91. System.out.println("请输入要修改的书号(仅能输入数字):");
  92. ArrayList<Book> array1=io.inbook();
  93. ArrayList<Book> array2 = null;
  94. boolean bool=false;
  95. int n=0;
  96. Scanner sc=new Scanner(System.in);
  97. int line=sc.nextInt();
  98. for(int i=0;i<array1.size();i++)
  99. {
  100. Book a=new Book();
  101. a=array1.get(i);
  102. if(a.getNum()==line){
  103. System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
  104. array2.add(a);
  105. n=i;
  106. bool=true;
  107. }
  108. }
  109. if(bool==false){
  110. System.out.println("您要查找的书籍不存在!");
  111. return;
  112. }
  113. System.out.println("请输入修改后的书名:");
  114. String name=sc.nextLine();
  115. System.out.println("请输入修改后的书号(仅能输入数字):");
  116. int num=sc.nextInt();
  117. System.out.println("请输入修改后的作者:");
  118. String author=sc.nextLine();
  119. System.out.println("请输入修改后的出版社:");
  120. String press=sc.nextLine();
  121. System.out.println("请输入修改后的出版时间:");
  122. int quantity=sc.nextInt();
  123. System.out.println("请输入修改后的数量:");
  124. String date=sc.nextLine();
  125. System.out.println("请输入修改后的价格:");
  126. String price=sc.nextLine();
  127. Book s=new Book();
  128. s.setName(name);
  129. s.setAuthor(author);
  130. s.setDate(date);
  131. s.setNum(num);
  132. s.setPress(press);
  133. s.setPrice(price);
  134. s.setQuantity(quantity);
  135. array1.set(n,s);
  136. FileWriter fr=new FileWriter("myFile\\book.txt");
  137. BufferedWriter br=new BufferedWriter(fr);
  138. for(int i=0;i<array1.size();i++) {
  139. Book a=new Book();
  140. a=array1.get(i);
  141. br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
  142. }
  143. System.out.println("修改图书信息成功!");
  144. System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
  145. br.close();
  146. fr.close();
  147. }
  148. public static void change3() throws IOException{
  149. //按照学号+姓名修改信息
  150. ArrayList<Reader> array1=io.inreader();
  151. ArrayList<Reader> array2 = null;
  152. boolean bool=false;
  153. int n=0;
  154. Scanner sc=new Scanner(System.in);
  155. System.out.println("请输入要修改的学号(仅能输入数字):");
  156. int nu=sc.nextInt();
  157. System.out.println("请输入要修改的姓名:");
  158. String l=sc.nextLine();
  159. String line=sc.nextLine();
  160. for(int i=0;i<array1.size();i++)
  161. {
  162. Reader a=new Reader();
  163. a=array1.get(i);
  164. if(a.getName().equals(line)&&a.getNum()==nu){
  165. System.out.println("您的信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
  166. n=i;
  167. bool=true;
  168. }
  169. }
  170. if(bool==false){
  171. System.out.println("您要查找的学生信息不存在!");
  172. return;
  173. }
  174. System.out.println("请输入修改后的姓名:");
  175. String name=sc.nextLine();
  176. System.out.println("请输入修改后的学号(仅能输入数字):");
  177. int h = Integer.parseInt(sc.nextLine());
  178. System.out.println("请输入修改后的学院:");
  179. String College=sc.nextLine();
  180. System.out.println("请输入修改后的专业班级:");
  181. String Classs=sc.nextLine();
  182. Reader s=new Reader();
  183. s.setName(name);
  184. s.setNum(h);
  185. s.setCollege(College);
  186. s.setClass(Classs);
  187. array1.set(n,s);
  188. FileWriter fr=new FileWriter("myFile\\reader.txt");
  189. BufferedWriter br=new BufferedWriter(fr);
  190. for(int i=0;i<array1.size();i++) {
  191. Reader a=new Reader();
  192. a=array1.get(i);
  193. br.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
  194. }
  195. System.out.println("修改读者信息成功!");
  196. System.out.print("修改后的读者信息:\n"+s.getNum() + "," + s.getName() + "," + s.getCollege() + "," + s.getClasss() );
  197. br.close();
  198. fr.close();
  199. }
  200. }

3.查找

  1. package functions;
  2. import EntityClass.Book;
  3. import EntityClass.Borrow;
  4. import EntityClass.Reader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8. public class Find {
  9. public static void findbook(ArrayList<Book> arrayList) throws IOException {
  10. System.out.println("1、按书名查询");
  11. System.out.println("2、按作者名查询");
  12. System.out.println("3、按出版社查询");
  13. Scanner sc = new Scanner(System.in);
  14. int s = sc.nextInt();
  15. switch (s) {
  16. case 1:
  17. findbook1();
  18. break;
  19. case 2:
  20. findbook2();
  21. break;
  22. case 3:
  23. findbook3();
  24. break;
  25. }
  26. }
  27. public static void findbook1() throws IOException{
  28. System.out.println("请输入书名:");
  29. Scanner sc=new Scanner(System.in);
  30. String line=sc.nextLine();
  31. ArrayList<Book> array1=io.inbook();
  32. boolean bool=false;
  33. for(int i=0;i<array1.size();i++)
  34. {
  35. Book a=new Book();
  36. a=array1.get(i);
  37. if(a.getName().equals(line)){
  38. System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
  39. bool=true;
  40. }
  41. }
  42. if(bool==false){
  43. System.out.println("您要查找的书籍不存在!");
  44. }
  45. }
  46. public static void findbook2() throws IOException{
  47. System.out.println("请输入作者名:");
  48. Scanner sc=new Scanner(System.in);
  49. String line=sc.nextLine();
  50. ArrayList<Book> array1=io.inbook();
  51. boolean bool=false;
  52. for(int i=0;i<array1.size();i++)
  53. {
  54. Book a=new Book();
  55. a=array1.get(i);
  56. if(a.getAuthor().equals(line)){
  57. System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
  58. bool=true;
  59. }
  60. }
  61. if(bool==false){
  62. System.out.println("您要查找的书籍不存在!");
  63. }
  64. }
  65. public static void findbook3() throws IOException{
  66. System.out.println("请输入出版社:");
  67. Scanner sc=new Scanner(System.in);
  68. String line=sc.nextLine();
  69. ArrayList<Book> array1=io.inbook();
  70. boolean bool=false;
  71. for(int i=0;i<array1.size();i++)
  72. {
  73. Book a=new Book();
  74. a=array1.get(i);
  75. if(a.getPress().equals(line)){
  76. System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
  77. bool=true;
  78. }
  79. }
  80. if(bool==false){
  81. System.out.println("您要查找的书籍不存在!");
  82. }
  83. }
  84. public static void findreader(ArrayList<Reader> arrayList) throws IOException {
  85. System.out.println("1、按学号查询");
  86. System.out.println("2、按姓名查询");
  87. System.out.println("3、按专业班级查询");
  88. Scanner sc = new Scanner(System.in);
  89. int s = sc.nextInt();
  90. switch (s) {
  91. case 1:
  92. findreader1();
  93. break;
  94. case 2:
  95. findreader2();
  96. break;
  97. case 3:
  98. findreader3();
  99. break;
  100. }
  101. }
  102. public static void findreader1() throws IOException{
  103. System.out.println("请输入学号:");
  104. Scanner sc=new Scanner(System.in);
  105. String lin=sc.nextLine();
  106. int line=Integer.parseInt(lin);
  107. ArrayList<Reader> array1=io.inreader();
  108. boolean bool=false;
  109. for(int i=0;i<array1.size();i++)
  110. {
  111. Reader a=new Reader();
  112. a=array1.get(i);
  113. if(a.getNum()==line){
  114. System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
  115. bool=true;
  116. }
  117. }
  118. if(bool==false){
  119. System.out.println("您要查找的学生信息不存在!");
  120. }
  121. }
  122. public static void findreader2() throws IOException{
  123. System.out.println("请输入姓名:");
  124. Scanner sc=new Scanner(System.in);
  125. String line=sc.nextLine();
  126. ArrayList<Reader> array1=io.inreader();
  127. boolean bool=false;
  128. for(int i=0;i<array1.size();i++)
  129. {
  130. Reader a=new Reader();
  131. a=array1.get(i);
  132. if(a.getName().equals(line)){
  133. System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
  134. bool=true;
  135. }
  136. }
  137. if(bool==false){
  138. System.out.println("您要查找的学生信息不存在!");
  139. }
  140. }
  141. public static void findreader3() throws IOException{
  142. System.out.println("请输入专业班级:");
  143. Scanner sc=new Scanner(System.in);
  144. String line=sc.nextLine();
  145. ArrayList<Reader> array1=io.inreader();
  146. boolean bool=false;
  147. for(int i=0;i<array1.size();i++)
  148. {
  149. Reader a=new Reader();
  150. a=array1.get(i);
  151. if(a.getClasss().equals(line)){
  152. System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
  153. bool=true;
  154. }
  155. }
  156. if(bool==false){
  157. System.out.println("您要查找的学生信息不存在!");
  158. }
  159. }
  160. public static void findborrow(ArrayList<Borrow> arrayList) throws IOException {
  161. System.out.println("1、按学号查询");
  162. System.out.println("2、按书名查询");
  163. System.out.println("3、按书号查询");
  164. Scanner sc = new Scanner(System.in);
  165. int s = sc.nextInt();
  166. switch (s) {
  167. case 1:
  168. findborrow1();
  169. break;
  170. case 2:
  171. findborrow2();
  172. break;
  173. case 3:
  174. findborrow2();
  175. break;
  176. }
  177. }
  178. public static void findborrow1() throws IOException{
  179. System.out.println("请输入学号:");
  180. Scanner sc=new Scanner(System.in);
  181. String line=sc.nextLine();
  182. ArrayList<Borrow> array1=io.inborrow();
  183. boolean bool=false;
  184. for(int i=0;i<array1.size();i++)
  185. {
  186. Borrow a=new Borrow();
  187. a=array1.get(i);
  188. if(a.getNum1()==Integer.parseInt(line)){
  189. System.out.println("您要找的书籍是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
  190. bool=true;
  191. }
  192. }
  193. if(bool==false){
  194. System.out.println("您要查找的借阅信息不存在!");
  195. }
  196. }
  197. public static void findborrow2() throws IOException{
  198. System.out.println("请输入书名:");
  199. Scanner sc=new Scanner(System.in);
  200. String line=sc.nextLine();
  201. ArrayList<Borrow> array1=io.inborrow();
  202. boolean bool=false;
  203. for(int i=0;i<array1.size();i++)
  204. {
  205. Borrow a=new Borrow();
  206. a=array1.get(i);
  207. if(a.getBookname().equals(line)){
  208. System.out.println("您要查询的借阅信息是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
  209. bool=true;
  210. }
  211. }
  212. if(bool==false){
  213. System.out.println("您要查找的借阅信息不存在!");
  214. }
  215. }
  216. public static void findborrow3() throws IOException{
  217. System.out.println("请输入书号:");
  218. Scanner sc=new Scanner(System.in);
  219. String line=sc.nextLine();
  220. ArrayList<Book> array1=io.inborrow();
  221. boolean bool=false;
  222. for(int i=0;i<array1.size();i++)
  223. {
  224. Borrow a=new Borrow();
  225. a=array1.get(i);
  226. if(a.getNum2()==Integer.parseInt(line)){
  227. System.out.println("您要查询的借阅信息是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
  228. bool=true;
  229. }
  230. }
  231. if(bool==false){
  232. System.out.println("您要查找的借阅信息不存在!");
  233. }
  234. }
  235. }

4.排序

  1. package functions;
  2. import EntityClass.Book;
  3. import EntityClass.Reader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.Scanner;
  8. public class sort {
  9. public static void sentakusort() throws IOException {
  10. System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用书号\n2、使用书名");
  11. int s;
  12. Scanner sc = new Scanner(System.in);
  13. while (true)
  14. {
  15. s = sc.nextInt();
  16. if(s!=1&&s!=2)
  17. System.out.println("输入有误,请重新输入");
  18. else break;
  19. }
  20. switch (s){
  21. case 1:
  22. sortingBook1(io.inbook());
  23. break;
  24. case 2:
  25. sortingBook2(io.inbook());
  26. break;
  27. }
  28. }
  29. //使用书号排列输出
  30. public static void sortingBook1(ArrayList<Book> array) {
  31. int[] arr = new int[100];
  32. for (int i = 0; i < array.size(); i++) {
  33. Book b = array.get(i);
  34. arr[i] = b.getNum();
  35. }
  36. for (int i1 = 0; i1 < array.size() - 1; i1++) {
  37. for (int j = 0; j < array.size() - 1 - i1; j++) {
  38. Book book1 = array.get(j);
  39. Book book2 = array.get(j+1);
  40. if (book1.getNum()>book2.getNum()) {
  41. int temp=arr[j];
  42. arr[j]=arr[j+1];
  43. arr[j+1]=temp;
  44. Collections.swap(array, j, j + 1);
  45. }
  46. }
  47. }
  48. System.out.println("已完成排序+\n排序后的列表:\n");
  49. for (int i = 0; i < array.size(); i++) {
  50. Book a = array.get(i);
  51. System.out.println(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress());
  52. }
  53. }
  54. //使用书名进行排序
  55. public static void sortingBook2(ArrayList<Book> array) {
  56. int[] arr = new int[100];
  57. for (int i = 0; i < array.size(); i++) {
  58. Book b = array.get(i);
  59. arr[i] = b.getNum();
  60. }
  61. for (int i1 = 0; i1 < array.size() - 1; i1++) {
  62. for (int j = 0; j < array.size() - 1 - i1; j++) {
  63. Book book1 = array.get(j);
  64. Book book2 = array.get(j+1);
  65. int bool= book1.getName().compareTo(book2.getName());
  66. if (bool>0) {
  67. int temp=arr[j];
  68. arr[j]=arr[j+1];
  69. arr[j+1]=temp;
  70. Collections.swap(array, j, j + 1);
  71. }
  72. }
  73. }
  74. System.out.println("已完成排序\n排序后的列表:\n");
  75. for (int i = 0; i < array.size(); i++) {
  76. Book a = array.get(i);
  77. System.out.println(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress());
  78. }
  79. }
  80. public static void sentakusortreader() throws IOException {
  81. System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用学号\n2、使用学院");
  82. int s;
  83. Scanner sc = new Scanner(System.in);
  84. while (true)
  85. {
  86. s = sc.nextInt();
  87. if(s!=1&&s!=2)
  88. System.out.println("输入有误,请重新输入");
  89. else break;
  90. }
  91. switch (s){
  92. case 1:
  93. sortingreader1(io.inreader());
  94. break;
  95. case 2:
  96. sortingreader2(io.inreader());
  97. break;
  98. }
  99. }
  100. public static void sortingreader1(ArrayList<Reader> array) { //使用学号排列输出
  101. int[] arr = new int[100];
  102. for (int i = 0; i < array.size(); i++) {
  103. Book b = array.get(i);
  104. arr[i] = b.getNum();
  105. }
  106. for (int i1 = 0; i1 < array.size() - 1; i1++) {
  107. for (int j = 0; j < array.size() - 1 - i1; j++) {
  108. Reader book1 = array.get(j);
  109. Reader book2 = array.get(j+1);
  110. if (book1.getNum()>book2.getNum()) {
  111. int temp=arr[j];
  112. arr[j]=arr[j+1];
  113. arr[j+1]=temp;
  114. Collections.swap(array, j, j + 1);
  115. }
  116. }
  117. }
  118. System.out.println("已完成排序\n排序后的列表:\n");
  119. for (int i = 0; i < array.size(); i++) {
  120. Reader a = array.get(i);
  121. System.out.println(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getCollege());
  122. }
  123. }
  124. public static void sortingreader2(ArrayList<Reader> array) {//使用学院进行排序
  125. int[] arr = new int[100];
  126. for (int i = 0; i < array.size(); i++) {
  127. Book b = array.get(i);
  128. arr[i] = b.getNum();
  129. }
  130. for (int i1 = 0; i1 < array.size() - 1; i1++) {
  131. for (int j = 0; j < array.size() - 1 - i1; j++) {
  132. Reader book1 = array.get(j);
  133. Reader book2 = array.get(j+1);
  134. int bool= book1.getName().compareTo(book2.getName());
  135. if (bool>0) {
  136. int temp=arr[j];
  137. arr[j]=arr[j+1];
  138. arr[j+1]=temp;
  139. Collections.swap(array, j, j + 1);
  140. }
  141. }
  142. }
  143. System.out.println("已完成排序\n排序后的列表:\n");
  144. for (int i = 0; i < array.size(); i++) {
  145. Reader a = array.get(i);
  146. System.out.println(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getCollege());
  147. }
  148. }
  149. }

5.删除

  1. package functions;
  2. import EntityClass.Book;
  3. import EntityClass.Reader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9. public class Delete {
  10. public static void sentakudelete1() throws IOException{
  11. System.out.println("您要选择哪种方法进行删除?:"+"\n"+"1、使用书名\n2、使用书号");
  12. int s;
  13. Scanner sc = new Scanner(System.in);
  14. while (true)
  15. {
  16. s = sc.nextInt();
  17. if(s!=1&&s!=2)
  18. System.out.println("输入有误,请重新输入");
  19. else break;
  20. }
  21. switch (s){
  22. case 1:
  23. detelebook1();
  24. break;
  25. case 2:
  26. detelebook2();
  27. break;
  28. }
  29. }
  30. public static void detelebook1() throws IOException{
  31. //用书名删除书籍
  32. System.out.println("请输入书名:");
  33. Scanner sc=new Scanner(System.in);
  34. String line=sc.nextLine();
  35. ArrayList<Book> array1=io.inbook();
  36. boolean bool=false;
  37. int n=0;
  38. for(int i=0;i<array1.size();i++)
  39. {
  40. Book a=new Book();
  41. a=array1.get(i);
  42. if(a.getName().equals(line)){
  43. System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
  44. n=i;
  45. bool=true;
  46. }
  47. }
  48. if(bool==false){
  49. System.out.println("您要查找的书籍不存在!");
  50. return;
  51. }
  52. array1.remove(n);
  53. System.out.println("删除成功!");
  54. FileWriter fr=new FileWriter("myFile\\book.txt");
  55. BufferedWriter br=new BufferedWriter(fr);
  56. for(int i=0;i<array1.size();i++) {
  57. Book a=new Book();
  58. a=array1.get(i);
  59. br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
  60. }
  61. System.out.println("修改图书信息成功!");
  62. br.close();
  63. fr.close();
  64. }
  65. public static void detelebook2() throws IOException{//用书号删除书籍
  66. System.out.println("请输入书号:");
  67. Scanner sc=new Scanner(System.in);
  68. int line=sc.nextInt();
  69. ArrayList<Book> array1=io.inbook();
  70. boolean bool=false;
  71. int n=0;
  72. for(int i=0;i<array1.size();i++)
  73. {
  74. Book a=new Book();
  75. a=array1.get(i);
  76. if(a.getNum()==line){
  77. System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
  78. n=i;
  79. bool=true;
  80. }
  81. }
  82. if(bool==false){
  83. System.out.println("您要查找的书籍不存在!");
  84. return;
  85. }
  86. ArrayList<Book> array2=new ArrayList<>();
  87. for (int i = 0; i < array1.size() - 1; i++) {
  88. if (i < n) {
  89. array2.set(i, array1.get(i));
  90. } else {
  91. array2.set(i, array1.get(i + 1));
  92. }
  93. }
  94. array1.remove(n);
  95. System.out.println("删除成功!");
  96. FileWriter fr=new FileWriter("myFile\\book.txt");
  97. BufferedWriter br=new BufferedWriter(fr);
  98. for(int i=0;i<array1.size();i++) {
  99. Book a=new Book();
  100. a=array1.get(i);
  101. br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
  102. }
  103. System.out.println("修改图书信息成功!");
  104. br.close();
  105. fr.close();
  106. }
  107. public static void detelebook3() throws IOException{//按照学号+姓名删除信息
  108. ArrayList<Reader> array1=io.inreader();
  109. ArrayList<Reader> array2 = null;
  110. boolean bool=false;
  111. int n=0;
  112. Scanner sc=new Scanner(System.in);
  113. System.out.println("请输入学号:");
  114. int nu=sc.nextInt();
  115. System.out.println("请输入姓名:");
  116. String l=sc.nextLine();
  117. String line=sc.nextLine();
  118. for(int i=0;i<array1.size();i++)
  119. {
  120. Reader a=new Reader();
  121. a=array1.get(i);
  122. if(a.getName().equals(line)&&a.getNum()==nu){
  123. System.out.println("您的信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
  124. n=i;
  125. bool=true;
  126. }
  127. }
  128. if(bool==false){
  129. System.out.println("您要查找的学生信息不存在!");
  130. return;
  131. }
  132. array1.remove(n);
  133. System.out.println("删除成功!");
  134. FileWriter fr=new FileWriter("myFile\\reader.txt");
  135. BufferedWriter br=new BufferedWriter(fr);
  136. for(int i=0;i<array1.size();i++) {
  137. Reader a=new Reader();
  138. a=array1.get(i);
  139. br.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
  140. }
  141. System.out.println("修改读者信息成功!");
  142. br.close();
  143. fr.close();
  144. }
  145. }

基本类

1.Book

  1. package EntityClass;
  2. public class Book extends Borrow {
  3. private String name;//书名
  4. private String author;//作者
  5. private String price;//价格
  6. private int num;//书号
  7. private String press;//出版社
  8. private String date;//出版日期
  9. private int quantity;//存馆数量
  10. public Book() {
  11. }
  12. public Book(String name, String author, String price, int num, String press, String date, int quantity) {
  13. this.name = name;
  14. this.author = author;
  15. this.price = price;
  16. this.num = num;
  17. this.press = press;
  18. this.date = date;
  19. this.quantity = quantity;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public String getAuthor() {
  25. return author;
  26. }
  27. public String getPrice() {
  28. return price;
  29. }
  30. public int getNum() {
  31. return num;
  32. }
  33. public String getPress() {
  34. return press;
  35. }
  36. public String getDate() {
  37. return date;
  38. }
  39. public int getQuantity() {
  40. return quantity;
  41. }
  42. public void setName(String name) {
  43. this.name = name;
  44. }
  45. public void setAuthor(String author) {
  46. this.author = author;
  47. }
  48. public void setPrice(String price) {
  49. this.price = price;
  50. }
  51. public void setNum(int num) {
  52. this.num = num;
  53. }
  54. public void setPress(String press) {
  55. this.press = press;
  56. }
  57. public void setDate(String date) {
  58. this.date = date;
  59. }
  60. public void setQuantity(int quantity) {
  61. this.quantity = quantity;
  62. }
  63. }

2.Borrow

  1. package EntityClass;
  2. public class Borrow {
  3. private String name;//姓名
  4. private int num1; //学号
  5. private int num2;//书号
  6. private String bookname; //书名
  7. private String borrowdate;//借阅日期
  8. private String Shouldreturndate;//应还日期
  9. private String returndate;//实际归还日期
  10. public Borrow() {
  11. }
  12. public Borrow(String name, int num1, int num2, String bookname, String borrowdate, String shouldreturndate, String returndate) {
  13. this.name = name;
  14. this.num1 = num1;
  15. this.num2 = num2;
  16. this.bookname = bookname;
  17. this.borrowdate = borrowdate;
  18. Shouldreturndate = shouldreturndate;
  19. this.returndate = returndate;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public int getNum1() {
  25. return num1;
  26. }
  27. public int getNum2() {
  28. return num2;
  29. }
  30. public String getBookname() {
  31. return bookname;
  32. }
  33. public String getBorrowdate() {
  34. return borrowdate;
  35. }
  36. public String getShouldreturndate() {
  37. return Shouldreturndate;
  38. }
  39. public String getReturndate() {
  40. return returndate;
  41. }
  42. public void setName(String name) {
  43. this.name = name;
  44. }
  45. public void setNum1(int num1) {
  46. this.num1 = num1;
  47. }
  48. public void setNum2(int num2) {
  49. this.num2 = num2;
  50. }
  51. public void setBookname(String bookname) {
  52. this.bookname = bookname;
  53. }
  54. public void setBorrowdate(String borrowdate) {
  55. this.borrowdate = borrowdate;
  56. }
  57. public void setShouldreturndate(String shouldreturndate) {
  58. Shouldreturndate = shouldreturndate;
  59. }
  60. public void setReturndate(String returndate) {
  61. this.returndate = returndate;
  62. }
  63. }

3.Reader

  1. package EntityClass;
  2. public class Reader extends Book {
  3. private String name;//姓名
  4. private int num;//学号
  5. private String college;//学院专业
  6. private String Classs;//班级
  7. public Reader(String name, int num, String college, String aClass) {
  8. this.name = name;
  9. this.num = num;
  10. this.college = college;
  11. Classs = aClass;
  12. }
  13. public Reader() {
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public int getNum() {
  19. return num;
  20. }
  21. public String getCollege() {
  22. return college;
  23. }
  24. public String getClasss() {
  25. return Classs;
  26. }
  27. public void setName(String name) {
  28. this.name = name;
  29. }
  30. public void setNum(int num) {
  31. this.num = num;
  32. }
  33. public void setCollege(String college) {
  34. this.college = college;
  35. }
  36. public void setClass(String aClass) {
  37. Classs = aClass;
  38. }
  39. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/556108?site
推荐阅读
相关标签
  

闽ICP备14008679号