Book class in Java with Enum and a HashSet

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

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&lt;String&gt; books = new HashSet&lt;&gt;();
        carti.add(&quot;Baltagul&quot;);                //classical literature
        carti.add(&quot;Morometii&quot;);               //classical literature
        carti.add(&quot;Dezvoltarea personalitatii&quot;);
        carti.add(&quot;Criminalul ABC&quot;);
        carti.add(&quot;In mintea ta&quot;);
        carti.add(&quot;Abecedar&quot;);
        carti.add(&quot;Assassin&#39;s Creed Revelations&quot;);
        carti.add(&quot;In mintea lui&quot;);
        carti.add(&quot;Culegere Mate&quot;);
        carti.add(&quot;Public Speaking&quot;);
    }
}

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&lt;Book&gt; books = new HashSet&lt;&gt;();
        books.add(new Book(&quot;Baltagul&quot;, Book.Type.Classic_Literature));                //classical literature
        books.add(new Book(&quot;Morometii&quot;, Book.Type.Classic_Literature));               //classical literature
        books.add(new Book(&quot;Dezvoltarea personalitatii&quot;));
        books.add(new Book(&quot;Criminalul ABC&quot;));
        books.add(new Book(&quot;In mintea ta&quot;));

        //here you retrieve the classical literature books

        Set&lt;Book&gt; classicBooks = books.stream()
                .filter(b -&gt; b.type == Book.Type.Classic_Literature)
                .collect(Collectors.toSet());
    }

答案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:

确定