欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-13 12:18:39
本文代码为原创一个简陋的管理系统,只做功能的测试。并没有去完善所有应有的功能,只做了输入输出查找,仅供参考!  菜单部分:  import...

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

菜单部分: 

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;
  }
 }
 }

}

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

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