赞
踩
目录
该图书管理系统充分展示面向对象思想,是类与对象,封装,继承,多态,抽象类,接口等知识的融合体现。
若有不了解的内容,可以查看本博主的曾写的相关内容。
面向对象程序三大特性一:封装(超详细)_WHabcwu的博客-CSDN博客
面向对象程序三大特性一:多态(超详细)_WHabcwu的博客-CSDN博客
面向对象程序三大特性一:多态(超详细)_WHabcwu的博客-CSDN博客
需求:登录的用户分为普通用户和管理员,实现界面目录的需求(均用姓名索引)。
普通用户登录后界面:
管理员用户登录后界面:
图书管理系统已有图书如下:
面向对象思想解决问题主要分为这3步:
- 找出其中的所有的对象
- 实现所有的对象
- 完成对象之间的交互
在图书管理系统中我们可以想到这几个对象:书,用户(普通用户和管理员),存放书的书架。
操作太多这里推荐新建将所有的操作都单独封装成一个类。
分为三个包进行实现:
book包里面实现书类和书架类
operation包里实现我们的操作类
user包里实现普通用户类与管理员类
其中所有的操作都实现名为IOperation的接口。便于统一管理(多态的体现)
普通用户和管理员都继承用户User这个父类。
实现所有类如下:
在这个类实现的是我们图书该有的属性,与相应的构造方法,以及为了方便我们打印而重写的toString方法
- 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 void setBorrowed(boolean borrowed) {
- isBorrowed = borrowed;
- }
-
- @Override
- public String toString() {
- return "{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- ", type='" + type + '\'' +
- ", "+(isBorrowed==false?"未被借出":"已被借出")+
- '}';
- }
- }
我们需要存放很多Book的数组,操作的数组,加入图书管理系统已有图书。
- public class BookList {
- private Book books[]=new Book[100];
- private int useSize;
-
- public BookList() {
- this.books[0] = new Book("三国演义","罗贯中",10,"小说");
- this.books[1] = new Book("西游记","吴承恩",9,"小说");
- this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
- this.useSize = 3;
- }
-
- public int getUseSize() {
- return useSize;
- }
-
- public void setUseSize(int useSize) {
- this.useSize = useSize;
- }
-
- public Book getbook(int pose){
- return books[pose];
-
- }
- public void setbook(int pose,Book book){
- books[pose]=book;
- }
-
- public Book[] getBooks() {
- return books;
- }
- }
这里的User类是作为NormalUser类与AdminUser类的父类,是一个抽象类。
- package user;
-
- import book.BookList;
- import operation.IOperation;
-
- public abstract class User {
- protected String name;
- IOperation iOperations[];
- public User(String name) {
- this.name = name;
- }
- public abstract int menu();
- public void doOperation(int choice, BookList bookList){
- iOperations[choice].work(bookList);
- }
-
- }
- package user;
-
- import operation.*;
-
- import java.util.Scanner;
-
- public class NormalUser extends User {
- public NormalUser(String name) {
- super(name);
- this.iOperations = new IOperation[]{new ExitOperation(), new FindOperation(),
- new BorrowOperation(), new ReturnOperation(),new ShowOperation()};
- }
-
- @Override
- public int menu() {
-
- 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;
- }
-
-
- }
- package user;
-
- import operation.*;
-
- import java.util.Scanner;
-
- public class AdminUser extends User{
- public AdminUser(String name) {
- super(name);
- this.iOperations=new IOperation[]{new ExitOperation(),new FindOperation()
- ,new AddOperation(), new DelOperation(),new ShowOperation()};
- }
- public int menu(){
- 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("**************************");
- Scanner scanner =new Scanner(System.in);
- System.out.println("请输入你的操作");
- int choice=scanner.nextInt();
- return choice;
- }
- }
会有一个work的抽象方法,参数类型为BookList的
- package operation;
-
- import book.BookList;
-
- public interface IOperation {
- void work(BookList bookList);
- }
用于退出该系统
- package operation;
-
- import book.BookList;
-
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("退出系统");
- }
- }
用于查找图书
- package operation;
- import book.Book;
- import book.BookList;
- import java.util.Scanner;
-
- public class FindOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- int size=bookList.getUseSize();
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入你要查询的书名");
- String name=scanner.nextLine();
- for (int i = 0; i < size; i++) {
- Book book = bookList.getbook(i);
- if(book.getName().equals(name)){
- System.out.println("找到这本书,信息如下");
- System.out.println(book);
- return;
- }
- }
- System.out.println("查无此书");
- }
- }
用于展示所有图书
- package operation;
-
- import book.Book;
- import book.BookList;
-
- public class ShowOperation implements IOperation {
- @Override
- public void work(BookList bookList) {
- int size=bookList.getUseSize();
- for (int i = 0; i < size; i++) {
- Book book = bookList.getbook(i);
- System.out.println(book);
- }
- }
- }
用于添加图书
- package operation;
- import book.Book;
- import book.BookList;
- import java.util.Scanner;
- public class AddOperation implements IOperation {
- @Override
- public void work(BookList bookList) {
- int size=bookList.getUseSize();
- if(size==bookList.getBooks().length){
- System.out.println("书架已满,无法添加图书");
- return;
- }
- 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);
- for (int i = 0; i < size; i++) {
- if(bookList.getbook(i).getName().equals(name)){
- System.out.println("该书已有,不再存入");
- return;
- }
- }
- bookList.setbook(size,book);
- bookList.setUseSize(size+1);
- }
-
- }
用于删除图书
- package operation;
-
- import book.BookList;
- import java.util.Scanner;
-
- public class DelOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- Scanner scanner=new Scanner(System.in);
- System.out.println("请输入要删除书的书名");
- String name=scanner.nextLine();
- int size = bookList.getUseSize();
- int i=0;
- for (i = 0; i < size; i++) {
- if(bookList.getbook(i).getName().equals(name)){
- break;
- }
- }
- if(i==size){
- System.out.println("你要删除的书不存在");
- return;
- }
- int j=0;
- for ( j = i; j <size-1; j++) {
- bookList.setbook(j,bookList.getbook(j+1));
- }
- bookList.setbook(j,null);
- bookList.setUseSize(size-1);
- System.out.println("删除成功");
- }
- }
- package operation;
-
- import book.BookList;
-
- import java.util.Scanner;
-
- public class BorrowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- Scanner scanner=new Scanner(System.in);
- System.out.println("请输入要借阅书的书名");
- int size=bookList.getUseSize();
- String name=scanner.nextLine();
- for (int i = 0; i < size; i++) {
- if(bookList.getbook(i).getName().equals(name)){
- bookList.getbook(i).setBorrowed(true);
- System.out.println("成功借出");
- return;
- }
- }
- System.out.println("你要借的书不存在");
- }
- }
- package operation;
-
- import book.BookList;
-
- import java.util.Scanner;
-
- public class ReturnOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- Scanner scanner=new Scanner(System.in);
- System.out.println("请输入要还的书的书名");
- int size=bookList.getUseSize();
- String name=scanner.nextLine();
- for (int i = 0; i < size; i++) {
- if(bookList.getbook(i).getName().equals(name)){
- bookList.getbook(i).setBorrowed(false);
- System.out.println("成功归还");
- return;
- }
- }
- System.out.println("你要还的书不存在");
- }
- }
- import java.util.Scanner;
-
- import book.BookList;
- import user.AdminUser;
- import user.NormalUser;
- import user.User;
-
- public class Text {
- public static User login(){
- Scanner scanner=new Scanner(System.in);
- System.out.println("请输入你的姓名");
- String name=scanner.nextLine();
- System.out.println("请输入你的身份:> 1.管理员 2.普通用户");
- int choice=scanner.nextInt();
- if(choice==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=-1;
- do{
- choice=user.menu();
- user.doOperation(choice,bookList);
- }while (choice!=0);
- }
- }
以上为我个人的小分享,如有问题,欢迎讨论!!!
都看到这了,不如关注一下,给个免费的赞
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。