加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

一个简陋的java图书管理系统

发布时间:2020-12-14 20:07:43 所属栏目:Java 来源:网络整理
导读:本文代码为原创一个简陋的管理系统,只做功能的测试。并没有去完善所有应有的功能,只做了输入输出查找,仅供参考! 菜单部分: import java.util.Scanner;public class Menu { int Min = 1; int Max = 3; public void getMenu(){ System.out.println("1、显

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

菜单部分: 

import java.util.Scanner;
public class Menu {
 int Min = 1;
 int Max = 3;
 public void getMenu(){
 System.out.println("1、显示/2、输入/3、查找");
 }
 public void getFindMenu(){
 System.out.println("1、编号/2、书名/3、作者");
 }
 public int setMenu(){
 System.out.println("输入序号:");
 Scanner reader = new Scanner(System.in);
 int num = reader.nextInt();
 if(num >= Min || num <= Max)
  return num;
 else
  return -1;
 }
}

重点的管理部分: 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;
import java.io.IOException;

public class Book {
 public void find(){
 Menu menu = new Menu();
 menu.getFindMenu();
 Scanner reader = new Scanner(System.in);
 int num = menu.setMenu();
 switch(num){
 case 1:
  System.out.println("请输入编号");
  Find(reader.next(),0);
  break;
 case 2:
  System.out.println("请输入书名");
  Find(reader.next(),1);
  break;
 case 3:
  System.out.println("请输入作者");
  Find(reader.next(),2);
  break;
 }
 }
 public void Find(String s,int n){
 try {
  Scanner in = new Scanner(new File("res/Book.txt"));
  while (in.hasNextLine()) {
  String str = in.nextLine();
  String[] book = str.trim().split("#");
  if(book[n].compareTo(s) == 0)
   System.out.println(book[0] +" "+ book[1] +" "+ book[2]);
  }
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
 }
 public String findNum(String s,int n){
 try {
  Scanner in = new Scanner(new File("res/Book.txt"));
  while (in.hasNextLine()) {
  String str = in.nextLine();
  String[] book = str.trim().split("#");
  if(book[n].compareTo(s) == 0)
   return book[n];
  }
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
 return "没有找到";
 }
 public String message(){
 Scanner reader = new Scanner(System.in);
 String str = "";
 String s = "";
 System.out.println("请输入编号");
 str = reader.next();
 if(findNum(str,0).compareTo("没有找到") != 0){
  System.out.println("此编号存在输入错误");
  return "@@!!";
 }
 s += str + "#";
 System.out.println("请输入书名");
 str = reader.next();
 s += str + "#";
 System.out.println("请输入作者");
 str = reader.next();
 s += str + "#n";
 return s;
 }
 public void setBook() {
 FileOutputStream fop = null;
 File file;
 String content = message();
 if(content.compareTo("@@!!") == 0)
  return ; 
 try {
  file = new File("res/Book.txt");
  fop = new FileOutputStream(file,true);
  byte[] contentInBytes = content.getBytes();
  fop.write(contentInBytes);
  fop.flush();
  fop.close();
  System.out.println("Done");
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  try {
  if (fop != null) {
   fop.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 }

 public void getBook() {
 try {
  Scanner in = new Scanner(new File("res/Book.txt"));
  while (in.hasNextLine()) {
  String str = in.nextLine();
  splitt(str);
  }
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
 }

 public static String[] splitt(String str) {
 String[] book = str.trim().split("#");
 for (int i = 0; i < book.length; i++) {
  System.out.println(book[i]);
 }
 System.out.println("n*********************");
 return book;
 }
}

主函数部分:

public class ManageBook {
 public static void main(String[] agse){
 Menu menu = new Menu();
 Book book = new Book();
 while(true){
  menu.getMenu();
  int num = menu.setMenu();
  switch(num){
  case 1:
   book.getBook();
   break;
  case 2:
   book.setBook();
   break;
  case 3:
   book.find();
   break;
  case -1:
   System.out.println("输入有误");
   break;
  }
 }
 }

}

关于管理系统的更多内容请点击《管理系统专题》进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • 图书管理系统java版
  • 图书管理系统java代码实现

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读