Java 图书馆 管理系统

图书馆资源管理系统
1、数据模型
图书、光盘、图画
共有属性:
编号、标题、作者、评级(一般、儿童、成人等)
图书:出版社、ISBN、页数、年份
光盘:出品者、出品年份、视频时长
图画:出品国籍、作品尺寸
2、功能
增加、删、改、查
统计:资源类别、年份等
3、数据持久存储
读取、保存

book.java

import java.io.Serializable;

public class book extends common implements Serializable {

		// TODO Auto-generated constructor stub
	//出版社、ISBN、页数、年份
	String press;
	String isbn;
	int page;
	int year;
	
	public book() {
		super();
	}
	
	public String getPress() {
		return press;
	}
	public void setPress(String press) {
		this.press = press;
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	
}

picture.java

import java.io.Serializable;

public class pictrue extends common implements Serializable {
    //出品国籍、作品尺寸
	String nation;
	String siz;
	public pictrue() {
		super();
	}
	public String getNation() {
		return nation;
	}
	public void setNation(String nation) {
		this.nation = nation;
	}
	public String getSiz() {
		return siz;
	}
	public void setSiz(String siz) {
		this.siz = siz;
	}
	
}

cd.java

import java.io.Serializable;

public class cd extends common implements Serializable {
   //出品者、出品年份、视频时长
	String out;
	int year;
	int tim;
	public cd() {
		super();
	}
	public String getOut() {
		return out;
	}
	public void setOut(String out) {
		this.out = out;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getTim() {
		return tim;
	}
	public void setTim(int tim) {
		this.tim = tim;
	}
	
}

common.java

import java.io.Serializable;

public class common implements Serializable{
	
	    //编号、标题、作者、评级(一般、儿童、成人等)
		int id;
		String title;
		String name;
		String level;
		
		public common() {
			
		}

		common(int id,String title,String name,String level)
		{
			this.id = id;
			this.title = title;
			this.name = name;
			this.level = level;
		}
		
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getLevel() {
			return level;
		}
		public void setLevel(String level) {
			this.level = level;
		}
		
	

}

demo.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;







public class demo {
	 public static void menu() {
	        System.out.println("=========图书管理系统==========");
	        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("==============================");
	        System.out.print("请选择: ");
	    }
	 public static void menu1() {
		    System.out.println("==============================");
	        System.out.println("          1.图书");
	        System.out.println("          2.光盘");
	        System.out.println("          3.图画");
	        System.out.println("==============================");
	        System.out.println("请选择操作对象:   ");
     }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ArrayList books = new ArrayList<>();
		File file1 = new File("D://xxx.txt" );
		if(file1.exists()) 
		   books = testReader1();
		
		File file2 = new File("D://xx.txt" );
		ArrayList cds = new ArrayList<>();
		if(file2.exists()) 
			 cds = testReader2();
		
		File file3 = new File("D://x.txt" );
		ArrayList pic = new ArrayList<>();
		if(file3.exists()) 
			 pic = testReader3();
		
        boolean flag = true;
		do {
        	 menu();
        	 Scanner input = new Scanner(System.in);
        	 int choice = input.nextInt();
        	 switch (choice) {
             case 1:
                 System.out.println("--->新增");
                 menu1();
                 int n1 = input.nextInt();
                 if(n1 == 1) add1(books);
                 else if(n1 == 2) add2(cds);
                 else if(n1 == 3) add3(pic);
                 else System.out.println("输入错误,请重新输入");
                 break;
             case 2:
                 System.out.println("--->删除");
                 menu1();
                 int n2 = input.nextInt();
                 if(n2 == 1) del1(books);
                 else if(n2 == 2) del2(cds);
                 else if(n2 == 3) del3(pic);
                 else System.out.println("输入错误,请重新输入");
                 
                 break;
             case 3:
                 System.out.println("--->修改");
                 menu1();
                 int n3 = input.nextInt();
                 if(n3 == 1) change1(books);
                 else if(n3 == 2) change2(cds);
                 else if(n3 == 3) change3(pic);
                 else System.out.println("输入错误,请重新输入");
          
                 break;
             case 4:
                 System.out.println("--->查看");
                 check(books,cds,pic);
                 break;
             case 5:
                 System.out.println("--->统计");
                 stat(books,cds,pic);
                 break;
             case 6:
                 System.out.println("--->您已退出");
                 break;
             default:
            	 System.out.println("输入序号错误,请重新输入");
            	 
         }
        	  if(choice == 6) flag = false;
        }while(flag == true);
		writeIntoFile1(books);
		writeIntoFile2(cds);
		writeIntoFile3(pic);
	}
  
	public static void add1(ArrayList array)
   {
		 //编号、标题、作者、评级(一般、儿童、成人等) 出版社、ISBN、页数、年份
		Scanner input = new Scanner(System.in);
		System.out.print("请输入编号:");
		int sid = input.nextInt();
		System.out.print("请输入标题:");
		String st = input.next();
		System.out.print("请输入作者:");
		String sa = input.next();
		System.out.print("请输入评级(一般、儿童、成人等):");
		String sl = input.next();
		System.out.print("请输入出版社:");
		String sp = input.next();
		System.out.print("请输入ISBN:");
		String si = input.next();
		System.out.print("请输入页数:");
		int spage = input.nextInt();
		System.out.print("请输入年份:");
		int sy = input.nextInt();
		
		book b = new book();
		b.setId(sid);
		b.setTitle(st);
		b.setName(sa);
		b.setLevel(sl);
		b.setPress(sp);
		b.setIsbn(si);
		b.setPage(spage);
		b.setYear(sy);
		
		array.add(b);
		
	   System.out.println("***********添加成功***********\n");
   }
	public static void add2(ArrayList array)
	   {
			
			Scanner input = new Scanner(System.in);
			System.out.print("请输入编号:");
			int sid = input.nextInt();
			System.out.print("请输入标题:");
			String st = input.next();
			System.out.print("请输入作者:");
			String sa = input.next();
			System.out.print("请输入评级(一般、儿童、成人等):");
			String sl = input.next();
			System.out.print("请输入出品者:");
			String sp = input.next();
			System.out.print("请输入出品年份:");
			int sy = input.nextInt();
			System.out.print("请输入视频时长:");
			int stim = input.nextInt();
	
			
			cd d = new cd();
			d.setId(sid);
			d.setTitle(st);
			d.setName(sa);
			d.setLevel(sl);
			d.setOut(sp);
			d.setTim(stim);
			d.setYear(sy);
			array.add(d);
			
		   System.out.println("***********添加成功***********\n");
	   }
	public static void add3(ArrayList array)
	   {
		
			Scanner input = new Scanner(System.in);
			System.out.print("请输入编号:");
			int sid = input.nextInt();
			System.out.print("请输入标题:");
			String st = input.next();
			System.out.print("请输入作者:");
			String sa = input.next();
			System.out.print("请输入评级(一般、儿童、成人等):");
			String sl = input.next();
			System.out.print("请输入国籍:");
			String nat = input.next();
			System.out.print("请输入作品尺寸:");
			String ssiz = input.next();
	
			pictrue p = new pictrue();
			p.setId(sid);
			p.setTitle(st);
			p.setName(sa);
			p.setLevel(sl);
			p.setNation(nat);
			p.setSiz(ssiz);
			array.add(p);
			
		   System.out.println("***********添加成功***********\n");
	   }
	public static void check(ArrayList array1,ArrayList array2,ArrayList array3)
	{
		System.out.println("图书:");
		for(int i = 0;i < array1.size();i ++)
		{
			book b = array1.get(i);
			System.out.println("编号为:"+b.getId());
			System.out.println("标题为:"+b.getTitle());
			System.out.println("作者为:"+b.getName());
			System.out.println("评级为:"+b.getLevel());
			System.out.println("出版社为:"+b.getPress());
			System.out.println("ISBN为:"+b.getIsbn());
			System.out.println("页数为:"+b.getPage());
			System.out.print("年份为:"+b.getYear());
			System.out.println("\n");	
		}
		System.out.println("*********************************");
		System.out.println("光盘:");
		for(int i = 0;i < array2.size();i ++)
		{
			cd b = array2.get(i);
			System.out.println("编号为:"+b.getId());
			System.out.println("标题为:"+b.getTitle());
			System.out.println("作者为:"+b.getName());
			System.out.println("评级为:"+b.getLevel());
			System.out.println("出品者为:"+b.getOut());
			System.out.println("出品年份为:"+b.getYear());
			System.out.println("视频时长为:"+b.getTim());
			System.out.println("\n");	
		}
		System.out.println("*********************************");
		System.out.println("图画:");
		for(int i = 0;i < array3.size();i ++)
		{
			pictrue b = array3.get(i);
			System.out.println("编号为:"+b.getId());
			System.out.println("标题为:"+b.getTitle());
			System.out.println("作者为:"+b.getName());
			System.out.println("评级为:"+b.getLevel());
			System.out.println("国籍为:"+b.getNation());
			System.out.println("尺寸为:"+b.getSiz());
			System.out.println("\n");	
		}
		System.out.println("*********************************");
	}
	
	public static void del1(ArrayList array)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要删除的对象编号:");
		int num = input.nextInt();
		boolean f = false;
		for(int i = 0;i < array.size();i ++)
		{
			book b = array.get(i);
			if(b.getId() == num)
			{
			   array.remove(i);
			   f = true;
			}
		}
	    if(f) System.out.println("***********删除成功!***********\n");
	    else System.out.println("失败,编号不存在");
	}
	
	public static void del2(ArrayList array)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要删除的对象编号:");
		int num = input.nextInt();
		boolean f = false;
		for(int i = 0;i < array.size();i ++)
		{
			cd b = array.get(i);
			if(b.getId() == num)
			{
				   array.remove(i);
				   f = true;
			}
		}
		 if(f) System.out.println("***********删除成功!***********\n");
		 else System.out.println("失败,编号不存在");
	}

	public static void del3(ArrayList array)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要删除的对象编号:");
		int num = input.nextInt();
		boolean f = false;
		for(int i = 0;i < array.size();i ++)
		{
			pictrue b = array.get(i);
			if(b.getId() == num)
			{
				   array.remove(i);
				   f = true;
			}
		}
		 if(f) System.out.println("***********删除成功!***********\n");
		 else System.out.println("失败,编号不存在");
	}
	
	public static void change1(ArrayList array)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要修改的对象编号:");
		int x = input.nextInt();
		
		System.out.print("请输入新编号:");
		int sid = input.nextInt();
		System.out.print("请输入新标题:");
		String st = input.next();
		System.out.print("请输入新作者:");
		String sa = input.next();
		System.out.print("请输入新评级(一般、儿童、成人等):");
		String sl = input.next();
		System.out.print("请输入新出版社:");
		String sp = input.next();
		System.out.print("请输入新ISBN:");
		String si = input.next();
		System.out.print("请输入新页数:");
		int spage = input.nextInt();
		System.out.print("请输入新年份:");
		int sy = input.nextInt();
		
		book b = new book();
		b.setId(sid);
		b.setTitle(st);
		b.setName(sa);
		b.setLevel(sl);
		b.setPress(sp);
		b.setIsbn(si);
		b.setPage(spage);
		b.setYear(sy);
		
		boolean f = false;
		for(int i = 0;i < array.size();i ++)
		{
			book t = array.get(i);
			if(t.getId() == x)
			{
			   array.set(i, b);
			   f = true;
			   break;
			}
		}
		if(f)System.out.println("***********修改成功!***********\n");
		else System.out.println("失败,编号不存在");
	}
	
	public static void change2(ArrayList array)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要修改的对象编号:");
		int x = input.nextInt();
		
		System.out.print("请输入新编号:");
		int sid = input.nextInt();
		System.out.print("请输入新标题:");
		String st = input.next();
		System.out.print("请输入新作者:");
		String sa = input.next();
		System.out.print("请输入新评级(一般、儿童、成人等):");
		String sl = input.next();
		System.out.print("请输入新出品者:");
		String sp = input.next();
		System.out.print("请输入新出品年份:");
		int sy = input.nextInt();
		System.out.print("请输入新视频时长:");
		int stim = input.nextInt();

		
		cd d = new cd();
		d.setId(sid);
		d.setTitle(st);
		d.setName(sa);
		d.setLevel(sl);
		d.setOut(sp);
		d.setTim(stim);
		d.setYear(sy);
		
		boolean f = false;
		for(int i = 0;i < array.size();i ++)
		{
			cd t = array.get(i);
			if(t.getId() == x)
			{
				array.set(i, d);
				f = true;
				break;
			}
			 
		}
		if(f)System.out.println("***********修改成功!***********\n");
		else System.out.println("失败,编号不存在");
	}
	public static void change3(ArrayList array)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要修改的对象编号:");
		int x = input.nextInt();
		
		System.out.print("请输入新编号:");
		int sid = input.nextInt();
		System.out.print("请输入新标题:");
		String st = input.next();
		System.out.print("请输入新作者:");
		String sa = input.next();
		System.out.print("请输入新评级(一般、儿童、成人等):");
		String sl = input.next();
		System.out.print("请输入国籍:");
		String nat = input.next();
		System.out.print("请输入作品尺寸:");
		String ssiz = input.next();

		pictrue p = new pictrue();
		p.setId(sid);
		p.setTitle(st);
		p.setName(sa);
		p.setLevel(sl);
		p.setNation(nat);
		p.setSiz(ssiz);
		
		boolean f = false;
		for(int i = 0;i < array.size();i ++)
		{
			pictrue t = array.get(i);
			if(t.getId() == x)
			{
				 array.set(i, p);
			 	 f = true;
			     break;
			}
			
		}
		if(f)System.out.println("***********修改成功!***********\n");
		else System.out.println("失败,编号不存在");
	}
	public static void stat(ArrayList array1,ArrayList array2,ArrayList array3)
	{
		    System.out.println("==============================");
	        System.out.println("          1.资源类别");
	        System.out.println("          2.年份");
	        System.out.println("==============================");
	        System.out.println("请选择:   ");
	        Scanner input = new Scanner(System.in);
	        int num = input.nextInt();
	        if(num == 1)
	        {
	        	System.out.println("图书共计:"+array1.size());
	        	System.out.println("光碟共计:"+array2.size());
	        	System.out.println("图画共计:"+array3.size());
	        }else
	        {
	        	System.out.println("请输入要统计的年份:");
				int x = input.nextInt();
				int cnt1 = 0,cnt2 = 0;
				for(int i = 0;i < array1.size();i ++)
				{
					book b = array1.get(i);
					if(b.getYear() == x)
					   cnt1 ++;
				}
				  for(int i = 0;i < array2.size();i ++)
				{
					cd d = array2.get(i);
					if(d.getYear() == x)
					   cnt2 ++;
				}
				
				System.out.println("该年份图书共计:"+cnt1);
				System.out.println("该年份光盘共计:"+cnt2);

	        }
			
	}
	


    public static void writeIntoFile1(ArrayList array ){
	     ObjectOutputStream oos ;
	     try {
	           oos = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream( "D://xxx.txt" )));
	          
	           oos .writeObject(array);
	          
	           oos .flush();
	           oos .close();      
	    } catch (IOException e ) {
	           // TODO Auto-generated catch block
	           e .printStackTrace();
	    } finally {
	          System. out .println( "图书文件写入完成!" );
	    }
	}
    
    public static ArrayList testReader1(){
    	 ArrayList b = null ;
         try {
              ObjectInputStream ois = new ObjectInputStream( new BufferedInputStream( new FileInputStream( "D://xxx.txt" )));
               b =(ArrayList) ois.readObject();
               ois .close();
        } catch (IOException e ) {
               // TODO Auto-generated catch block
               e .printStackTrace();
        } catch (ClassNotFoundException e ) {
               // TODO Auto-generated catch block
               e .printStackTrace();
        } finally {
              System. out .println( "图书文件读取成功!" );
        }
         return b ;
  }

    public static void writeIntoFile2(ArrayList array ){
	     ObjectOutputStream oos ;
	     try {
	           oos = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream( "D://xx.txt" )));
	          
	           oos .writeObject(array);
	          
	           oos .flush();
	           oos .close();      
	    } catch (IOException e ) {
	           // TODO Auto-generated catch block
	           e .printStackTrace();
	    } finally {
	          System. out .println( "光盘文件写入完成!" );
	    }
	}
    
    public static ArrayList testReader2(){
   	 ArrayList b = null ;
        try {
             ObjectInputStream ois = new ObjectInputStream( new BufferedInputStream( new FileInputStream( "D://xx.txt" )));
              b =(ArrayList) ois.readObject();
              ois .close();
       } catch (IOException e ) {
              // TODO Auto-generated catch block
              e .printStackTrace();
       } catch (ClassNotFoundException e ) {
              // TODO Auto-generated catch block
              e .printStackTrace();
       } finally {
             System. out .println( "光盘文件读取成功!" );
       }
        return b ;
 } 
      
    public static void writeIntoFile3(ArrayList array ){
	     ObjectOutputStream oos ;
	     try {
	           oos = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream( "D://x.txt" )));
	          
	           oos .writeObject(array);
	          
	           oos .flush();
	           oos .close();      
	    } catch (IOException e ) {
	           // TODO Auto-generated catch block
	           e .printStackTrace();
	    } finally {
	          System. out .println( "图画文件写入完成!" );
	    }
	}
   
   public static ArrayList testReader3(){
  	 ArrayList b = null ;
       try {
            ObjectInputStream ois = new ObjectInputStream( new BufferedInputStream( new FileInputStream( "D://x.txt" )));
             b =(ArrayList) ois.readObject();
             ois .close();
      } catch (IOException e ) {
             // TODO Auto-generated catch block
             e .printStackTrace();
      } catch (ClassNotFoundException e ) {
             // TODO Auto-generated catch block
             e .printStackTrace();
      } finally {
            System. out .println( "图画文件读取成功!" );
      }
       return b ;
}
    
}

你可能感兴趣的:(java,java,开发语言)