style:fmt

This commit is contained in:
dichgrem
2025-11-15 13:30:17 +08:00
parent c20e8949a5
commit b03fcbeded
11 changed files with 220 additions and 223 deletions

View File

@@ -1,4 +1,5 @@
package com.study.demo3;
public class Bicycle extends Vehicle {
@Override
public void start() {

View File

@@ -5,26 +5,22 @@ public class Book {
protected String title;
protected boolean available;
public Book() {
this.available = true;
}
public Book(String isbn, String title) {
this.isbn = isbn;
this.title = title;
this.available = true;
}
public Book(String isbn, String title, boolean available) {
this.isbn = isbn;
this.title = title;
this.available = available;
}
public String getIsbn() {
return isbn;
}
@@ -49,11 +45,11 @@ public class Book {
this.available = available;
}
public void returnBook() {
if (!this.available) {
this.available = true;
System.out.println(this.title + " (ISBN: " + this.isbn + ") has been returned and is now available.");
System.out.println(
this.title + " (ISBN: " + this.isbn + ") has been returned and is now available.");
} else {
System.out.println(this.title + " (ISBN: " + this.isbn + ") is already available.");
}

View File

@@ -1,7 +1,7 @@
package com.study.demo3;
public class Car extends Vehicle {
@Override
public void start() {
System.out.println("The car is started");
}

View File

@@ -1,20 +1,17 @@
package com.study.demo3;
public class EBook extends Book {
private double fileSize;
public EBook() {
super();
}
public EBook(String isbn, String title, double fileSize) {
super(isbn, title);
this.fileSize = fileSize;
}
public double getFileSize() {
return fileSize;
}
@@ -25,6 +22,14 @@ public class EBook extends Book {
@Override
public String toString() {
return "EBook [ISBN=" + isbn + ", Title=" + title + ", FileSize=" + fileSize + "MB, Available=" + available + "]";
return "EBook [ISBN="
+ isbn
+ ", Title="
+ title
+ ", FileSize="
+ fileSize
+ "MB, Available="
+ available
+ "]";
}
}

View File

@@ -1,8 +1,9 @@
package com.study.demo3;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
public class Library {
@@ -12,12 +13,9 @@ public class Library {
this.books = new HashMap<>();
}
public void addBook(Book b) {
String isbn = b.getIsbn();
if (books.containsKey(isbn)) {
System.out.println("The book already exists, adding it failed.");
} else {
@@ -26,12 +24,9 @@ public class Library {
}
}
public List<Book> searchAvailableBooks() {
List<Book> availableBooks = new ArrayList<>();
for (Book book : books.values()) {
if (book.isAvailable()) {
availableBooks.add(book);

View File

@@ -1,26 +1,22 @@
package com.study.demo3;
import java.util.List;
public class LibraryTest {
public static void main(String[] args) {
Library lib = new Library();
EBook e1 = new EBook("E001", "Java Basics", 5.2);
e1.setAvailable(false);
PrintedBook p1 = new PrintedBook("P001", "Design Patterns", 3);
lib.addBook(e1);
lib.addBook(p1);
lib.addBook(p1);
System.out.println("\n--- 可借阅图书列表 (available = true) ---");
List<Book> availableList = lib.searchAvailableBooks();
@@ -33,7 +29,6 @@ public class LibraryTest {
}
}
e1.returnBook();
System.out.println("\n--- 恢复 e1 后可借阅图书列表 ---");
availableList = lib.searchAvailableBooks();

View File

@@ -1,4 +1,5 @@
package com.study.demo3;
public class Main {
public static void main(String[] args) {
@@ -7,15 +8,12 @@ public class Main {
EBook ebook = new EBook("123-456", "Java Programming", 2.5);
System.out.println("EBook Status (Initial): " + ebook.toString());
ebook.setAvailable(false);
System.out.println("EBook Status (Borrowed): " + ebook.toString());
ebook.returnBook();
System.out.println("EBook Status (After Return): " + ebook.toString());
System.out.println("\n--- 测试 PrintedBook ---");
PrintedBook pbook = new PrintedBook();
pbook.setIsbn("789-012");
@@ -23,11 +21,9 @@ public class Main {
pbook.setStock(10);
System.out.println("PBook Status (Initial): " + pbook.toString());
pbook.setAvailable(false);
System.out.println("PBook Status (Borrowed): " + pbook.toString());
pbook.returnBook();
System.out.println("PBook Status (After Return): " + pbook.toString());
}

View File

@@ -1,19 +1,17 @@
package com.study.demo3;
public class PrintedBook extends Book {
private int stock;
public PrintedBook() {
super();
}
public PrintedBook(String isbn, String title, int stock) {
super(isbn, title);
this.stock = stock;
}
public int getStock() {
return stock;
}
@@ -24,6 +22,14 @@ public class PrintedBook extends Book {
@Override
public String toString() {
return "PrintedBook [ISBN=" + isbn + ", Title=" + title + ", Stock=" + stock + ", Available=" + available + "]";
return "PrintedBook [ISBN="
+ isbn
+ ", Title="
+ title
+ ", Stock="
+ stock
+ ", Available="
+ available
+ "]";
}
}

View File

@@ -1,4 +1,5 @@
package com.study.demo3;
import java.util.*;
public class StudentManager {

View File

@@ -1,4 +1,5 @@
package com.study.demo3;
public class Train extends Vehicle {
@Override
public void start() {

View File

@@ -1,4 +1,5 @@
package com.study.demo3;
public class Vehicle {
protected String name;