项目简介:
该项目为图书管理系统,实现用户的区分,包括管理员和普通用户,管理员的行为包括:添加图书,删除图书,查找图书,显示图书和退出程序等,普通用户的用法包括:查找图书,借阅图书,归还图书和退出程序。
思路简介:
利用类,对象,抽象类,接口,继承,封装和多态及组合等知识,将对象提取出来,对象----用户和书,用户可以分为管理员和普通用户,所以设置一个抽象类User,管理员和普通用户继承该抽象类,由于书还要包括书架,所以书和书架分两个类,为了实现对应的操作,并且是操作的方法一致,所以要构造一个操作的接口,其他的操作用类来实现该接口,就完成了对应的操作。具体实现方式见详细代码!
- package book;
-
- public class Book {
- private String name;
- private String author;
- private int price;
- private String type;
- private boolean isBorrowed;
- //构造方法
- 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 +
- '}';
- }
- }
- package book;
- public class BookList {
- public Book[] books = new Book[10];
- private int usedSize;
- //通过构造方法初始化三本书
- public BookList(){
- books[0] = new Book("三国演义","罗贯中",100,"小说");
- books[1] = new Book("西游记","吴承恩",200,"小说");
- books[2] = new Book("红楼梦","曹雪芹",300,"小说");
- this.usedSize = 3 ;
- }
-
- public int getUsedSize() {
- return usedSize;
- }
-
- public void setUsedSize(int usedSize) {
- this.usedSize = usedSize;
- }
- //通过下标 获取书
- public Book getPos(int pos){
- return books[pos];
- }
- //存书的位置
- public void setBooks(Book book,int pos){
- books[pos]=book;
- }
- }
- package operations;
-
- import book.BookList;
-
- public interface IOperation {
- void work(BookList bookList);
- }
- package operations;
- import book.Book;
- import book.BookList;
- import java.util.Scanner;
- public class AddOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("添加图书!");
- System.out.println("请输入你要添加的图书的名字:");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
-
- System.out.println("请输入你要添加的图书的作者:");
- String author=scanner.nextLine();
-
- System.out.println("请输入你要添加的图书的价格:");
- int price=scanner.nextInt();
- scanner.nextLine();//读取回车
-
- System.out.println("请输入你要添加的图书的类型:");
- String type=scanner.nextLine();
- //通过构造方法,实例化一本书
- Book book=new Book(name,author,price,type);
-
- //currentSize当前书架上书的数量
- int currentSize=bookList.getUsedSize();
-
- //把书放到下标为currentSize的位置处
- bookList.setBooks(book,currentSize);
- //书的数量加1
- bookList.setUsedSize(currentSize+1);
-
- System.out.println("添加成功!");
- }
- }
- package operations;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class BorrowedOperation 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 currentSize=bookList.getUsedSize();
- //历遍书架上的每一本书
- for (int i = 0; i < currentSize; i++) {
- Book book=bookList.getPos(i);
- if(name.equals(book.getName())){
- book.setBorrowed(true);//设置为借出状态
- System.out.println("借阅成功!");
- return;
- }
- }
- }
- }
- package operations;
- import book.Book;
- import book.BookList;
- import java.util.Scanner;
- 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 index=-1;//是否有书的标志
- //遍历数组,看是否有你要删除的图书的下标
- int currentSize=bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book=bookList.getPos(i);
- if(name.equals(book.getName())){
- index=i;
- break;
- }
- }
-
- //没有要删除的书
- if(index==-1){
- System.out.println("没有你要删除的这本书!");
- return;
- }
- //有这本书,后一项覆盖前一项
- for (int i = index; i <currentSize-1 ; i++) {
- //找到要删除的书的后一本
- Book book=bookList.getPos(i+1);
- //把这本书放到前一个位置上
- bookList.setBooks(book,i);
- }
-
- //把数组最后一项指向null
- bookList.setBooks(null,currentSize-1);
- //书架上书的数量 -1
- bookList.setUsedSize(currentSize-1);
-
- System.out.println("删除成功!");
- }
- }
- package operations;
- import book.Book;
- import book.BookList;
- public class DisplayOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("显示所有图书!");
- int currentSize=bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book=bookList.getPos(i);
- System.out.println(book);
- }
- }
- }
- package operations;
- import book.BookList;
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- System.out.println("退出程序!");
- System.exit(0);//退出
-
- }
- }
- package operations;
- import book.Book;
- import book.BookList;
- import java.util.Scanner;
-
- public class ReturnOperation 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 currentSize=bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book=bookList.getPos(i);
- if(name.equals(book.getName())){
- book.setBorrowed(false);//设置为:未被借出状态
- System.out.println("归还图书成功!");
- return;
- }
- }
- System.out.println("没有你要归还的书!");
- }
- }
- package operations;
- import book.Book;
- import book.BookList;
- import java.util.Scanner;
- public class SearchOperation 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 currentSize=bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book=bookList.getPos(i);
- if(name.equals(book.getName())){
- System.out.println("找到"+name+"了");
- System.out.println(book);
- return;
- }
- }
- System.out.println("没有这本书!");
- }
- }
- package user;
- import book.BookList;
- import operations.IOperation;
-
- //抽象类
- public abstract class User {
- protected String name;
- public User(String name){
- this.name=name;
- }
- //使用者都有选择菜单
- public abstract int menu();
-
- protected IOperation[] iOperations;//接口数组
- public void doOperation(int choice, BookList bookList){
- iOperations[choice].work(bookList);
- }
- }
- package user;
- import operations.*;
- import java.util.Scanner;
- public class AdminUser extends User {
- public AdminUser(String name) {
- super(name);
- //接口数组
- this.iOperations=new IOperation[]{
- new ExitOperation(),
- new SearchOperation(),
- new AddOperation(),
- new DelOperation(),
- new DisplayOperation()
- };
- }
- public int menu(){
- System.out.println("*********************************");
- System.out.println( "hello ! "+name+"欢迎登入图书管理系统!");
- 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;
- }
- }
- package user;
- import operations.*;
- import java.util.Scanner;
- public class NormalUser extends User {
- public NormalUser(String name) {
- super(name);
- this.iOperations=new IOperation[]{
- new ExitOperation(),
- new SearchOperation(),
- new BorrowedOperation(),
- new ReturnOperation(),
- new DisplayOperation()
- };
- }
- public int menu(){
- System.out.println("*********************************");
- System.out.println( "hello ! "+name+"欢迎登入图书管理系统!");
- System.out.println("1.查找图书");
- System.out.println("2.借阅图书");
- System.out.println("3.归还图书");
- System.out.println("4.显示图书");
- System.out.println("0.退出系统");
- System.out.println("*********************************");
-
- Scanner scanner=new Scanner(System.in);
- int choice=scanner.nextInt();
- return choice;
- }
- }
- import book.BookList;
- import user.AdminUser;
- import user.NormalUser;
- import user.User;
- import java.util.Scanner;
-
- public class Main {
- //登入
- public static User login(){
- System.out.println("请输入你的姓名:");
- Scanner scanner=new Scanner(System.in);
- String userName=scanner.nextLine();
-
- System.out.println("请输入你的身份:1-管理员,0-普通用户");
- int choice= scanner.nextInt();
- if(choice==1){
- return new AdminUser(userName);
- }else{
- return new NormalUser(userName);
- }
-
- }
- public static void main(String[] args) {
- //1、准备数据
- BookList bookList=new BookList();
- //2、登录,
- User user= login();//向上转型
-
- while(true){
- int choice = user.menu();//多态,动态绑定
- user.doOperation(choice,bookList);
- }
- }
- }


