英文:
Book class in Java with Enum and a HashSet
问题
以下是翻译好的部分:
public class Book {
enum Type {
Classic_Literature, THRILLER, Psychology, MANUAL, Self_Improvement;
}
String name;
String author;
double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String get.name() {
return name;
}
}
public class Books extends Book {
public Books(String name, String author, double price) {
super(name, author, price);
}
public static void main(String[] args) {
HashSet<String> books = new HashSet<>();
books.add("Baltagul"); //classical literature
books.add("Morometii"); //classical literature
books.add("Dezvoltarea personalitatii");
books.add("Criminalul ABC");
books.add("In mintea ta");
books.add("Abecedar");
books.add("Assassin's Creed Revelations");
books.add("In mintea lui");
books.add("Culegere Mate");
books.add("Public Speaking");
}
}
关于问题:
- 我该如何创建一个链接,以便可以打印出所有来自“Literatura clasica”(古典文学)的书籍?
- 我不太清楚如何实现这样一个方法。
(注意:我已经移除了代码部分的翻译,只提供了代码的英文原文。)
英文:
Need to create a book class with some features:name,author,price.The type of the book must be an enum and also i need to add some books using hashset.
public class Book {
enum Type {
Classic_Literature, THRILLER, Psychology, MANUAL, Self_Improvement;
}
String name;
String author;
double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String get.name() {
return name;
}
}
public class Books extends Book {
public Books(String name, String author, double price) {
super(name, author, price);
}
public static void main(String[] args) {
HashSet<String> books = new HashSet<>();
carti.add("Baltagul"); //classical literature
carti.add("Morometii"); //classical literature
carti.add("Dezvoltarea personalitatii");
carti.add("Criminalul ABC");
carti.add("In mintea ta");
carti.add("Abecedar");
carti.add("Assassin's Creed Revelations");
carti.add("In mintea lui");
carti.add("Culegere Mate");
carti.add("Public Speaking");
}
}
The books are in Romanian so don't mind them.
My problem is:
-How can I make a link so that I can print out all books from "Literatura clasica"(Classical Literature)?
I don't exactly know how to implement such a method.
答案1
得分: 1
public class Book {
enum Type {
Classic_Literature, THRILLER, Psychology, MANUAL, Self_Improvement;
}
String name;
String author;
double price;
Type type;
public Book(String name) {
this.name = name;
}
public Book(String name, Type type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
HashSet<Book> books = new HashSet<>();
books.add(new Book("Baltagul", Book.Type.Classic_Literature)); //古典文学
books.add(new Book("Morometii", Book.Type.Classic_Literature)); //古典文学
books.add(new Book("Dezvoltarea personalitatii"));
books.add(new Book("Criminalul ABC"));
books.add(new Book("In mintea ta"));
//在这里检索古典文学书籍
Set<Book> classicBooks = books.stream()
.filter(b -> b.type == Book.Type.Classic_Literature)
.collect(Collectors.toSet());
}
英文:
Some ideas to start improving your code.....
public class Book {
enum Type {
Classic_Literature, THRILLER, Psychology, MANUAL, Self_Improvement;
}
String name;
String author;
double price;
Type type;
public Book(String name) {
this.name = name;
}
public Book(String name, Type type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
HashSet<Book> books = new HashSet<>();
books.add(new Book("Baltagul", Book.Type.Classic_Literature)); //classical literature
books.add(new Book("Morometii", Book.Type.Classic_Literature)); //classical literature
books.add(new Book("Dezvoltarea personalitatii"));
books.add(new Book("Criminalul ABC"));
books.add(new Book("In mintea ta"));
//here you retrieve the classical literature books
Set<Book> classicBooks = books.stream()
.filter(b -> b.type == Book.Type.Classic_Literature)
.collect(Collectors.toSet());
}
答案2
得分: 0
有关于代码的一些误解。
<br>
- 将物品作为书籍,但在主函数中没有使用它。
<br>那么为什么进行了定义?
也许需要这样:
<br>//书籍集合
<br>HashSet<Book> books = new HashSet<>();
<br>2. 为什么Books
应该扩展为Book
?
在这里不需要。
<br>Books
只是一个常规类,在其中通过HashSet
进行聚合。
<br>3. 基本的Book
实例化。
也许需要这样:
<br>books.put(new Book("Poezii", "Eminescu", 123))
<br>.4. 书籍分类在哪里链接?
<br>也许可以修改构造函数:
<br>public Book(String name, String author, double price, BookType bookType)
<br>然后进行适当的实例化
<br>.5. 显示特定分类内的书籍。
只需在集合内使用常规循环,并仅对特定的BookType
(例如:Classic_Literature)进行验证。
英文:
There are some misconceptions on the code.
<br>
- Have the item as book and not use it within main.
<br>So why was defined ?
Maybe this is required:
<br>//colection of books
<br>HashSet<Book> books = new HashSet<>();
<br>2. WhyBooks
should extendsBook
?
No need here.<br> Books is just a regular class where you do aggregation viaHashSet
<br>3.BasicBook
instantiation.
Maybe it's needed:
<br>books.put(new Book("Poezii", "Eminescu", 123))
<br>.4.Where is the classification of book linked ?
<br>Maybe altering the constructor:
<br>public Book(String name, String author, double price, BookType bookType)
<br> and after do a proper instantiation
<br>.5. Display the books within a classification.<br> Just use a regular loop within collection and validate only for a certainBookType
(eg: Classic_Literature)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论