Book class in Java with Enum and a HashSet

huangapple go评论153阅读模式
英文:

Book class in Java with Enum and a HashSet

问题

以下是翻译好的部分:

  1. public class Book {
  2. enum Type {
  3. Classic_Literature, THRILLER, Psychology, MANUAL, Self_Improvement;
  4. }
  5. String name;
  6. String author;
  7. double price;
  8. public Book(String name, String author, double price) {
  9. this.name = name;
  10. this.author = author;
  11. this.price = price;
  12. }
  13. public String get.name() {
  14. return name;
  15. }
  16. }
  1. public class Books extends Book {
  2. public Books(String name, String author, double price) {
  3. super(name, author, price);
  4. }
  5. public static void main(String[] args) {
  6. HashSet<String> books = new HashSet<>();
  7. books.add("Baltagul"); //classical literature
  8. books.add("Morometii"); //classical literature
  9. books.add("Dezvoltarea personalitatii");
  10. books.add("Criminalul ABC");
  11. books.add("In mintea ta");
  12. books.add("Abecedar");
  13. books.add("Assassin's Creed Revelations");
  14. books.add("In mintea lui");
  15. books.add("Culegere Mate");
  16. books.add("Public Speaking");
  17. }
  18. }

关于问题:

  • 我该如何创建一个链接,以便可以打印出所有来自“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.

  1. public class Book {
  2. enum Type {
  3. Classic_Literature, THRILLER, Psychology, MANUAL, Self_Improvement;
  4. }
  5. String name;
  6. String author;
  7. double price;
  8. public Book(String name, String author, double price) {
  9. this.name = name;
  10. this.author = author;
  11. this.price = price;
  12. }
  13. public String get.name() {
  14. return name;
  15. }
  16. }
  1. public class Books extends Book {
  2. public Books(String name, String author, double price) {
  3. super(name, author, price);
  4. }
  5. public static void main(String[] args) {
  6. HashSet&lt;String&gt; books = new HashSet&lt;&gt;();
  7. carti.add(&quot;Baltagul&quot;); //classical literature
  8. carti.add(&quot;Morometii&quot;); //classical literature
  9. carti.add(&quot;Dezvoltarea personalitatii&quot;);
  10. carti.add(&quot;Criminalul ABC&quot;);
  11. carti.add(&quot;In mintea ta&quot;);
  12. carti.add(&quot;Abecedar&quot;);
  13. carti.add(&quot;Assassin&#39;s Creed Revelations&quot;);
  14. carti.add(&quot;In mintea lui&quot;);
  15. carti.add(&quot;Culegere Mate&quot;);
  16. carti.add(&quot;Public Speaking&quot;);
  17. }
  18. }

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

  1. public class Book {
  2. enum Type {
  3. Classic_Literature, THRILLER, Psychology, MANUAL, Self_Improvement;
  4. }
  5. String name;
  6. String author;
  7. double price;
  8. Type type;
  9. public Book(String name) {
  10. this.name = name;
  11. }
  12. public Book(String name, Type type) {
  13. this.name = name;
  14. this.type = type;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. }
  1. public static void main(String[] args) {
  2. HashSet<Book> books = new HashSet<>();
  3. books.add(new Book("Baltagul", Book.Type.Classic_Literature)); //古典文学
  4. books.add(new Book("Morometii", Book.Type.Classic_Literature)); //古典文学
  5. books.add(new Book("Dezvoltarea personalitatii"));
  6. books.add(new Book("Criminalul ABC"));
  7. books.add(new Book("In mintea ta"));
  8. //在这里检索古典文学书籍
  9. Set<Book> classicBooks = books.stream()
  10. .filter(b -> b.type == Book.Type.Classic_Literature)
  11. .collect(Collectors.toSet());
  12. }
英文:

Some ideas to start improving your code.....

  1. public class Book {
  2. enum Type {
  3. Classic_Literature, THRILLER, Psychology, MANUAL, Self_Improvement;
  4. }
  5. String name;
  6. String author;
  7. double price;
  8. Type type;
  9. public Book(String name) {
  10. this.name = name;
  11. }
  12. public Book(String name, Type type) {
  13. this.name = name;
  14. this.type = type;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. }
  1. public static void main(String[] args) {
  2. HashSet&lt;Book&gt; books = new HashSet&lt;&gt;();
  3. books.add(new Book(&quot;Baltagul&quot;, Book.Type.Classic_Literature)); //classical literature
  4. books.add(new Book(&quot;Morometii&quot;, Book.Type.Classic_Literature)); //classical literature
  5. books.add(new Book(&quot;Dezvoltarea personalitatii&quot;));
  6. books.add(new Book(&quot;Criminalul ABC&quot;));
  7. books.add(new Book(&quot;In mintea ta&quot;));
  8. //here you retrieve the classical literature books
  9. Set&lt;Book&gt; classicBooks = books.stream()
  10. .filter(b -&gt; b.type == Book.Type.Classic_Literature)
  11. .collect(Collectors.toSet());
  12. }

答案2

得分: 0

有关于代码的一些误解。
<br>

  1. 将物品作为书籍,但在主函数中没有使用它。
    <br>那么为什么进行了定义?
    也许需要这样:
    <br>//书籍集合
    <br>HashSet&lt;Book&gt; books = new HashSet&lt;&gt;();
    <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>

  1. 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&lt;Book&gt; books = new HashSet&lt;&gt;();
    <br>2. Why Books should extends Book ?
    No need here.<br> Books is just a regular class where you do aggregation via HashSet
    <br>3.Basic Book instantiation.
    Maybe it's needed:
    <br> books.put(new Book(&quot;Poezii&quot;, &quot;Eminescu&quot;, 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 certain BookType (eg: Classic_Literature)
    ...

huangapple
  • 本文由 发表于 2020年4月7日 04:43:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61068650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定