Java——面向对象练习(图书管理系统的实现)
Posted rain67
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java——面向对象练习(图书管理系统的实现)相关的知识,希望对你有一定的参考价值。
文章目录
Java——面向对象练习(图书管理系统的实现)
在前面一段时间里,学习了 Java 面向对象的相关内容:包的使用、继承、多态、抽象类、接口等知识,现在为了巩固我们所学习的知识,接下来我们将从零到有实现一个简单版的图书管理系统的代码实现。
一、实现效果展示
(1)功能简介
(2)登陆界面
(3)菜单界面
(4)功能展示
1.显示图书
2.新增图书
3.删除图书
4.查找图书
5.借阅图书
6.归还图书
二、具体代码实现
1.类的设计
我们通过这个管理系统的功能简介等,可以知道我们需要设计的几个类
(1)创建图书相关的类
先创建 package book
创建 Book 类, 表示一本书
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
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;
}
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\\'' +
", author='" + author + '\\'' +
", price=" + price +
", type='" + type + '\\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
这个书的类中,有书的各种属性:书名、作者、价格、类别、借阅情况,同时将这些属性都封装起来。
创建 BookList 类, 用来保存 N 本书.
package book;
public class BookList {
private Book[] books = new Book[100];
private int usedSize;
public BookList(){
//默认有四本书
books[0] = new Book("西游记","吴承恩",10,"小说" );
books[1] = new Book("水浒传","施耐庵",20,"小说" );
books[2] = new Book("红楼梦","曹雪芹",10,"小说" );
books[3] = new Book("三国演义","罗贯中",10,"小说" );
this.usedSize = 4;
}
public Book getBooks(int pos) {
return this.books[pos];
}
public void setBooks(int pos,Book book) {
//默认放到顺序表的最后
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
用顺序表的方式存储书籍,且在书架中默认有四本书,设置一个 现有书籍数量 usedSize的属性,在构造方法中存储四本书,usedSize = 4.
(2) 创建操作相关的类
先创建 package operation
接下来创建一组操作类, 每个类对应一个用户的动作.
抽象出 Operation 的好处: 让操作和操作之间低耦合, 让操作和用户之间低耦合.
1.接口的实现
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
2.新增书籍
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList){
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();
for (int i = 0; i <bookList.getUsedSize() ; i++) {
Book book = bookList.getBooks(i);
if(name.equals(book.getName())){
System.out.println("书架存在这本书,不能重复添加!");
return;
}
}
int currentSize = bookList.getUsedSize();
Book book = new Book(name,author,price,type);
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
System.out.println("增加成功!");
}
}
3.删除书籍
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除书籍! ");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的图书的名字:");
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int index = -1;
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if(name.equals(book.getName())) {
//找到了这本书
index = i;
}
}
if(index == -1) {
System.out.println("没有你要删除的书!");
return;
}
for (int i = index; i < currentSize-1; i++) {
Book book1 = bookList.getBooks(i+1);
bookList.setBooks(i,book1);
//bookList[i] = bookList[i+1];
}
//防止内存泄漏
bookList.setBooks(currentSize-1,null);
//控制usedSize
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!");
}
}
4.展示书籍
package operation;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
for (int i = 0; i <bookList.getUsedSize() ; i++) {
Book book = bookList.getBooks(i);
System.out.println(book);
}
}
}
5.查找书籍
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList booklist){
System.out.println("请输入要查找的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i <booklist.getUsedSize() ; i++) {
Book book = booklist.getBooks(i);
if(name.equals(book.getName())){
System.out.println("找到此书入下:");
System.out.println(book);
return ;
}
}
System.out.println("没有这本书!");
}
}
6.借阅书籍
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
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();
for (int i = 0; i <bookList.getUsedSize(); i++) {
Book book = bookList.getBooks(i);
if(name.equals(book.getName()) && book.isBorrowed()==false) {
//找到了就借阅
book.setBorrowed(true);
return ;
}
}
System.out.println("借阅失败,没有此书 ,或此书已被借出");
}
}
7.归还书籍
package operation;
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();
for (int i = 0; i <bookList.getUsedSize(); i++) {
Book book = bookList.getBooks(i);
if(name.equals(book.getName())) {
//找到了就归还
book.setBorrowed(false);
System.out.println("归还成功!");
return ;
}
}
System.out.println("归还失败,没有此书!");
}
}
8.退出系统
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}
(3)创建用户相关的类
1.父类 用户 user 的实现
package users;
import book.BookList;
import operation.*;
public abstract class User {
protected String name;
protected IOperation[] iOperations ;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
2.管理员用户的实现
package users;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
Java开发 | 图书管理系统 | 小程序 | 小练习