• JAVA作业-.图书馆查书、借书、还书


    JAVA作业-.图书馆查书、借书、还书

    1.UML类图

    2.功能展示

    1. 系统界面

    ####2.查书功能

    (1)查看所有图书

    (2)按照作者或书名查书

    **####3.查看已借的书并对其进行归或借操作

    ####4.还书

    3.核心代码

    1.DAO展示
    package library;
    
    import java.util.ArrayList;
    
    
    public interface CartDao{
    
    	public ArrayList<Item> getList();
    	public void addBook(Book book);  //加入书籍
    	public void deleteBook(int num);  //归还书籍
    	public void showCart();		//查看借阅书的情况
    	public void clearCart();		//归还所有借阅的书籍
    	
    }
    
    2.主函数
    package library;
    import java.io.IOException;//导入 这个包来捕获输入输出异常
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class main {
    	public static void main(String[] args) throws NumberFormatException, IOException 
    	{
    		Scanner sc =new Scanner(System.in);
    		Mall mall=new Mall();
    		CartDao cart =new CartDaoLisImpl();
    		System.out.println("    欢迎来到图书馆");
    		System.out.println("接下来你将进入图书管理系统");
    		while(true)
    		{
    			System.out.println("1.浏览图书");
    			System.out.println("2.查找你想要的图书");
    			System.out.println("3.查看你借的书");
    			System.out.println("4.归还你借的书");
    			System.out.println("5.退出");
    			System.out.println("请输入操作");
    			int flag=0;
    			int n=sc.nextInt();
    			sc.nextLine();
    			switch(n)
    			{
    			case 1:
    				System.out.println("所有的图书:");
    				Mall.showAllBook();
    				System.out.println("请输入你要借的书的编号");
    				n=sc.nextInt();
    				while(n>mall.getList().size())
    				{
    					System.out.println("抱歉本图书馆暂无该图书,请重新输入!");
    					n=sc.nextInt();
    				}
    				cart.addBook(mall.getList().get(n-1));
    				break;
    				
    			case 2:
    				System.out.println("1.按照书名查找");
    				System.out.println("2.按照作者查找");
    				n=sc.nextInt();
    				sc.nextLine();
    				if(n==1)
    				{
    					System.out.println("请输入你要查找的书名");
    					String s1=sc.next();
    					ArrayList<Book> searchOfGoods =Mall.searchName(s1);
    					
    					//System.out.println(searchOfGoods1.size());
    					for(int i=0;i<searchOfGoods.size();i++)
    					{
    						System.out.println((i+1)+":"+searchOfGoods.get(i));
    					}
    					
    					System.out.println("请选择要借的图书的编号:");
    					n=sc.nextInt();
    					cart.addBook(searchOfGoods.get(n-1));
    				}
    				else
    				{
    					System.out.println("请输入你要查找到作者");
    					String s1=sc.next();
    					ArrayList<Book> searchOfGoods =Mall.searchPrice(s1);
    					
    					for(int i=0;i<searchOfGoods.size();i++)
    					{
    						System.out.println((i+1)+":"+searchOfGoods.get(i));
    					}
    					System.out.println("请选择要借的书的编号:");
    					n=sc.nextInt();
    					cart.addBook(searchOfGoods.get(n-1));
    				}
    				break;
    			case 3:
    				while(flag==0)
    				{
    					System.out.println();
    					System.out.println("******************************************************************************************************");  
    					System.out.println("你的借阅情况如下:");
    					cart.showCart();
    					System.out.println("******************************************************************************************************");  
    					System.out.println("1.想再借书");
    					System.out.println("2.归还书籍");
    					System.out.println("3.归还所有的书籍");
    					System.out.println("4.退出借阅表");
    					System.out.println("请输入操作");
    					n=sc.nextInt();
    					switch(n)
    					{
    					case 1:
    						System.out.println("1.浏览书库来借书");
    						System.out.println("2.查找书籍来借书");
    						int n1=sc.nextInt();
    						sc.nextLine();
    						switch(n1)
    						{
    						case 1:
    							System.out.println("所有的图书:");
    							Mall.showAllBook();
    							System.out.println("请输入你要借的书的编号");
    							n1=sc.nextInt();
    							while(n1>mall.getList().size())
    							{
    								System.out.println("抱歉本图书馆暂无该图书,请重新输入!");
    								n1=sc.nextInt();
    							}
    							cart.addBook(mall.getList().get(n1-1));
    							break;
    							
    						case 2:
    							System.out.println("1.按照书名查找");
    							System.out.println("2.按照作者查找");
    							n1=sc.nextInt();
    							sc.nextLine();
    							if(n1==1)
    							{
    								System.out.println("请输入你要查找的书名");
    								String s1=sc.next();
    								ArrayList<Book> searchOfGoods =Mall.searchName(s1);
    								
    								//System.out.println(searchOfGoods1.size());
    								for(int i=0;i<searchOfGoods.size();i++)
    								{
    									System.out.println((i+1)+":"+searchOfGoods.get(i));
    								}
    								
    								System.out.println("请选择要借的图书的编号:");
    								n1=sc.nextInt();
    								cart.addBook(searchOfGoods.get(n1-1));
    							}
    							else
    							{
    								System.out.println("请输入你要查找到作者");
    								String s1=sc.next();
    								ArrayList<Book> searchOfGoods =Mall.searchPrice(s1);
    								
    								for(int i=0;i<searchOfGoods.size();i++)
    								{
    									System.out.println((i+1)+":"+searchOfGoods.get(i));
    								}
    								System.out.println("请选择要借的书的编号:");
    								n1=sc.nextInt();
    								cart.addBook(searchOfGoods.get(n1-1));
    							}
    							break;
    							}
    						break;
    					case 2:
        					
    						System.out.println("请输入要归还书籍的序号");
    						n=sc.nextInt();
    						cart.deleteBook(n-1);
    						break;
    					case 3:
    						
    						cart.clearCart();
    						System.out.println("已归还所有书籍");
    						break;
    					case 4:
    						flag=1;
    						break;
    					}
    			
    					
    					
    				}
    				break;
    			case 4:
    				while(true)
    				{
    
    					System.out.println();
    					System.out.println("******************************************************************************************************");  
    					System.out.println("你的借阅情况如下:");
    					cart.showCart();
    					System.out.println("******************************************************************************************************");  
    					System.out.println("1.归还书籍");
    					System.out.println("2.归还所有的书籍");
    					System.out.println("3.退出");
    					System.out.println("请输入操作");
    					n=sc.nextInt();
    					switch(n)
    					{
    					
    					case 1:
        					
    						System.out.println("请输入要归还书籍的序号");
    						n=sc.nextInt();
    						cart.deleteBook(n-1);
    						break;
    					case 2:
    						
    						cart.clearCart();
    						System.out.println("已归还所有书籍");
    						break;
    					case 3:
    						flag=1;
    						break;
    					}
    			
    					
    					break;	
    				}
    				break;
    			case 5:
    				System.out.println("图书馆欢迎您!");
    				return ;
    			
    			}
    			
    		}
    		
    	}
    }
    
    
    3.查书功能
    package library;
    
    import java.io.BufferedReader;//读取文本
    import java.io.FileReader;//读取文件
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.StringTokenizer;//分割字符串
    
    import library.Book;
    
    
    public class Mall {
    static ArrayList<Book > mallList= new ArrayList<Book >();
    	
    	
    	public static ArrayList<Book> getList() {
    		return mallList;
    	}
    	
    	public static void showAllBook() throws NumberFormatException, IOException//抛出异常判断
    	{
    		BufferedReader in = new BufferedReader(new FileReader("F:\goods.txt"));//读取文件
    		String s;
    		Book goods;
    		int i=1;
    		while ((s = in.readLine()) != null)
    		{
    			StringTokenizer p = new StringTokenizer(s, " ");// 以" "为分隔符进行遍历
    			String goodsName=p.nextToken();//读取书名
    			String goodsPrice=p.nextToken();//读取作者
    			
    			if(i<=29)
    			{
    				goods=new Books(goodsName,goodsPrice);
    				mallList.add(goods);
    				System.out.println(i+":"+goods.toString());//输出所有的书以及其作者
    				i++;
    			}
    		
    				
    		}
    		in.close();
    		
    	}
    
    
    	public static ArrayList<Book> searchName(String type){//按照书名进行查找
    	ArrayList<Book> good = new ArrayList<Book>();
    		for(int i=0;i<mallList.size();i++)
    		{
    			if(mallList.get(i).getGoodsName().equals(type))
    			{
    				good.add(mallList.get(i));
    			}
    		}
    		
    		return good;
    	}
    
    	public static ArrayList<Book> searchPrice(String type) {//按照作者查找
    	ArrayList<Book> good = new ArrayList<Book>();
    		
    		
    		for(int i=0;i<mallList.size();i++)
    		{
    			if(mallList.get(i).getGoodsPrice().equals(type))
    			{
    				good.add(mallList.get(i));
    			}
    		}
    		return good;
    	}
    	
    }
    
    
    4.借书,还书操作
    package library;
    
    import java.util.ArrayList;
    
    public  class CartDaoLisImpl implements CartDao{
    	
    	ArrayList<Item> cartList= new ArrayList<Item>();
    	public ArrayList<Item> getList() {
    		return cartList;
    	}
    	public void addBook(Book goods)
    	{
    		int i;
    		for(i=0;i<cartList.size();i++)
    		{
    			if(cartList.get(i).getItem().equals(goods)==true)
    			{
    				cartList.get(i).increase();
    				break;
    			}
    		}
    		if(i>=cartList.size())
    		{
    			Item e = new Item(goods);
    			cartList.add(e);
    		}	
    	}
    	public void deleteBook(int num)
    	{
    		if(cartList.size()==0)
    		{
    			System.out.println("你的借阅记录为空");
    			return;
    		}
    		if(num<cartList.size())
    		{
    			if(cartList.get(num).getNum()==1) 
    			{
    				cartList.remove(num);
    			}
    			else
    			{
    				cartList.get(num).reduce();
    			}
    		}
    		if(num>cartList.size())
    		{
    			System.out.println("该书籍还未收集");
    		}	
    	}
    	public void showCart()//查看借阅的书的记录
    	{
    		for(int i=0;i<cartList.size();i++)
    		{
    			System.out.println((i+1)+":"+cartList.get(i).toString());
    		}
    	}
    	public void clearCart()//归还所有的书
    	{
    		cartList.clear();
    	}
    	
    	
    	
    }
    class Item 							
    {
    	private Book item; //记录借阅的书
    	private int num;    //记录借阅的书的数量
    	
    	public Item(Book item)
    	{
    		this.item = item;
    		num = 1;
    	}
    	public void increase() //再次借书
    	{
    		num++;
    	}
    	public void reduce() //归还书籍
    	{
    		num--;
    	}
    	
    	@Override
    	public String toString() {
    		return item + "      数量:" + num;
    	}
    	public Book getItem() {
    		return item;
    	}
    	public void setItem(Book item) {
    		this.item = item;
    	}
    	public int getNum() {
    		return num;
    	}
    	public void setNum(int num) {
    		this.num = num;
    	}
    	
    }
    
  • 相关阅读:
    jmeter 数据库类型的脚本
    jmeter 协议到脚本编写
    python socket粘包
    微信公众号开发
    常用windows和office激活工具
    分辨率等概念
    设置单元格同高或同宽
    单元格内容前或后增加内容
    单元格内数字复制和递增
    excel单元格内容换行
  • 原文地址:https://www.cnblogs.com/lowena/p/14110712.html
Copyright © 2020-2023  润新知