62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.*;
public class Hello {
public static void main(String[] args) {
// phase 2
final int total = 3;
final Random random = new Random();
List<Book> books = new ArrayList<>(total);
try (Scanner sc = new Scanner(System.in)) {
for (int i = 1; i <= total; i++) {
var book = new Book();
book.setCode("code" + i);
book.setName("name" + i);
book.setPrice(random.nextDouble() * 1000);
books.add(book);
}
}
// phase 3
for (var book : books) {
System.out.println(book);
}
}
}
// Phase 1
class Book {
private String code;
private String name;
private Double price;
@Override
public String toString() {
String format = String.format("code=%s,name=%s,price=%f", this.code, this.name, this.price);
return format;
}
public String getCode() {
return code;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setCode(String code) {
this.code = code;
}
}