java图书管理系统实现

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_38499625/article/details/79614017

题目:编写java代码,实现对图书的管理,包括增删改查、借阅及归还。

实现代码如下:

import java.util.Scanner;

public class BookTest {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		BookManager book = new BookManager();
		
		boolean flag=true;
		while (flag) {
			//界面
			System.out.println("------------");
			System.out.println("欢迎来到图书管理系统");
			System.out.println("0 借阅排行榜");
			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.println("请选择:");
			
			
			int choice = sc.nextInt();			
			//   try/catch
			try {
				book.Manage(choice);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			
		}
	}

}

import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.Set;

public class BookManager{
	LinkedHashMap<String, Book> stu = new LinkedHashMap<String, Book>();
	Book book = new Book();
	Scanner sc = new Scanner(System.in);
		
	public void Manage(int choice) throws Exception{			
			switch (choice) {
				case 0:
					lists();
					break;
				case 1:
					add();
					break;
				case 2:
					find();
					break;
				case 3:
					delete();
					break;
				case 4:
					borrow();
					break;
				case 5:
					back();
					break;
				case 6:
					System.out.println("您已退出该系统");					
					break;
				default: {
					throw new Exception("没有该功能,请重新选择!");
				}		
			}		
	}
	
	//排行榜
	public void lists() {
		Set<String> set=stu.keySet();
		for(String key:set) {
			if(stu.get(key).getNum()<stu.get(key+1).getNum()) {
				System.out.println(stu.get(key));
			}
		}	
	}

	//添加方法
	public void add() {
		System.out.println("请输入所要添加的图书编号:");
		String key = sc.next();
		System.out.println("请输入所要添加的图书名称:");
		String name = sc.next();
		System.out.println("请输入所要添加的图书码 :");
		String id = sc.next();
		System.out.println("请输入所要添加的图书价格");
		double price = sc.nextDouble();
		System.out.println("请输入所要添加的图书数目:");
		int num = sc.nextInt();
		
		stu.put(key, new Book(name,id,price,num));
		System.out.println("添加完成!");
		find();
	}

	//查看方法
	public void find() {
		System.out.println("所有图书如下:");
		Set<String> set=stu.keySet();
		for(String key:set) {
			System.out.print(stu.get(key));	
			if(stu.get(key).getNum()==0) {
				System.out.println("不可借");
			}else if(stu.get(key).getNum()>0) {
				System.out.println("可借");
			}
		}	
	}

	//删除方法\
	public void delete() {
		System.out.println("请输入删除的图书名称:");
		String key2 = sc.next();
		
		Set<String> set=stu.keySet();
		for(String key:set) {
			if(stu.get(key).getName().equals(key2)) {
				stu.remove(key);
				System.out.println("删除成功");
			}else {
				System.out.println("该图书不存在");
			}
			
		}		
	}

	//借阅方法
	public void borrow() {
		System.out.println("请输入您所要借阅的图书名称:");
		String names = sc.next();
		Set<String> set=stu.keySet();
		for(String key:set) {
			if(stu.get(key).getName().equals(names)) {
				if(stu.get(key).getNum()>0) {				
					int num=stu.get(key).getNum()-1;
					stu.get(key).setNum(num);
					System.out.println("借阅成功");
				}else {
					System.out.println("该书库存不足");
				}
			}else {
				System.out.println("该图书不存在");
			}		
		}	
	}

	//归还方法
	public void back() {
		System.out.println("请输入所要归还的图书名称:");
		String names = sc.next();
		Set<String> set=stu.keySet();
		for(String key:set) {
			if(stu.get(key).getName().equals(names)) {
				int num=stu.get(key).getNum()+1;
				stu.get(key).setNum(num);
				System.out.println("归还成功");
			}else {
				System.out.println("该书不在本库中");
			}		
		}
	}
}
public class Book {

	private String id;
	private String name;
	private double price;
	private int num;

	public Book() {

	}

	public Book(String name, String id, double price, int num) {
		this.name = name;
		this.id = id;
		this.price = price;
		this.num = num;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toString() {
		return this.name + "\t" + this.id + "\t" + this.price + "\t" + this.num;
	}
}

总结:此次的借阅排行榜并未真正实现,接下来会补充

展开阅读全文

没有更多推荐了,返回首页