本文代碼為原創一個簡陋的管理系統,只做功能的測試。並沒有去完善所有應有的功能,只做了輸入輸出查找。僅供參考!
菜單部分:
1 import java.util.Scanner;
2 public class Menu {
3 int Min = 1;
4 int Max = 3;
5 public void getMenu(){
6 System.out.println("1、顯示/2、輸入/3、查找");
7 }
8 public void getFindMenu(){
9 System.out.println("1、編號/2、書名/3、作者");
10 }
11 public int setMenu(){
12 System.out.println("輸入序號:");
13 Scanner reader = new Scanner(System.in);
14 int num = reader.nextInt();
15 if(num >= Min || num <= Max)
16 return num;
17 else
18 return -1;
19 }
20 }
重點的管理部分:
1 import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.io.FileOutputStream;
4 import java.util.Scanner;
5 import java.io.IOException;
6
7 public class Book {
8 public void find(){
9 Menu menu = new Menu();
10 menu.getFindMenu();
11 Scanner reader = new Scanner(System.in);
12 int num = menu.setMenu();
13 switch(num){
14 case 1:
15 System.out.println("請輸入編號");
16 Find(reader.next(), 0);
17 break;
18 case 2:
19 System.out.println("請輸入書名");
20 Find(reader.next(), 1);
21 break;
22 case 3:
23 System.out.println("請輸入作者");
24 Find(reader.next(), 2);
25 break;
26 }
27 }
28 public void Find(String s,int n){
29 try {
30 Scanner in = new Scanner(new File("res/Book.txt"));
31 while (in.hasNextLine()) {
32 String str = in.nextLine();
33 String[] book = str.trim().split("#");
34 if(book[n].compareTo(s) == 0)
35 System.out.println(book[0] +" "+ book[1] +" "+ book[2]);
36 }
37 } catch (FileNotFoundException e) {
38 e.printStackTrace();
39 }
40 }
41 public String findNum(String s,int n){
42 try {
43 Scanner in = new Scanner(new File("res/Book.txt"));
44 while (in.hasNextLine()) {
45 String str = in.nextLine();
46 String[] book = str.trim().split("#");
47 if(book[n].compareTo(s) == 0)
48 return book[n];
49 }
50 } catch (FileNotFoundException e) {
51 e.printStackTrace();
52 }
53 return "沒有找到";
54 }
55 public String message(){
56 Scanner reader = new Scanner(System.in);
57 String str = "";
58 String s = "";
59 System.out.println("請輸入編號");
60 str = reader.next();
61 if(findNum(str,0).compareTo("沒有找到") != 0){
62 System.out.println("此編號存在輸入錯誤");
63 return "@@!!";
64 }
65 s += str + "#";
66 System.out.println("請輸入書名");
67 str = reader.next();
68 s += str + "#";
69 System.out.println("請輸入作者");
70 str = reader.next();
71 s += str + "#\n";
72 return s;
73 }
74 public void setBook() {
75 FileOutputStream fop = null;
76 File file;
77 String content = message();
78 if(content.compareTo("@@!!") == 0)
79 return ;
80 try {
81 file = new File("res/Book.txt");
82 fop = new FileOutputStream(file,true);
83 byte[] contentInBytes = content.getBytes();
84 fop.write(contentInBytes);
85 fop.flush();
86 fop.close();
87 System.out.println("Done");
88 } catch (IOException e) {
89 e.printStackTrace();
90 } finally {
91 try {
92 if (fop != null) {
93 fop.close();
94 }
95 } catch (IOException e) {
96 e.printStackTrace();
97 }
98 }
99 }
100
101 public void getBook() {
102 try {
103 Scanner in = new Scanner(new File("res/Book.txt"));
104 while (in.hasNextLine()) {
105 String str = in.nextLine();
106 splitt(str);
107 }
108 } catch (FileNotFoundException e) {
109 e.printStackTrace();
110 }
111 }
112
113 public static String[] splitt(String str) {
114 String[] book = str.trim().split("#");
115 for (int i = 0; i < book.length; i++) {
116 System.out.println(book[i]);
117 }
118 System.out.println("\n*********************");
119 return book;
120 }
121 }
主函數部分:
1 public class ManageBook {
2
3 public static void main(String[] agse){
4 Menu menu = new Menu();
5 Book book = new Book();
6 while(true){
7 menu.getMenu();
8 int num = menu.setMenu();
9 switch(num){
10 case 1:
11 book.getBook();
12 break;
13 case 2:
14 book.setBook();
15 break;
16 case 3:
17 book.find();
18 break;
19 case -1:
20 System.out.println("輸入有誤");
21 break;
22 }
23 }
24 }
25
26 }