zoukankan      html  css  js  c++  java
  • Java-简陋的图书管理

    本文代码为原创一个简陋的管理系统,只做功能的测试。并没有去完善所有应有的功能,只做了输入输出查找。仅供参考!

    菜单部分:

     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 + "#
    ";
     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("
    *********************");
    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 }
  • 相关阅读:
    VisualSVN Server 和 Subversion (都是服务器端安装)
    pl/sql导出dmp格式数据时,命令行一闪而退的问题
    Linux各种时间类型与时间函数提供技术文档
    erlang tuple的一些操作
    erlang 题目:一个integer列表,按照数字出现的次数由少到多排序,相同的数字小 的在前面
    一些erlang 题目
    各种排序
    erlang gen_tcp 详细例子
    erlang receive语句大诠释
    ets结合record的增删改查操作
  • 原文地址:https://www.cnblogs.com/ABook/p/5537685.html
Copyright © 2011-2022 走看看