赞
踩
图书管理系统是对抽象类、接口、继承、封装等知识点的应用,也是对学习的知识的总结。
对于这个图书管理系统我们大致分为三个步骤进行编写。
(源码我放在最后)
我们先先创建一个包book
1.1在包中我们先创建Book类来表示一本书
- package book;
-
- public class Book {
- private String name;
- private String author;
- private int price;
- private String type;
- private boolean isBorrowed =false;
-
- public Book(String name, String author, int price, String type) {
- this.name = name;
- this.author = author;
- this.price = price;
- this.type = type;
- }
-
- public String getName() {return name;}
- public void setName(String name) {this.name = name;}
- public String getAuthor() {return author;}
- public void setAuthor(String author) {this.author = author;}
- public int getPrice() {return price;}
- public void setPrice(int price) {this.price = price;}
- public String getType() {return type;}
- public void setType(String type) {this.type = type;}
- public boolean isBorrowed() {return isBorrowed;}
- public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- ", type='" + type + '\'' +
- ", isBorrowed=" + isBorrowed +
- '}';
- }
- }
在这个类当中由于我们对于一本书中的类容进行了编写,由于为都为私有,我们设置了它的get和set方法,同时我们也重写了toString方法,这样一本书就编写好了。
1.2创建BookList类用来保存N本书
- package book;
-
- public class BookList {
- private Book[] books=new Book[10];
- private int size=0;
- public BookList(){
- books[0] = new Book("三国演义", "罗贯中", 100, "小说");
- books[1] = new Book("水浒传", "施耐庵", 100, "小说");
- books[2] = new Book("西游记", "吴承恩", 100, "小说");
- this.size=3;
- }
-
- public Book[] getBooks() {return books;}
- public void setBooks(Book[] books) {this.books = books;}
- public int getSize() {return size;}
- public void setSize(int size) {this.size = size;}
- }
在初始化的时候我们定义三本书放进去,并将它4们的get和set方法设置出来。
我们将相关类都放在operation包中
我们先创建一个接口
- package operation;
-
- import book.BookList;
-
- public interface IOperation {
- void work(BookList bookList);
- }
我们在接口中添加了一个方法,去遍历很多本书,通过继承让其他类继承这个接口,从而实现work方法。
- public class AddOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- }
- }
- public class DelOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- }
- }
- public class FindOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- }
- }
- public class RemoveOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- }
- }
- public class DisplayOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- }
- }
- public class BorrowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- }
- }
- public class ReturnOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- }
- }
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- }
- }
我们创建了不同的方法通过调用接口来实现方法。
创建一个user包
- package uesr;
-
- import operation.IOperation;
-
- public abstract class User {
- protected String name;
- protected IOperation[] operations;
-
- //显示菜单
- abstract public int menu();
- }
创建普通用户类
- package uesr;
-
- import operation.*;
-
- import java.util.Scanner;
-
- public class NormalUser extends User{
- public NormalUser(String name){
- this.name=name;
- this.operations=new IOperation[]{
- new ExitOperation(),
- new FindOperation(),
- new BorrowOperation(),
- new ReturnOperation()
- };
- }
-
- @Override
- public int menu() {
- System.out.println("=============");
- System.out.println("欢迎用户使用图书系统");
- System.out.println("1、查找图书");
- System.out.println("2、借阅图书");
- System.out.println("3、归还图书");
- System.out.println("0、退出系统");
- System.out.println("=============");
- System.out.println("输入你的选择");
- Scanner scanner=new Scanner(System.in);
- int choice=scanner.nextInt();
- return choice;
- }
- }
创建管理员类
- package uesr;
-
- import operation.*;
-
- import java.util.Scanner;
-
- public class AdminUser extends User{
- public AdminUser(String name){
- this.name=name;
- this.operations=new IOperation[]{
- new ExitOperation(),
- new FindOperation(),
- new AddOperation(),
- new DelOperation(),
- new DisplayOperation()
- };
- }
- @Override
- public int menu() {
- System.out.println("=============");
- System.out.println("欢迎管理员使用图书系统");
- System.out.println("1、查找图书");
- System.out.println("2、新增图书");
- System.out.println("3、删除图书");
- System.out.println("4、显示所有图书");
- System.out.println("0、退出系统");
- System.out.println("=============");
- System.out.println("输入你的选择");
- Scanner scanner=new Scanner(System.in);
- int choice=scanner.nextInt();
- return choice;
- }
- }
- import book.BookList;
- import uesr.AdminUser;
- import uesr.NormalUser;
- import uesr.User;
-
- import java.util.Scanner;
-
- public class Main {
- public static User login(){
- Scanner scanner=new Scanner(System.in);
- System.out.println("请输入姓名");
- String name=scanner.next();
- System.out.println("请输入你的身份(1、管理员 2、普通用户)");
- int who=scanner.nextInt();
- if(who==1){
- return new AdminUser(name);
- }else{
- return new NormalUser(name);
- }
- }
-
- public static void main(String[] args) {
- BookList bookList=new BookList();
-
- User user=login();
-
- int choice=user.menu();
- user.doOperation(choice,bookList);
- }
- }
对于主函数的整合,我们想要将他们结合起来我们要在User中添加doOperation方法:
- public void doOperation(int choice, BookList bookList){
- this.operations[choice].work(bookList);
- }
通过调用方法来进行对于类的整合。
最后我们加入while循环使代码循环起来
- while (true) {
- int choice = user.menu();
- user.doOperation(choice, bookList);
- }
实现:
5.1 AddOperation(添加)实现类
- package operation;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class AddOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("新曾图书!");
- Scanner scanner=new Scanner(System.in);
- System.out.println("请输入书的名称:");
- String name=scanner.nextLine();
- System.out.println("请输入书的作者:");
- String author=scanner.nextLine();
- System.out.println("请输入书的类型:");
- String type=scanner.nextLine();
- System.out.println("请输入书的价格:");
- int price=scanner.nextInt();
-
- Book book=new Book(name,author,price,type);
- int size=bookList.getSize();
- for (int i = 0; i < size; i++) {
- Book book1=bookList.getBook(i);
- if(book.getName().equals(book1.getName())){
- System.out.println("书名已经重复");
- return;
- }
- }
-
- bookList.setBook(size,book);
- bookList.setSize(size+1);
- System.out.println("新增图书成功!!!!");
-
- }
- }
在使用时我们要在BookList中加入两个方法
- public Book getBook(int size){
- return books[size];
- }
- public void setBook(int pos,Book book){
- books[pos]=book;
- }
5.2 DisplayOperation(打印)类的实现
- public class DisplayOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- int size=bookList.getSize();
- for (int i = 0; i < size; i++) {
- Book book1=bookList.getBook(i);
- System.out.println(book1);
- }
- }
- }
要注意:不能直接写System.out.println(bookList[i]);
5.3 FindOperation(查找)类的实现
- public class FindOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("查找图书!!");
- System.out.println("请输入图书的名称:");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
- int size=bookList.getSize();
- for (int i = 0; i < size; i++) {
- Book book1=bookList.getBook(i);
- if(book1.getName().equals(name)){
- System.out.println("找到了!!!");
- System.out.println(book1);
- return;
- }
- }
- System.out.println("没有找到!!!");
- }
- }
5.4 ExitOperation(退出)类的实现
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("退出成功!");
- System.exit(0);
- }
- }
5.5 DelOperation(删除)类的实现
- public class DelOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("删除图书!!");
- System.out.println("请输入删除图书的名称:");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
- int size=bookList.getSize();
- int pos=-1;
- for (int i = 0; i < size; i++) {
- Book book1=bookList.getBook(i);
- if(book1.getName().equals(name)){
- pos=i;
- break;
- }
- }
- if(pos!=-1){
- for (int i = pos; i <size-1 ; i++) {
- Book book=bookList.getBook(i+1);
- bookList.setBook(i,book);
- }
- bookList.setSize(size-1);
- System.out.println("删除成功!!!");
- return;
- }
- System.out.println("删除失败!!!!");
- }
- }
5.6 BorrowOperation(借阅)类的实现
- public class BorrowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("借阅图书!!");
- System.out.println("请输入借阅图书的名称:");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
- int size=bookList.getSize();
- for (int i = 0; i < size; i++) {
- Book book1=bookList.getBook(i);
- if(book1.getName().equals(name)){
- book1.setBorrowed(true);
- System.out.println("借阅成功!!!!");
- return;
- }
- }
- System.out.println("借阅失败!!!");
- }
- }
5.7 ReturnOperation(归还)类的实现
- public class RemoveOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("归还图书!!");
- System.out.println("请输入归还图书的名称:");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
- int size=bookList.getSize();
- for (int i = 0; i < size; i++) {
- Book book1=bookList.getBook(i);
- if(book1.getName().equals(name)){
- book1.setBorrowed(false);
- System.out.println("归还成功!!!!");
- return;
- }
- }
- System.out.println("归还失败!!!");
- }
- }
这样简单的图书管理系统就做完成了,下面是代码:图书管理系统。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。