图书管理系统完整代码

package com.youjiuye.bms;

/**
 * @author 丢了风筝的线
 * 2019-12-2
 * 图书管理系统的入口
 */
import com.youjiuye.service.Tool;

public class Bms {
	public static void main(String[] args) {
		Tool.Wellcome();
		Tool.systemOperation();
		
	}
}

package com.youjiuye.service;

import com.youjiuye.bean.Book;

/**
 * 
 * @author 丢了风筝的线 2019-12-2 图书管理系统的服务类/功能模块
 */
public class Service {

	// 根据书籍类型查看图书
	public Book[] getBooksByType(String type, Book[] books) {
		Book[] books1 = new Book[20];
		int index = 0;

		// 查找相关类型的书并且存在books1
		for (int i = 0; i < books.length; i++) {
			if (books[i] != null && books[i].getType() == type) {

				// 存储到books1中
				books1[index++] = books[i];
			}
		}
		return books1;
	}

	// 展示书籍
	public boolean showBooks(Book[] books) {
		boolean bo = false;
		for (int i = 0; i < books.length; i++) {
			if (books[i] != null) {
				System.out.println(books[i]);
				bo = true;
			}
		}
		return bo;
	}

	/**
	 * 借书操作
	 * 
	 * @param book
	 *            所借的书籍
	 * @param books
	 *            图书馆里的图书 每借一本,图书馆里的图书减少一本,并且自己的借阅书库中添加一本
	 */
	public void borrowBook(Book book, Book[] lbooks, Book[] userBooks) {
		if (book != null) {
			for (int i = 0; i < lbooks.length; i++) {

				// 如果该容器中的这本书不是null 才进行判断
				if (lbooks[i] != null) {

					// 容器中书的id
					String id1 = lbooks[i].getId();

					// 要借的书的id
					String id2 = book.getId();

					// 基本数据类型的等值判断用==,引用类型的等值判断用方法
					if (id1.equals(id2)) {
						lbooks[i] = null;
						System.out.println("借阅成功!");
						break;
					}
				}
			}
			for (int i = 0; i < userBooks.length; i++) {
				if (userBooks[i] == null) {
					userBooks[i] = book;
					break;
				}
			}
		}

	}

	// 还书
	public void returnBook(Book book, Book[] Lbooks, Book[] userBooks) {
		if (book != null) {
			for (int i = 0; i < Lbooks.length; i++) {
				if (Lbooks[i] == null) {
					Lbooks[i] = book;
					break;
				}
			}
			for (int i = 0; i < userBooks.length; i++) {
				if (userBooks[i] != null && userBooks[i].getId().equals(book.getId())) {
					userBooks[i] = null;
				}
			}
		}

	}

	// 修改书籍类型
	public boolean changeBookType(Book book, String type, Book[] books) {
		boolean bo = false;
		if (book != null) {
			for (int i = 0; i < books.length; i++) {
				if (books[i] != null) {
					if (books[i].getId().equals(book.getId())) {

						// 修改藏书中的书籍类型
						books[i].setType(type);
						break;
					}
				}
			}
		}
		return bo;
	}

	// 添加书籍信息
	public void addBook(Book[] books, Book book) {
		if (book != null) {
			for (int i = 0; i < books.length; i++) {
				if (books[i] == null) {
					books[i] = book;
					System.out.println("添加成功!");
					break;
				}
			}
		}
	}

	/**
	 * 跟据图书的编号生成图书对象
	 * 
	 * @param id
	 *            书的编号
	 * @param books
	 *            图书馆里的图书
	 * @return 一个book对象
	 */
	public Book getBook(String id, Book[] books) {
		Book bo = null;
		for (int i = 0; i < books.length; i++) {
			if (books[i] != null && books[i].getId().equals(id)) {
				bo = books[i];
			}
		}
		return bo;
	}

	// 判断当前的书库是否为空
	public boolean judegIsEmpty(Book[] b) {
		boolean bo = false;
		for (int i = 0; i < b.length; i++) {
			if (b[i] != null) {
				bo = true;
			}
		}
		return bo;
	}

}

package com.youjiuye.service;

import java.util.Scanner;

import com.youjiuye.bean.Book;
import com.youjiuye.bean.Library;
import com.youjiuye.bean.Users;

/**
 * 
 * @author 丢了风筝的线 2019-12-2 用来给用户进行一些操作提示,并判断用户的输入
 */
public class Tool {

	// 欢迎页
	public static void Wellcome() {
		System.out.println("***********************************");
		System.out.println("\t欢迎来到何老板图书馆                        ");
		System.out.println("***********************************");
	}

	// 获取用户输入身份信息
	public static Users inputIdentityInfo(Users u) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入您的姓名:");
		String name = sc.next();
		System.out.println("请选择您的身份(用户/管理员):");
		String identity = sc.next();
		System.out.println("请输入密码:");
		String password = sc.next();
		if (identityCheck(identity, password)) {
			System.out.println("登录成功!");
			u = new Users(name, identity);
			System.out.println("欢迎 " + identity + " " + name + " 来到系统");
			hint(identity);
		} else {
			System.out.println("您输入的信息有误,请重新输入!");
			u = inputIdentityInfo(u);
		}
		return u;
	}

	// 验证用户的身份
	public static boolean identityCheck(String name, String password) {
		boolean bo = false;
		if (name.equals("用户")) {
			if (password.equals("123")) {
				bo = true;
			}
		} else if (name.equals("管理员")) {
			if (password.equals("111")) {
				bo = true;
			}
		}
		return bo;
	}

	// 系统功能提示
	public static void hint(String identity) {
		System.out.println("请选择您想要的服务");
		if (identity.equals("用户")) {
			System.out.println("1、借书\t2、查看已借书籍\t3、还书");
		} else if (identity.equals("管理员")) {
			System.out.println("1、添加书籍\t2、修改书籍信息");
		}
	}

	// 确认用户选择的操作
	public static int judgeChoseOperation() {
		Scanner sc = new Scanner(System.in);
		int chose = sc.nextInt();
		return chose;
	}

	// 借阅书籍
	public static boolean borrow(Service s, Library l, Users u) {
		boolean bo = true;
		while (bo) {
			System.out.println("当前图书馆中藏书:");
			s.showBooks(l.getBooks());
			System.out.println("请输入要借阅的图书编号:");
			Scanner sc = new Scanner(System.in);
			String bookid = sc.next();
			Book boo = s.getBook(bookid, l.getBooks());
			s.borrowBook(boo, l.getBooks(), u.getBos());
			System.out.println("当前已成功借阅书籍:");
			s.showBooks(u.getBos());
			System.out.println("是否继续借阅书籍(y/n):");
			boolean b = false;
			bo = judgeOperationCon(b);
		}
		return bo;
	}

	// 系统操作
	public static void systemOperation() {
		Users u0 = null;
		Users u = inputIdentityInfo(u0);
		if (u.getIdentity().equals("用户")) {
			int chose = judgeChoseOperation();
			userOperationDetail(u, chose);
		} else {
			int chose = judgeChoseOperation();
			adminOperationDetail(u, chose);
		}
	}

	// 用户操作

	// 管理员操作
	public static void adminOperationDetail(Users u, int chose) {
		Service s = new Service();
		Library l = new Library("和老板图书馆");
		switch (chose) {
		case 1:

			// 添加书籍
			addBook(s, l);
			System.out.println("是否进行其他操作(y/n):");
			boolean boo = false;
			if (judgeOperationCon(boo)) {
				hint(u.getIdentity());
				adminOperationDetail(u, judgeChoseOperation());
			}

			break;
		case 2:

			// 修改书籍
			modifyBook(s, l);
			System.out.println("是否进行其他操作(y/n):");
			boolean booo = false;
			if (judgeOperationCon(booo)) {
				hint(u.getIdentity());
				adminOperationDetail(u, judgeChoseOperation());
			}
			break;
		default:
			System.out.println("进行正确 的输入!");
			System.out.println("1、添加书籍\t2、修改书籍信息");
			int rchose = judgeChoseOperation();
			adminOperationDetail(u, rchose);
			break;
		}
	}

	// 添加书籍信息
	public static void addBook(Service s, Library l) {
		boolean b = true;
		while (b) {
			System.out.println("当前图书馆中藏书:");
			s.showBooks(l.getBooks());
			System.out.println("请输入要还书的编号:");
			Scanner sc = new Scanner(System.in);
			String bookid = sc.next();
			System.out.println("请输入要还书的名字:");
			String bookName = sc.next();
			System.out.println("请输入要还书的作者:");
			String bookAuthor = sc.next();
			System.out.println("请输入要还书的类型:");
			String bookType = sc.next();
			Book book = new Book(bookid, bookName, bookType, bookAuthor);
			s.addBook(l.getBooks(), book);
			System.out.println("当前图书馆中的图书:");
			s.showBooks(l.getBooks());
			System.out.println("是否继续添加书籍(y/n):");
			boolean bo = false;
			b = judgeOperationCon(bo);
		}

	}

	// 修改书籍信息
	public static void modifyBook(Service s, Library l) {
		boolean b = true;
		while (b) {
			System.out.println("当前图书馆中藏书:");
			s.showBooks(l.getBooks());
			System.out.println("请输入要修改的图书的编号:");
			Scanner sc = new Scanner(System.in);
			String bookid = sc.next();
			Book book = s.getBook(bookid, l.getBooks());
			if (book != null) {
				s.addBook(l.getBooks(), book);
				System.out.println("当前图书馆中的图书:");
				s.showBooks(l.getBooks());
				System.out.println("是否继续添加书籍(y/n):");
				boolean bo = false;
				b = judgeOperationCon(bo);
			} else {
				System.out.println("对不起,当前书库中没有该图书");
				System.out.println("是否继续修改(y/n):");
				boolean bo = false;
				b = judgeOperationCon(bo);
			}

		}

	}

	// 用户操作
	public static void userOperationDetail(Users u, int chose) {
		Service s = new Service();
		Library l = new Library("和老板图书馆");
		switch (chose) {
		case 1:

			// 借阅书籍
			if (!borrow(s, l, u)) {
				System.out.println("是否进行其他操作(y/n):");
				boolean bo = false;
				if (judgeOperationCon(bo)) {
					hint(u.getIdentity());
					userOperationDetail(u, judgeChoseOperation());
				}
			}
			break;
		case 2:

			// 查阅已借阅的书籍
			if (!s.showBooks(u.getBos())) {
				System.out.println("您当前还没有已借阅的书籍,请选择其他操作");
				hint(u.getIdentity());
				userOperationDetail(u, judgeChoseOperation());
			} else {
				System.out.println("是否进行其他操作(y/n):");
				boolean bo = false;
				if (judgeOperationCon(bo)) {
					hint(u.getIdentity());
					userOperationDetail(u, judgeChoseOperation());
				}
			}
			break;
		case 3:

			// 还书
			returnBook(u, s, l);
			break;
		default:
			System.out.println("进行正确 的输入!");
			System.out.println("1、借书\t2、查看已借书籍\t3、还书");
			int rchose = judgeChoseOperation();
			userOperationDetail(u, rchose);
			break;
		}
	}

	// 还书
	private static void returnBook(Users u, Service s, Library l) {
		if (s.judegIsEmpty(u.getBos())) {
			System.out.println("已借阅的书籍:");
			s.showBooks(u.getBos());
			System.out.println("请输入要还书的编号:");
			Scanner sc = new Scanner(System.in);
			String bookid = sc.next();
			Book book = s.getBook(bookid, u.getBos());
			s.returnBook(book, l.getBooks(), u.getBos());
			System.out.println("还书成功");

			if (s.judegIsEmpty(u.getBos())) {
				System.out.println("是否继续还书(y/n):");
				boolean b = false;
				if (judgeOperationCon(b)) {
					returnBook(u, s, l);
				} else {
					System.out.println("是否进行其他操作(y/n):");
					boolean bo = false;
					if (judgeOperationCon(bo)) {
						hint(u.getIdentity());
						userOperationDetail(u, judgeChoseOperation());
					}
				}
			} else {
				System.out.println("您已没有已借阅的图书了,是否选择其他操作(y/n):");
				boolean bo = false;
				if (judgeOperationCon(bo)) {
					hint(u.getIdentity());
					userOperationDetail(u, judgeChoseOperation());
				}
			}

		} else {
			System.out.println("您目前还没有已借阅的书籍,请选择其他操作!");
			hint(u.getIdentity());
			userOperationDetail(u, judgeChoseOperation());
		}

	}

	// 判断是否继续操作
	public static boolean judgeOperationCon(boolean b) {

		String yn = "";
		Scanner sc = new Scanner(System.in);
		yn = sc.next();
		if (yn.equals("y")) {
			b = true;
		} else if (yn.equals("n")) {
			b = false;
		} else {
			System.out.println("请进行正确的输入!");
			judgeOperationCon(b);
		}
		return b;
	}

}

package com.youjiuye.bean;

/**
 * 
 * @author 丢了风筝的线 2019-12-2 图书基本类
 */
public class Book {
	private String id;
	private String bookName;
	private String author;
	private String type;

	public Book(String id) {

		this.id = id;
	}

	public Book(String id, String bookName, String type, String author) {
		this.id = id;
		this.bookName = bookName;
		this.type = type;
		this.author = author;
	}

	@Override
	public String toString() {

		return "[ 图书编号 =" + getId() + "\t 书名=" + getBookName() + "\t 作者=" + getAuthor() + "\t 类型=" + getType() + " ]";
	}

	public String getId() {
		return id;
	}

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

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}

package com.youjiuye.bean;

public class Library {
	private String name;

	// 图书馆的藏书
	private static Book[] books = new Book[20];

	// 初始化图书馆
	static {
		books[0] = new Book("001", "java", "编程类", "和老板");
		books[1] = new Book("002", "html", "编程类", "江南");
		books[2] = new Book("003", "流浪地球", "科幻类", "爪哇");
		books[3] = new Book("004", "天龙八部", "武侠类", "金庸");
		books[4] = new Book("005", "一千零一夜", "儿童类", "安徒生");
		books[5] = new Book("006", "霸道总裁", "言情类", "萌妹");
		books[6] = new Book("007", "射雕英雄传", "武侠类", "金庸");
	}

	public Library(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

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

	public Book[] getBooks() {
		return books;
	}

}

package com.youjiuye.bean;

public class Users {
	private String name;
	private String identity;
	private String password;

	// 存放借阅的图书
	private Book[] bos = new Book[10];

	public Users() {
	}

	public Users(String name, String identity) {
		this.name = name;
		this.identity = identity;
	}

	public String getName() {
		return name;
	}

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

	public String getIdentity() {
		return identity;
	}

	public void setIdentity(String identity) {
		this.identity = identity;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Book[] getBos() {
		return bos;
	}

	public void setBos(Book[] bos) {
		this.bos = bos;
	}

	@Override
	public String toString() {
		return "Users [name=" + getName() + "identity=" + getIdentity() + ", password=" + getPassword() + "]";
	}

}

你可能感兴趣的:(java,实践项目,java实现图书管理系统,图书馆里系统)