基于文件系统的图书管理系统的设计与实现

朋友让写的一个课程设计,花了一天的时间做了下。

功能:图书信息的输入、添加、修改、查询和排序

1、输入多本书

2、添加一本书

3、修改一本书的书名

4、修改一本书的借书日期

5、显示所有书本的信息

6、查询指定书名的书本信息
7、查询在某个日期段的书本信息

8、按日期顺序显示所有的书本信息

我把源码贴上,希望对也在做这个设计的同行有所帮助。

源码存放于是BookInput.java中

 

import java.io.*;
import java.util.*;//导入Scanner类
public class BookInput
{
 
 public BookInput() throws IOException
 {
  while(true)
  {
   System.out.println("***********欢迎访问图书系统***********/n");
   System.out.println("1、初始输入多本书");
   System.out.println("2、补充添加一本书");
   System.out.println("3、查询指定书名的书本信息");
   System.out.println("4、查询在某个日期段的书本信息");
   System.out.println("5、修改一本书的书名");
   System.out.println("6、修改一本书的借书日期");
   System.out.println("7、显示所有书本的信息");
   System.out.println("8、按日期顺序显示所有的书本信息");
   System.out.println("0、退出");
   System.out.println("***********请选择您要的操作***********/n");

   //由于System.in是字节流,所以需要一个适配器转换,即InputStreamReader;
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   int a = Integer.parseInt(br.readLine());  //接收键盘字符并转换成整型
   switch(a)    
   {
    case 1: addBooks();  break;
    case 2: addBook();  break;
    case 3: searchByName(); break;
    case 4: searchByDate(); break;
    case 5: modifyBookName(); break;
    case 6: modifyBookDate(); break;
    case 7: showAll();  break;
    case 8: showByDate(); break;
    case 0:  System.exit(0); break;
    default : System.out.println("输入有误请重新输入:");
   }
  }
 }
 
 public static void main(String args[]) throws IOException
 {
  BookInput bi=new BookInput();
 } 

 //功能函数如下:
 public void addBooks()   //初始输入多本书
 {
  try{
   FileWriter f=new FileWriter("books.txt");
   while(true)//用一个while循环输入多本书
   {
    System.out.println("请输入书名:");
    Scanner scanner=new Scanner(System.in); //新建Scanner实例,从键盘输入
    String bookname=scanner.next(); //接受用户输入的书名,并赋值给一个字符串变量
    if(bookname.equals("end")) break;//判断用户输入的书名是否为end,是则退出循环
    System.out.println("您输入的书名是:"+bookname); //显示用户输入的书名
    f.write(bookname+"  ");//将书名写入文件
    System.out.println("请输入借书日期:");//显示“请输入借书日期:”
    String borrowtime=scanner.next();//接受用户输入的时间,并赋值给一个字符串变量
    System.out.println("您输入的借书日期为:"+borrowtime);//显示用户输入的借书日期
    f.write(borrowtime+"  ");//将借书日期写入文件
   }
   f.close();
  }
  catch(IOException ew){
   System.out.println(ew);
  }
 }

 public void addBook()
 {
  try{
   FileWriter f=new FileWriter("books.txt",true);
   Scanner scanner=new Scanner(System.in);
   System.out.println("请输入书名:");
   String bookname=scanner.next();
   System.out.println("您输入的书名是:"+bookname);
   f.write(bookname+"  ");//将书名写入文件
   System.out.println("请输入借书日期:");//显示“请输入借书日期:”
   String borrowtime=scanner.next();//接受用户输入的时间,并赋值给一个字符串变量
   System.out.println("您输入的借书日期为:"+borrowtime);//显示用户输入的借书日期
   f.write(borrowtime+"  ");//将借书日期写入文件
   f.close();
  }
  catch(IOException ew){
   System.out.println(ew);
  }
 }
 
 public void searchByName() throws IOException
 {
  try{
   File f=new File("books.txt");
   BufferedInputStream   bin=new  BufferedInputStream(new FileInputStream(f));  
            byte[]  buff=new  byte[((int)f.length())]; 
            bin.read(buff);  
            String  str=new  String(buff,"utf-8");  
            String[] all=str.split(" ");   //以空格做为分隔符,文本里的内容现在存放在all[]数组中
   
   System.out.println("请输入要查找的书名:");
   Scanner sc=new Scanner(System.in);
   String bookname=sc.next();
   for(int i=0;i<all.length;i++)
   {
    if(all[i].equals(bookname))
     System.out.println("书名:"+all[i]+"     日期:"+all[i+2]);   //这里+2的意思是,书名和日期之前有一个空格符
   }
  }
  catch(FileNotFoundException e){
   System.out.println(e);
  }
 }

 public void searchByDate() throws IOException
 {
  try{
   File f=new File("books.txt");
   BufferedInputStream   bin=new  BufferedInputStream(new FileInputStream(f));  
            byte[]  buff=new  byte[((int)f.length())];  
            bin.read(buff);  
            String  str=new  String(buff,"utf-8");  
            String[] all=str.split(" ");   //以空格做为分隔符,文本里的内容现在存放在all[]数组中
   
   System.out.println("请输入要查找书的日期(格式为****.*):");
   System.out.println("请输入起始日期:");
   Scanner sc=new Scanner(System.in);
   String startDate=sc.next();
   System.out.println("请输入结束日期:");
   String endDate=sc.next();
   for(int i=0;i<all.length;i++)
   {
    if((all[i].compareTo(startDate)>=0)&&(all[i].compareTo(endDate)<=0))
     System.out.println("书名:"+all[i-2]+"     日期:"+all[i]);    
   }
  }
  catch(FileNotFoundException e){
   System.out.println(e);
  }
 }

 //修改books.txt里的内容,先把文件读到byte[]里,更新数组,再写回去
 public void modifyBookName() throws IOException  
 {
  try{
   File f=new File("books.txt");
   BufferedInputStream   bin=new  BufferedInputStream(new FileInputStream(f));  
            byte[]  buff=new  byte[((int)f.length())];  
            bin.read(buff);  
            String  str=new  String(buff,"utf-8");  
            String[] all=str.split(" ");   //以空格做为分隔符
  
            OutputStream fout=new FileOutputStream(f);  
   
   for(int i=0,j=2;i<all.length&&j<all.length;i=i+4,j=j+4)
    System.out.println("书名:"+all[i]+"     日期:"+all[j]);  
   System.out.println("请输入要修改的书名:");
   Scanner sc=new Scanner(System.in);
   String bookname=sc.next();
   String newbn=new String();
   System.out.println("您输入的书名是:"+bookname);
   for(int j=0;j<all.length;j++)
   {
    if(bookname.equals(all[j]))
    {
     System.out.println("请输入新书名:");
     newbn=sc.next();
     System.out.println("您输入的书名为:"+newbn);
    }
   }
   for(int k=0;k<all.length;k++)
   {
    all[k] = all[k].replaceAll(bookname,newbn); 
    fout.write((all[k]+" ").getBytes());                           
   }
   fout.flush(); 
   fout.close();
   bin.close();
  }
  catch(IOException ew){
   System.out.println(ew);
  }
 }

 public void modifyBookDate()
 {
  try{
   File f=new File("books.txt");
   BufferedInputStream   bin=new  BufferedInputStream(new FileInputStream(f));  
            byte[]  buff=new  byte[((int)f.length())];  
            bin.read(buff);  
            String  str=new  String(buff,"utf-8");  
            String[] all=str.split(" ");   //以空格做为分隔符
  
            OutputStream fout=new FileOutputStream(f);    
   
   for(int i=0,j=2;i<all.length&&j<all.length;i=i+4,j=j+4)
    System.out.println("书名:"+all[i]+"     日期:"+all[j]);   
   System.out.println("请输入要修改的日期:");
   Scanner sc=new Scanner(System.in);
   String bookname=sc.next();
   String newbn=new String();
   System.out.println("您输入的日期是:"+bookname);
   for(int j=0;j<all.length;j++)
   {
    if(bookname.equals(all[j]))
    {
     System.out.println("请输入新日期:");
     newbn=sc.next();
     System.out.println("您所输入的新日期为:"+newbn);
    }
   }
   for(int k=0;k<all.length;k++)
   {
    all[k]=all[k].replaceAll(bookname,newbn);  
    fout.write((all[k]+" ").getBytes());                                
   }
   fout.flush(); 
   fout.close();
   bin.close();
  }
  catch(IOException ew){
   System.out.println(ew);
  }
 }


 public void showAll()
 {
  try{
   File f=new File("books.txt");
   BufferedInputStream   bin=new  BufferedInputStream(new FileInputStream(f));  
            byte[]  buff=new  byte[((int)f.length())];  
            bin.read(buff);  
            String  str=new  String(buff,"utf-8");  
            String[] all=str.split(" ");   //以空格做为分隔符
   System.out.println("所有图书信息如下示:");
   for(int i=0,j=2;i<all.length&&j<all.length;i=i+4,j=j+4)
    System.out.println("书名:"+all[i]+"     日期:"+all[j]);  
  }
  catch(IOException ew){
   System.out.println(ew);
  }
 }
 public void showByDate() throws NullPointerException
 {
  try{
   File f=new File("books.txt");
   BufferedInputStream   bin=new  BufferedInputStream(new FileInputStream(f));
            byte[]  buff=new  byte[((int)f.length())];
            bin.read(buff);
            String  str=new  String(buff,"utf-8");
            String[] all=str.split(" ");   //以空格做为分隔符
   
   //以下功能是将文本内容里日期放到数组中
   int m=0;
   for(int i=2;i<all.length;i=i+4)
   {
    m=m+1;
   }
   String[] bookDate=new String[m];
   String[] bookName=new String[m];
   for(int i=2,j=0;i<all.length&&j<m;i=i+4,j++)
   {
    bookDate[j]=all[i];
   }
   for(int i=0,j=0;i<all.length&&j<m;i=i+4,j++)
   {
    bookName[j]=all[i];
   }
   //用冒泡排序法进行排序
   String changeDate;
   String changeName;
   for(int i=0;i<bookDate.length;i++)
   {
    for(int j=bookDate.length-1;j>=i;j--)
    {
     if(bookDate[i].compareTo(bookDate[j])>0)
     {
      changeDate=bookDate[i];
      changeName=bookName[i];

      bookDate[i]=bookDate[j];
      bookName[i]=bookName[j];

      bookDate[j]=changeDate;
      bookName[j]=changeName;

     }
    }
   }

   System.out.println("按日期从小到大排序,所有图书信息如下示:");
   for(int i=0;i<bookDate.length;i++)
   {
    System.out.println("书名:"+bookName[i]+"  日期:"+bookDate[i]);
   }
   
  }
  catch(IOException ew){
   System.out.println(ew);
  }
 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值