mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-17 21:01:59 -05:00
Compare commits
1 Commits
main
...
b03fcbeded
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b03fcbeded |
@@ -1,7 +1,8 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class Bicycle extends Vehicle {
|
public class Bicycle extends Vehicle {
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
System.out.println("The bike is started");
|
System.out.println("The bike is started");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,61 +1,57 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class Book {
|
public class Book {
|
||||||
protected String isbn;
|
protected String isbn;
|
||||||
protected String title;
|
protected String title;
|
||||||
protected boolean available;
|
protected boolean available;
|
||||||
|
|
||||||
|
public Book() {
|
||||||
public Book() {
|
this.available = true;
|
||||||
this.available = true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public Book(String isbn, String title) {
|
||||||
public Book(String isbn, String title) {
|
this.isbn = isbn;
|
||||||
this.isbn = isbn;
|
this.title = title;
|
||||||
this.title = title;
|
this.available = true;
|
||||||
this.available = true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public Book(String isbn, String title, boolean available) {
|
||||||
public Book(String isbn, String title, boolean available) {
|
this.isbn = isbn;
|
||||||
this.isbn = isbn;
|
this.title = title;
|
||||||
this.title = title;
|
this.available = available;
|
||||||
this.available = available;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public String getIsbn() {
|
||||||
public String getIsbn() {
|
return isbn;
|
||||||
return isbn;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setIsbn(String isbn) {
|
public void setIsbn(String isbn) {
|
||||||
this.isbn = isbn;
|
this.isbn = isbn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTitle(String title) {
|
public void setTitle(String title) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAvailable() {
|
public boolean isAvailable() {
|
||||||
return available;
|
return available;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAvailable(boolean available) {
|
public void setAvailable(boolean available) {
|
||||||
this.available = available;
|
this.available = available;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void returnBook() {
|
||||||
public void returnBook() {
|
if (!this.available) {
|
||||||
if (!this.available) {
|
this.available = true;
|
||||||
this.available = true;
|
System.out.println(
|
||||||
System.out.println(this.title + " (ISBN: " + this.isbn + ") has been returned and is now available.");
|
this.title + " (ISBN: " + this.isbn + ") has been returned and is now available.");
|
||||||
} else {
|
} else {
|
||||||
System.out.println(this.title + " (ISBN: " + this.isbn + ") is already available.");
|
System.out.println(this.title + " (ISBN: " + this.isbn + ") is already available.");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class Car extends Vehicle {
|
public class Car extends Vehicle {
|
||||||
@Override
|
@Override
|
||||||
|
public void start() {
|
||||||
public void start() {
|
System.out.println("The car is started");
|
||||||
System.out.println("The car is started");
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,35 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class EBook extends Book {
|
public class EBook extends Book {
|
||||||
private double fileSize;
|
private double fileSize;
|
||||||
|
|
||||||
|
public EBook() {
|
||||||
public EBook() {
|
super();
|
||||||
super();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public EBook(String isbn, String title, double fileSize) {
|
||||||
|
super(isbn, title);
|
||||||
public EBook(String isbn, String title, double fileSize) {
|
this.fileSize = fileSize;
|
||||||
super(isbn, title);
|
}
|
||||||
this.fileSize = fileSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public double getFileSize() {
|
||||||
public double getFileSize() {
|
return fileSize;
|
||||||
return fileSize;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setFileSize(double fileSize) {
|
public void setFileSize(double fileSize) {
|
||||||
this.fileSize = fileSize;
|
this.fileSize = fileSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
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
|
||||||
|
+ "]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +1,37 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class Library {
|
public class Library {
|
||||||
|
|
||||||
private Map<String, Book> books;
|
|
||||||
|
|
||||||
public Library() {
|
private Map<String, Book> books;
|
||||||
this.books = new HashMap<>();
|
|
||||||
|
public 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 {
|
||||||
|
books.put(isbn, b);
|
||||||
|
System.out.println("Book added successfully: " + b.getTitle());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Book> searchAvailableBooks() {
|
||||||
|
List<Book> availableBooks = new ArrayList<>();
|
||||||
public void addBook(Book b) {
|
|
||||||
String isbn = b.getIsbn();
|
|
||||||
|
|
||||||
|
for (Book book : books.values()) {
|
||||||
if (books.containsKey(isbn)) {
|
if (book.isAvailable()) {
|
||||||
System.out.println("The book already exists, adding it failed.");
|
availableBooks.add(book);
|
||||||
} else {
|
}
|
||||||
books.put(isbn, b);
|
|
||||||
System.out.println("Book added successfully: " + b.getTitle());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public List<Book> searchAvailableBooks() {
|
|
||||||
List<Book> availableBooks = new ArrayList<>();
|
|
||||||
|
|
||||||
|
|
||||||
for (Book book : books.values()) {
|
|
||||||
if (book.isAvailable()) {
|
|
||||||
availableBooks.add(book);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return availableBooks;
|
|
||||||
}
|
}
|
||||||
|
return availableBooks;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +1,39 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class LibraryTest {
|
public class LibraryTest {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Library lib = new Library();
|
Library lib = new Library();
|
||||||
|
|
||||||
|
EBook e1 = new EBook("E001", "Java Basics", 5.2);
|
||||||
EBook e1 = new EBook("E001", "Java Basics", 5.2);
|
|
||||||
|
|
||||||
e1.setAvailable(false);
|
|
||||||
|
|
||||||
|
e1.setAvailable(false);
|
||||||
PrintedBook p1 = new PrintedBook("P001", "Design Patterns", 3);
|
|
||||||
|
|
||||||
|
PrintedBook p1 = new PrintedBook("P001", "Design Patterns", 3);
|
||||||
lib.addBook(e1);
|
|
||||||
lib.addBook(p1);
|
|
||||||
|
|
||||||
|
lib.addBook(e1);
|
||||||
lib.addBook(p1);
|
lib.addBook(p1);
|
||||||
|
|
||||||
|
lib.addBook(p1);
|
||||||
System.out.println("\n--- 可借阅图书列表 (available = true) ---");
|
|
||||||
List<Book> availableList = lib.searchAvailableBooks();
|
|
||||||
|
|
||||||
if (availableList.isEmpty()) {
|
System.out.println("\n--- 可借阅图书列表 (available = true) ---");
|
||||||
System.out.println("No books are currently available for loan.");
|
List<Book> availableList = lib.searchAvailableBooks();
|
||||||
} else {
|
|
||||||
for (Book book : availableList) {
|
if (availableList.isEmpty()) {
|
||||||
|
System.out.println("No books are currently available for loan.");
|
||||||
System.out.println("- " + book.getTitle() + " (ISBN: " + book.getIsbn() + ")");
|
} else {
|
||||||
}
|
for (Book book : availableList) {
|
||||||
}
|
|
||||||
|
System.out.println("- " + book.getTitle() + " (ISBN: " + book.getIsbn() + ")");
|
||||||
|
}
|
||||||
e1.returnBook();
|
|
||||||
System.out.println("\n--- 恢复 e1 后可借阅图书列表 ---");
|
|
||||||
availableList = lib.searchAvailableBooks();
|
|
||||||
for (Book book : availableList) {
|
|
||||||
System.out.println("- " + book.getTitle() + " (ISBN: " + book.getIsbn() + ")");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
e1.returnBook();
|
||||||
|
System.out.println("\n--- 恢复 e1 后可借阅图书列表 ---");
|
||||||
|
availableList = lib.searchAvailableBooks();
|
||||||
|
for (Book book : availableList) {
|
||||||
|
System.out.println("- " + book.getTitle() + " (ISBN: " + book.getIsbn() + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,30 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
System.out.println("--- 测试 EBook ---");
|
|
||||||
|
|
||||||
EBook ebook = new EBook("123-456", "Java Programming", 2.5);
|
|
||||||
System.out.println("EBook Status (Initial): " + ebook.toString());
|
|
||||||
|
|
||||||
|
System.out.println("--- 测试 EBook ---");
|
||||||
ebook.setAvailable(false);
|
|
||||||
System.out.println("EBook Status (Borrowed): " + ebook.toString());
|
|
||||||
|
|
||||||
|
EBook ebook = new EBook("123-456", "Java Programming", 2.5);
|
||||||
ebook.returnBook();
|
System.out.println("EBook Status (Initial): " + ebook.toString());
|
||||||
System.out.println("EBook Status (After Return): " + ebook.toString());
|
|
||||||
|
|
||||||
|
ebook.setAvailable(false);
|
||||||
System.out.println("\n--- 测试 PrintedBook ---");
|
System.out.println("EBook Status (Borrowed): " + ebook.toString());
|
||||||
PrintedBook pbook = new PrintedBook();
|
|
||||||
pbook.setIsbn("789-012");
|
|
||||||
pbook.setTitle("Python Guide");
|
|
||||||
pbook.setStock(10);
|
|
||||||
System.out.println("PBook Status (Initial): " + pbook.toString());
|
|
||||||
|
|
||||||
|
ebook.returnBook();
|
||||||
pbook.setAvailable(false);
|
System.out.println("EBook Status (After Return): " + ebook.toString());
|
||||||
System.out.println("PBook Status (Borrowed): " + pbook.toString());
|
|
||||||
|
|
||||||
|
System.out.println("\n--- 测试 PrintedBook ---");
|
||||||
pbook.returnBook();
|
PrintedBook pbook = new PrintedBook();
|
||||||
System.out.println("PBook Status (After Return): " + pbook.toString());
|
pbook.setIsbn("789-012");
|
||||||
}
|
pbook.setTitle("Python Guide");
|
||||||
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +1,35 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class PrintedBook extends Book {
|
public class PrintedBook extends Book {
|
||||||
private int stock;
|
private int stock;
|
||||||
|
|
||||||
|
public PrintedBook() {
|
||||||
public PrintedBook() {
|
super();
|
||||||
super();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public PrintedBook(String isbn, String title, int stock) {
|
||||||
public PrintedBook(String isbn, String title, int stock) {
|
super(isbn, title);
|
||||||
super(isbn, title);
|
this.stock = stock;
|
||||||
this.stock = stock;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public int getStock() {
|
||||||
public int getStock() {
|
return stock;
|
||||||
return stock;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setStock(int stock) {
|
public void setStock(int stock) {
|
||||||
this.stock = stock;
|
this.stock = stock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "PrintedBook [ISBN=" + isbn + ", Title=" + title + ", Stock=" + stock + ", Available=" + available + "]";
|
return "PrintedBook [ISBN="
|
||||||
}
|
+ isbn
|
||||||
|
+ ", Title="
|
||||||
|
+ title
|
||||||
|
+ ", Stock="
|
||||||
|
+ stock
|
||||||
|
+ ", Available="
|
||||||
|
+ available
|
||||||
|
+ "]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,29 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class StudentManager {
|
public class StudentManager {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
List<String> studentNames = new ArrayList<>();
|
List<String> studentNames = new ArrayList<>();
|
||||||
Map<String, Integer> studentScores = new HashMap<>();
|
Map<String, Integer> studentScores = new HashMap<>();
|
||||||
|
|
||||||
studentNames.add("张三");
|
studentNames.add("张三");
|
||||||
studentNames.add("李四");
|
studentNames.add("李四");
|
||||||
studentNames.add("王五");
|
studentNames.add("王五");
|
||||||
|
|
||||||
studentScores.put("张三", 85);
|
studentScores.put("张三", 85);
|
||||||
studentScores.put("李四", 77);
|
studentScores.put("李四", 77);
|
||||||
studentScores.put("王五", 99);
|
studentScores.put("王五", 99);
|
||||||
|
|
||||||
System.out.println("--- 所有学生姓名 ---");
|
System.out.println("--- 所有学生姓名 ---");
|
||||||
for (String name : studentNames) {
|
for (String name : studentNames) {
|
||||||
System.out.println(name);
|
System.out.println(name);
|
||||||
}
|
|
||||||
|
|
||||||
studentScores.put("张三", 70);
|
|
||||||
|
|
||||||
Integer zhangsanScore = studentScores.get("张三");
|
|
||||||
System.out.println("\n--- 修改后张三的成绩 ---");
|
|
||||||
System.out.println("张三的成绩: " + zhangsanScore);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
studentScores.put("张三", 70);
|
||||||
|
|
||||||
|
Integer zhangsanScore = studentScores.get("张三");
|
||||||
|
System.out.println("\n--- 修改后张三的成绩 ---");
|
||||||
|
System.out.println("张三的成绩: " + zhangsanScore);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class Train extends Vehicle {
|
public class Train extends Vehicle {
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
System.out.println("The train is started");
|
System.out.println("The train is started");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class Vehicle {
|
public class Vehicle {
|
||||||
protected String name;
|
protected String name;
|
||||||
|
|
||||||
public Vehicle(String name) {
|
public Vehicle(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vehicle() {
|
|
||||||
this.name = "Unknown Vehicle";
|
|
||||||
}
|
|
||||||
|
|
||||||
public void start() {
|
public Vehicle() {
|
||||||
System.out.println("Vehicle Start");
|
this.name = "Unknown Vehicle";
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void start() {
|
||||||
System.out.println("Vehicle Stop");
|
System.out.println("Vehicle Start");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
System.out.println("Vehicle Stop");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user