一个简陋的图书管理系统 重要管理部分源代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值