目录
看了别人的案例,我也写了一个自己的图书管理系统,熟悉了一下继承,接口,向上转型,动态绑定的知识,这么规划的优点呢毋庸置疑的:高度模块化,充分解耦合,高内聚等等...,话不多说,直接贴代码
感兴趣也可以访问我的gitee:图书管理系统
1.User
1.抽象类
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/13 0:19
*/
public abstract class User {
private String name;
public User(String name) {
this.name = name;
}
public User(){
}
public abstract void menu();
public abstract void IOperation(int n,ListBook listBook);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.管理员
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/13 0:26
*/
public class Admin extends User{
public Admin(String name){
super(name);
}
@Override
public void menu() {
System.out.println("尊敬的管理员:"+this.getName()+"欢迎来到图书管理系统!");
System.out.println("1.新增图书");
System.out.println("2.删除图书");
System.out.println("3.修改图书");
System.out.println("4.图书列表");
System.out.println("5.查找图书");
System.out.println("0.退出系统");
}
@Override
public void IOperation(int n,ListBook listBook) {
IOperation[] iOperations=new IOperation[6];
iOperations[0]=new ExitSystem();
iOperations[1]=new InsertBook();
iOperations[2]=new DeleteBook();
iOperations[3]=new AmendBook();
iOperations[4]=new TraverseTheList();
iOperations[5]=new FindBook();
if(n<0||n>=iOperations.length){
System.out.println("选择有误!");
return;
}
iOperations[n].work(listBook);
}
}
3.普通用户
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/13 0:43
*/
public class Student extends User{
public Student(String name){
super(name);
}
@Override
public void menu() {
System.out.println("尊敬的"+this.getName()+"欢迎来到图书馆");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("4.图书列表");
System.out.println("0.退出系统");
}
@Override
public void IOperation(int n, ListBook listBook) {
IOperation[] iOperations=new IOperation[6];
iOperations[0]=new ExitSystem();
iOperations[1]=new FindBook();
iOperations[2]=new BorrowBook();
iOperations[3]=new BookReturn();
iOperations[4]=new TraverseTheList();
if(n<0||n>=iOperations.length){
System.out.println("选择有误!");
return;
}
iOperations[n].work(listBook);
}
}
2.操作类
1.接口类
public interface IOperation {
void work(ListBook listBook);
}
2.方法类
1.增加图书
import java.util.Scanner;
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/12 23:21
*/
public class InsertBook implements IOperation {
@Override
public void work(ListBook listBook) {
Scanner scanner=new Scanner(System.in);
System.out.println("新增图书:");
System.out.println("请输入书名:");
String name=scanner.next();
System.out.println("请输入作者:");
String author=scanner.next();
System.out.println("请输入类型:");
String type=scanner.next();
System.out.println("请输入价格:");
double price=scanner.nextDouble();
Book book=new Book(name,author,type,price);
listBook.add(book);
System.out.println("新增成功!");
}
}
2.删除图书
import java.util.Scanner;
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/12 23:36
*/
public class DeleteBook implements IOperation{
@Override
public void work(ListBook listBook) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要删除的图书名:");
String name=scanner.next();
for (int i = 0; i < listBook.size(); i++) {
Book book=listBook.get(i);
if(book.getName().equals(name)){
System.out.println("找到了!");
System.out.println(book);
System.out.println("是否删除? [y/n]");
if(scanner.next().equals("y")){
if(listBook.remove(i)!=null){
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
return;
}
}
System.out.println("没找到这本书!");
}
}
3.查找图书
import java.util.Scanner;
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/12 23:46
*/
public class FindBook implements IOperation{
@Override
public void work(ListBook listBook) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要查找的书名:");
String name=scanner.next();
for (int i = 0; i < listBook.size(); i++) {
Book book=listBook.get(i);
if(book.getName().equals(name)){
System.out.println("找到啦!");
System.out.println(book);
return;
}
}
System.out.println("没找到!");
}
}
4.修改图书信息
import java.util.Scanner;
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/13 0:00
*/
public class AmendBook implements IOperation{
@Override
public void work(ListBook listBook) {
System.out.println("修改图书信息:");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要修改的书名:");
String name=scanner.next();
for (int i = 0; i < listBook.size(); i++) {
Book book=listBook.get(i);
if(name.equals(book.getName())){
System.out.println("请输入书名:");
name=scanner.next();
System.out.println("请输入作者:");
String author=scanner.next();
System.out.println("请输入类型:");
String type=scanner.next();
System.out.println("请输入价格:");
double price=scanner.nextDouble();
book=new Book(name,author,type,price);
listBook.set(i,book);
System.out.println("修改成功!");
return;
}
}
System.out.println("没找到这本书");
}
}
5.图书列表
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/12 23:54
*/
public class TraverseTheList implements IOperation{
@Override
public void work(ListBook listBook) {
for (int i = 0; i < listBook.size(); i++) {
Book book=listBook.get(i);
System.out.println(book);
}
System.out.println("共"+listBook.size()+"本书");
}
}
6.借阅图书
import java.util.Scanner;
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/13 0:07
*/
public class BorrowBook implements IOperation{
@Override
public void work(ListBook listBook) {
Scanner scanner=new Scanner(System.in);
System.out.println("借书:");
System.out.println("请输入你要借的书名:");
String name=scanner.next();
for (int i = 0; i < listBook.size(); i++) {
Book book=listBook.get(i);
if(name.equals(book.getName())){
if(book.isStatus()){
System.out.println("这本书已经被借出去了!");
}else {
book.setStatus(true);
System.out.println("借书成功!");
}
return;
}
}
System.out.println("没有这本书!");
}
}
7.归还图书
import java.util.Scanner;
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/13 0:14
*/
public class BookReturn implements IOperation{
@Override
public void work(ListBook listBook) {
Scanner scanner=new Scanner(System.in);
System.out.println("还书:");
System.out.println("请输入你要还的书名:");
String name=scanner.next();
for (int i = 0; i < listBook.size(); i++) {
Book book=listBook.get(i);
if(name.equals(book.getName())){
if(book.isStatus()){
book.setStatus(false);
System.out.println("还书成功!");
}else {
System.out.println("该书没有被借出!");
}
return;
}
}
System.out.println("没有这本书!");
}
}
8.退出系统
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/13 0:23
*/
public class ExitSystem implements IOperation{
@Override
public void work(ListBook listBook) {
System.out.println("退出成功!");
System.exit(0);
}
}
3.封装图书管理
1.图书属性
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/12 21:52
*/
public class Book {
private String name;
private String author;
private String type;
private double price;
private boolean status;
public Book() {
}
public Book(String name, String author, String type, double price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
@Override
public String toString() {
return "Book{" + name + " " + author + " " + type + " " + price + " " + (status ? "已借出" : "未借出") + "}";
}
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
2.图书管理
错字懒得改了,就这样吧,反正也不影响代码,嘿嘿
import java.util.ArrayList;
/**
* @description:
* @User :蔡如学
* @date :Created in 2022/9/12 22:03
*/
public class ListBook {
private final ArrayList<Book> books=new ArrayList<>();
public ListBook() {
Book book1=new Book("三国演义","罗贯中","四大名著",18.8);
Book book2=new Book("西游记","罗贯中","四大名著",19.6);
Book book3=new Book("水浒传","罗贯中","四大名著",22.9);
Book book4=new Book("红楼梦","罗贯中","四大名著",35.8);
books.add(book1);
books.add(book2);
books.add(book3);
books.add(book4);
}
//增
public boolean add(Book book){
return books.add(book);
}
// 改
public void set(int index, Book book) {
books.set(index,book);
}
// 查找
public Book get(int index){
return books.get(index);
}
// 删
public Book remove(int index){
return books.remove(index);
}
// 列表
public ArrayList<Book> getBooks(){
return books;
}
public int size(){
return books.size();
}
}
本来到这就不想往下写了,毕竟都深夜两点了,结果发布说我篇幅太短! 唉!!!
4.运行截图