mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-16 20:42:00 -05:00
style:fmt
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
com/study/test/
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.study.demo;
|
package com.study.demo;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
// I am 212306206
|
// I am 212306206
|
||||||
public class test1_type {
|
public class test1_type {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -13,4 +13,3 @@ public class test2_for {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,4 +16,3 @@ public class test3_1_99 {
|
|||||||
System.out.println("1+2+...+99 的和 = " + sum);
|
System.out.println("1+2+...+99 的和 = " + sum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package com.study.demo;
|
package com.study.demo;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class test5 {
|
public class test5 {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ public abstract class Animal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public abstract void makeSound();
|
public abstract void makeSound();
|
||||||
|
|
||||||
public abstract void move();
|
public abstract void move();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ public class Rectangle {
|
|||||||
private double width = 1.0;
|
private double width = 1.0;
|
||||||
private double height = 1.0;
|
private double height = 1.0;
|
||||||
|
|
||||||
public Rectangle() {}
|
public Rectangle() {
|
||||||
|
}
|
||||||
|
|
||||||
public Rectangle(double width, double height) {
|
public Rectangle(double width, double height) {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
|
|||||||
@@ -24,17 +24,23 @@ public class Student {
|
|||||||
public int getMaxScore() {
|
public int getMaxScore() {
|
||||||
int max = score[0];
|
int max = score[0];
|
||||||
for (int s : score) {
|
for (int s : score) {
|
||||||
if (s > max) max = s;
|
if (s > max)
|
||||||
|
max = s;
|
||||||
}
|
}
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
public char getGrade() {
|
public char getGrade() {
|
||||||
double avg = getAverage();
|
double avg = getAverage();
|
||||||
if (avg >= 90) return 'A';
|
if (avg >= 90)
|
||||||
else if (avg >= 80) return 'B';
|
return 'A';
|
||||||
else if (avg >= 70) return 'C';
|
else if (avg >= 80)
|
||||||
else if (avg >= 60) return 'D';
|
return 'B';
|
||||||
else return 'E';
|
else if (avg >= 70)
|
||||||
|
return 'C';
|
||||||
|
else if (avg >= 60)
|
||||||
|
return 'D';
|
||||||
|
else
|
||||||
|
return 'E';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,6 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int[] scores = { 85, 90, 78, 92, 88, 76, 95, 89, 79, 94 };
|
int[] scores = { 85, 90, 78, 92, 88, 76, 95, 89, 79, 94 };
|
||||||
Student student = new Student("5001", "张三", scores);
|
Student student = new Student("5001", "张三", scores);
|
||||||
|
|
||||||
System.out.println("平均分: " + student.getAverage());
|
System.out.println("平均分: " + student.getAverage());
|
||||||
System.out.println("最高分: " + student.getMaxScore());
|
System.out.println("最高分: " + student.getMaxScore());
|
||||||
System.out.println("等级: " + student.getGrade());
|
System.out.println("等级: " + student.getGrade());
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
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() {
|
||||||
|
|||||||
@@ -5,26 +5,22 @@ public class Book {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -49,11 +45,11 @@ public class Book {
|
|||||||
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(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 {
|
} else {
|
||||||
System.out.println(this.title + " (ISBN: " + this.isbn + ") is already available.");
|
System.out.println(this.title + " (ISBN: " + this.isbn + ") is already available.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
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,20 +1,17 @@
|
|||||||
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) {
|
public EBook(String isbn, String title, double fileSize) {
|
||||||
super(isbn, title);
|
super(isbn, title);
|
||||||
this.fileSize = fileSize;
|
this.fileSize = fileSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public double getFileSize() {
|
public double getFileSize() {
|
||||||
return fileSize;
|
return fileSize;
|
||||||
}
|
}
|
||||||
@@ -25,6 +22,14 @@ public class EBook extends Book {
|
|||||||
|
|
||||||
@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,8 +1,9 @@
|
|||||||
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 {
|
||||||
|
|
||||||
@@ -12,12 +13,9 @@ public class Library {
|
|||||||
this.books = new HashMap<>();
|
this.books = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void addBook(Book b) {
|
public void addBook(Book b) {
|
||||||
String isbn = b.getIsbn();
|
String isbn = b.getIsbn();
|
||||||
|
|
||||||
|
|
||||||
if (books.containsKey(isbn)) {
|
if (books.containsKey(isbn)) {
|
||||||
System.out.println("The book already exists, adding it failed.");
|
System.out.println("The book already exists, adding it failed.");
|
||||||
} else {
|
} else {
|
||||||
@@ -26,12 +24,9 @@ public class Library {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public List<Book> searchAvailableBooks() {
|
public List<Book> searchAvailableBooks() {
|
||||||
List<Book> availableBooks = new ArrayList<>();
|
List<Book> availableBooks = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
for (Book book : books.values()) {
|
for (Book book : books.values()) {
|
||||||
if (book.isAvailable()) {
|
if (book.isAvailable()) {
|
||||||
availableBooks.add(book);
|
availableBooks.add(book);
|
||||||
|
|||||||
@@ -1,26 +1,22 @@
|
|||||||
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(e1);
|
||||||
lib.addBook(p1);
|
lib.addBook(p1);
|
||||||
|
|
||||||
|
|
||||||
lib.addBook(p1);
|
lib.addBook(p1);
|
||||||
|
|
||||||
|
|
||||||
System.out.println("\n--- 可借阅图书列表 (available = true) ---");
|
System.out.println("\n--- 可借阅图书列表 (available = true) ---");
|
||||||
List<Book> availableList = lib.searchAvailableBooks();
|
List<Book> availableList = lib.searchAvailableBooks();
|
||||||
|
|
||||||
@@ -33,7 +29,6 @@ public class LibraryTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
e1.returnBook();
|
e1.returnBook();
|
||||||
System.out.println("\n--- 恢复 e1 后可借阅图书列表 ---");
|
System.out.println("\n--- 恢复 e1 后可借阅图书列表 ---");
|
||||||
availableList = lib.searchAvailableBooks();
|
availableList = lib.searchAvailableBooks();
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
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) {
|
||||||
|
|
||||||
@@ -7,15 +8,12 @@ public class Main {
|
|||||||
EBook ebook = new EBook("123-456", "Java Programming", 2.5);
|
EBook ebook = new EBook("123-456", "Java Programming", 2.5);
|
||||||
System.out.println("EBook Status (Initial): " + ebook.toString());
|
System.out.println("EBook Status (Initial): " + ebook.toString());
|
||||||
|
|
||||||
|
|
||||||
ebook.setAvailable(false);
|
ebook.setAvailable(false);
|
||||||
System.out.println("EBook Status (Borrowed): " + ebook.toString());
|
System.out.println("EBook Status (Borrowed): " + ebook.toString());
|
||||||
|
|
||||||
|
|
||||||
ebook.returnBook();
|
ebook.returnBook();
|
||||||
System.out.println("EBook Status (After Return): " + ebook.toString());
|
System.out.println("EBook Status (After Return): " + ebook.toString());
|
||||||
|
|
||||||
|
|
||||||
System.out.println("\n--- 测试 PrintedBook ---");
|
System.out.println("\n--- 测试 PrintedBook ---");
|
||||||
PrintedBook pbook = new PrintedBook();
|
PrintedBook pbook = new PrintedBook();
|
||||||
pbook.setIsbn("789-012");
|
pbook.setIsbn("789-012");
|
||||||
@@ -23,11 +21,9 @@ public class Main {
|
|||||||
pbook.setStock(10);
|
pbook.setStock(10);
|
||||||
System.out.println("PBook Status (Initial): " + pbook.toString());
|
System.out.println("PBook Status (Initial): " + pbook.toString());
|
||||||
|
|
||||||
|
|
||||||
pbook.setAvailable(false);
|
pbook.setAvailable(false);
|
||||||
System.out.println("PBook Status (Borrowed): " + pbook.toString());
|
System.out.println("PBook Status (Borrowed): " + pbook.toString());
|
||||||
|
|
||||||
|
|
||||||
pbook.returnBook();
|
pbook.returnBook();
|
||||||
System.out.println("PBook Status (After Return): " + pbook.toString());
|
System.out.println("PBook Status (After Return): " + pbook.toString());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -24,6 +22,14 @@ public class PrintedBook extends Book {
|
|||||||
|
|
||||||
@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,4 +1,5 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class StudentManager {
|
public class StudentManager {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
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() {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package com.study.demo3;
|
package com.study.demo3;
|
||||||
|
|
||||||
public class Vehicle {
|
public class Vehicle {
|
||||||
protected String name;
|
protected String name;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user