book 类:
package com.kfm.OOP.extend.book;/*
@CreatTime:2023-08-17 20:58
*/
public class Book {
private String title;
private String author;
private Integer year;
private Double price;
public Book(){}
public Book(String title, String author, Integer year, Double price) {
this.title = title;
this.author = author;
this.year = year;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String info(){
return year + "年出版的 " + author + " 的 《" + title + "》, 售价" + price;
}
}
library 类
package com.kfm.OOP.extend.book;
public class Library {
private Book[] books = new Book[10];
private int count = 0;
{
for (int i = 0; i < 10; i++) {
String name = "第" + (i + 1) + "本书";
addBook(new Book(name, "佚名", 2021, 3.86));
}
}
// 添加图书功能 Book类的书
public void addBook(Book book) {
if(count >= books.length){
/* System.out.println("\u001B[31m 抱歉,图书馆已满,无法添加新书!\u001B[0m");
return;
}
if (count >= books.length) {*/
expandCapacity();
}
books[count++] = book;
}
// 扩展数组容量
private void expandCapacity() {
int length = books.length * 2;
Book[] newBooks = new Book[length];
System.arraycopy(books, 0, newBooks, 0, count);
books = newBooks;
}
// 查找书
public void search(String title) {
boolean found = false;
for (int i = 0; i < count; i++) {
if (books[i].getTitle().equals(title)) {
System.out.println(books[i].info());
found = true;
}
}
if (!found) {
System.out.println("\u001B[31m 抱歉,没有找到标题为 " + title + " 的书。\u001B[0m");
}
}
// 显示书的信息
public void show() {
if (count == 0) {
System.out.println("\u001B[31m 目前没有存书!\u001B[0m");
} else {
for (int i = 0; i < count; i++) {
System.out.println(books[i].info());
}
}
}
}
Main 类
package com.kfm.OOP.extend.book;/*
@CreatTime:2023-08-17 21:06
*/
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
static Library library = new Library();//static 属于类
public static void start() {
print("欢迎使用 开发喵图书管理系统 ");
showMenu();
}
private static void showMenu() {
print("请选择功能:");
print("1. 添加图书");
print("2. 查找图书");
print("3. 显示所有图书");
print("0. 退出系统");
System.out.print("请输入:");
int menu = sc.nextInt();
selectMenu(menu);
}
private static void selectMenu(int menu) {
switch (menu) {
case 1 -> addBook();
case 2 -> searchBook();
case 3 -> showAllBook();
default -> exit();
}
// 再次展示菜单
showMenu();
}
private static void exit() {
print("欢迎下次光临");
System.exit(0);
}
private static void showAllBook(){
library.show();
}
private static void searchBook() {
print("请输入书的名称:");
String title = sc.next();
library.search(title);
}
private static void addBook() {
print("请输入书的【名称】:");
String title = sc.next();
print("请输入书的【作者】:");
String author = sc.next();
print("请输入书的【出版年份】:");
int year =sc.nextInt();
print("请输入书的【出版价格】:");
double price = sc.nextDouble();
// 对象创建好了
Book book = new Book(title, author, year, price);
// 添加到 library 中的数组
library.addBook(book);
print("添加 《" + title + "》 成功!");
}
private static void print(String msg) {
System.out.println(msg);
}
}
测试类
package com.kfm.OOP.extend.book;/*
@CreatTime:2023-08-17 21:11
*/
public class Test {
public static void main(String[] args) {
Main.start();
}
}