前言:
前面小怡给大家分享了抽象类和接口,今天小怡给大家分享用Java实现图书管理系统。
1.功能
不同的用户看到的菜单是不一样的,我们分为两个用户身份,管理员和普通用户。
2.知识点
数据类型、变量、数组、方法、类和对象、继承和多态、封装、抽象类和接口。今天要把上述知识点全部用起来。
3. 实现框架
首先我们要建立一个新的文件 ,在文件内建立三个包,分别命名为book(书籍)、user(使用者)、ioperation(操作)。
3.1 book包
因为是图书管理系统,我们先写和图书相关的内容。book包里有三个类,分别是Book(图书)、BookList(书架)、Main(主功能)。
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 +
'}';
}
}
BookList类:
public class BookList {
private Book[] books=new Book[10];
private int usedSize;//有效的数据个数【实际放的书的个数】
public BookList(){
books[0]=new Book("三国演义","罗贯中",30,"小说");
books[1]=new Book("西游记","吴承恩",40,"小说");
books[2]=new Book("红楼梦","曹雪芹",50,"小说");
this.usedSize=3;
}
}
Main类(这个得在其他类写好了才来写):
public class Main {
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 Librarian(name);
}else if(choice==2){
return new Reader(name);
}else{
return null;
}
}
public static void main(String[] args) {
BookList bookList=new BookList();
User user=login();
user.menu();
int choice=user.menu();
user.doIoperation(choice,bookList);
}
}
3.2 user包
user包里有三个类,分别是:Librarian(图书管理员)、Reader(读者)、User(使用者)。
User类:
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doIoperation(int choice, BookList bookList){
ioPerations[choice].work(bookList);
}
}
Librarian类:
public class Librarian extends User{
public Librarian(String name) {
super(name);
this.ioPerations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation(),
};
}
public int menu(){
System.out.println("欢迎"+name+"来到图书管理系统");
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;
}
}
Reader类:
public class Reader extends User{
public Reader(String name) {
super(name);
this.ioPerations=new IOPeration[]{new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
public int menu(){
System.out.println("欢迎"+name+"来到图书管理系统");
System.out.println("*******读者菜单*******");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("*********************");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入您的操作: ");
int choice=scanner.nextInt();
return choice;
}
}
3.3 ioperation 包
现在书的属性和用户的属性的包和类以及完成差不多,由于想要实现的功能和操作基本上是针对图书的,我们可以创一个包ioperation,还有命名一个接口IOPeration。 里卖弄有七个类,一个接口。
AddOperation类(新增图书):
public class AddOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("新增图书……");
}
}
BorrowOperation类(借阅图书):
public class BorrowOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("借阅图书……");
}
}
DelOperation类(删除图书):
public class DelOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("删除图书……");
}
}
ExitOperation类(退出系统):
public class ExitOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("退出系统……");
}
}
FindOperation类(查找图书):
public class FindOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("查找图书……");
}
}
IOPeration接口:
public interface IOPeration {
void work(BookList bookList);
}
ReturnOperation类(归还图书):
public class ReturnOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("归还图书……");
}
}
ShowOperation类(显示图书):
public class ShowOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("显示图书……");
}
}
4.实现功能
4.1 退出系统
退出系统比较容易实现
System.exit(0);
4.2 显示图书
显示图书其实就需要遍历数组,我们使用一个for循环就行。
int currentSize=bookList.getUsedSize();
for(int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
System.out.println(book);
}
在BookList里面也要加上get和set。
public Book getBooks(int pos) {
return 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;
}
4.3 查找图书
这里用到了for循环和equals
Scanner scanner=new Scanner(System.in);
System.out.printf("请输入您的书名: ");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书");
System.out.println(book);
return;
}
}
System.out.println("没有你要找的这本书");
4.4 新增图书
要先输入各种成员变量构建对象,再判断数组满没满,然后放到数组的最后一个位置,used++。
public void work(BookList bookList){
System.out.println("新增图书……");
int currentSize=bookList.getUsedSize();
if(currentSize==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("请输入价格: ");
int price=scanner.nextInt();
System.out.println("请输入书的类型: ");
String type=scanner.nextLine();
Book newbook=new Book(name,author,price,type);
for (int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("有这本书,不能增加");
System.out.println(book);
return;
}
}
bookList.setBooks(currentSize,newbook);
bookList.setUsedSize(currentSize+1);
System.out.println("新增图书成功");
}
4.5 借阅图书
借阅图书和查找图书有很大一部分是相似的。
int currentSize=bookList.getUsedSize();
for (int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
if (book.isBorrowed()==true){
System.out.println("这本书已经被借出了");
return;
}
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有你要借阅的这本书");
4.6 归还图书
借阅和归还的整体逻辑是修改状态。
System.out.println("归还图书……");
Scanner scanner=new Scanner(System.in);
System.out.printf("请输入您归还的书名: ");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
if (book.isBorrowed()==true){
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
}
System.out.println("没有你要归还的图书");
4.7 删除图书
Scanner scanner=new Scanner(System.in);
System.out.print("请输入您删除的书名: ");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
int pos=-1;
int i=0;
for (;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书");
pos=i;
break;
}
}
if (i==currentSize){
System.out.println("没有你要删除的书");
return;
}
//开始删除
for (int j=pos;j<currentSize-1;j++){
Book book=bookList.getBooks(j+1);
bookList.setBooks(j,book);
}
bookList.setUsedSize(currentSize-1);
5.运行案例
6.源代码
Book类:
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==true) ?"已借出":"未借出")+
'}';
}
}
BookList类:
package book;
public class BookList {
private Book[] books=new Book[10];
private int usedSize;//有效的数据个数【实际放的书的个数】
public BookList(){
books[0]=new Book("三国演义","罗贯中",30,"小说");
books[1]=new Book("西游记","吴承恩",40,"小说");
books[2]=new Book("红楼梦","曹雪芹",50,"小说");
this.usedSize=3;
}
public Book getBooks(int pos) {
return 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;
}
public Book[] getBooks(){
return books;
}
}
Main类:
package book;
import ioperation.IOPeration;
import user.Librarian;
import user.Reader;
import user.User;
import java.util.Scanner;
public class Main {
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 Librarian(name);
}else if(choice==2){
return new Reader(name);
}else{
return null;
}
}
public static void main(String[] args) {
BookList bookList=new BookList();
User user=login();
user.menu();
int choice=user.menu();
user.doIoperation(choice,bookList);
}
}
AddOperation类:
package ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("新增图书……");
int currentSize=bookList.getUsedSize();
if(currentSize==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 newbook=new Book(name,author,price,type);
for (int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("有这本书,不能增加");
System.out.println(book);
return;
}
}
bookList.setBooks(currentSize,newbook);
bookList.setUsedSize(currentSize+1);
System.out.println("新增图书成功");
}
}
BorrowOperation类:
package ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("借阅图书……");
Scanner scanner=new Scanner(System.in);
System.out.printf("请输入您借阅的书名: ");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
if (book.isBorrowed()==true){
System.out.println("这本书已经被借出了");
return;
}
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有你要借阅的这本书");
}
}
DelOperation类:
package ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("删除图书……");
Scanner scanner=new Scanner(System.in);
System.out.print("请输入您删除的书名: ");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
int pos=-1;
int i=0;
for (;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书");
pos=i;
break;
}
}
if (i==currentSize){
System.out.println("没有你要删除的书");
return;
}
//开始删除
for (int j=pos;j<currentSize-1;j++){
Book book=bookList.getBooks(j+1);
bookList.setBooks(j,book);
}
bookList.setUsedSize(currentSize-1);
}
}
ExitOperation类:
package ioperation;
import book.BookList;
public class ExitOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("退出系统……");
System.exit(0);
}
}
FindOperation类:
package ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("查找图书……");
Scanner scanner=new Scanner(System.in);
System.out.printf("请输入您的书名: ");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书");
System.out.println(book);
return;
}
}
System.out.println("没有你要找的这本书");
}
}
IOPeration接口:
package ioperation;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);
}
ReturnOperation类:
package ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("归还图书……");
Scanner scanner=new Scanner(System.in);
System.out.printf("请输入您归还的书名: ");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
if (book.isBorrowed()==true){
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
}
System.out.println("没有你要归还的图书");
}
}
ShowOperation类:
package ioperation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("显示图书……");
int currentSize=bookList.getUsedSize();
for(int i=0;i<currentSize;i++){
Book book=bookList.getBooks(i);
System.out.println(book);
}
}
}
Librarian类:
package user;
import ioperation.*;
import java.util.Scanner;
public class Librarian extends User{
public Librarian(String name) {
super(name);
this.ioPerations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation(),
};
}
public int menu(){
System.out.println("欢迎"+name+"来到图书管理系统");
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;
}
}
Reader类:
package user;
import ioperation.*;
import java.util.Scanner;
public class Reader extends User{
public Reader(String name) {
super(name);
this.ioPerations=new IOPeration[]{new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
public int menu(){
System.out.println("欢迎"+name+"来到图书管理系统");
System.out.println("*******读者菜单*******");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("*********************");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入您的操作: ");
int choice=scanner.nextInt();
return choice;
}
}
User类:
package user;
import book.BookList;
import ioperation.IOPeration;
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doIoperation(int choice, BookList bookList){
ioPerations[choice].work(bookList);
}
}
今天的分享到这里结束啦,小怡和大家一起进步一起学习。“没有人不喜欢聪慧而努力的人,所以,所有的老板都会青睐酸梅汤型员工。想当这样的员工不容易,重在时间的积累。快捷得不到成绩,只有在历练中才能收获一种有价值的甜”。
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 小怡分享之Java图书管理系统
发表评论 取消回复