here is my code:

package com.company; class Roman { private Roman []roman = new Roman[20]; String author; String name; int data; int size; String genre; Roman(){ } Roman(String author,String name,int data, int size,String genre){ this.author= author; this.name=name; this.data=data; this.size=size; this.genre=genre; } void print(Roman rom){ System.out.println("НазваниС : "+rom.name); System.out.println("Автор : "+rom.author); System.out.println("Π“ΠΎΠ΄ создания : "+rom.data+" Π³."); System.out.println("Π Π°Π·ΠΌΠ΅Ρ€ : "+rom.size+" Π‘Ρ‚Ρ€Π°Π½ΠΈΡ†"); System.out.println("Π–Π°Π½Ρ€Ρ‹ : "+rom.genre); System.out.println("\n"); } /*void minyear(){ int min = roman[0].data; for(int i=0;i<=roman.length;i++){ if(roman[i].data<min) min=roman[i].data; } System.out.println("Бамая ΡΡ‚Π°Ρ€ΡˆΠΈΠΉ Ρ€ΠΎΠΌΠ°Π½ : "+min); }*/ } public class Main { public static void main(String[] args) { Roman []roman = new Roman[20]; Roman t; roman[0] = new Roman("Иван Π‘Π΅Ρ€Π³Π΅Π΅Π²ΠΈΡ‡ Π’ΡƒΡ€Π³Π΅Π½Π΅Π²","ΠžΡ‚Ρ†Ρ‹ ΠΈ Π΄Π΅Ρ‚ΠΈ",1862,300,"Π›ΡŽΠ±ΠΎΠ²Π½Ρ‹ΠΉ Ρ€ΠΎΠΌΠ°Π½, Ѐилософский Ρ€ΠΎΠΌΠ°Π½, ΠŸΠΎΠ»ΠΈΡ‚ΠΈΡ‡Π΅ΡΠΊΠ°Ρ фантастика"); roman[1] = new Roman("Иван Π‘Π΅Ρ€Π³Π΅Π΅Π²ΠΈΡ‡ Π’ΡƒΡ€Π³Π΅Π½Π΅Π²","ΠœΡƒΠΌΡƒ",1854,35,"Π₯удоТСствСнноС ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅"); roman[2] = new Roman("Иван Π‘Π΅Ρ€Π³Π΅Π΅Π²ΠΈΡ‡ Π’ΡƒΡ€Π³Π΅Π½Π΅Π²","Записки ΠΎΡ…ΠΎΡ‚Π½ΠΈΠΊΠ°",1852,50,"Π₯удоТСствСнноС ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅"); roman[3] = new Roman("Π›Π΅Π² НиколаСвич Волстой","Π’ΠΎΠΉΠ½Π° ΠΈ ΠΌΠΈΡ€",1867,1274,"Π ΠΎΠΌΠ°Π½, Π›ΡŽΠ±ΠΎΠ²Π½Ρ‹ΠΉ Ρ€ΠΎΠΌΠ°Π½, ВоСнная ΠΏΡ€ΠΎΠ·Π°, Π˜ΡΡ‚ΠΎΡ€ΠΈΡ‡Π΅ΡΠΊΠΈΠΉ ΠΆΠ°Π½Ρ€, Ѐилософский Ρ€ΠΎΠΌΠ°Π½"); roman[4] = new Roman("Π›Π΅Π² НиколаСвич Волстой","Анна ΠšΠ°Ρ€Π΅Π½ΠΈΠ½Π°",1877,776,"Π ΠΎΠΌΠ°Π½, Π Π΅Π°Π»ΠΈΠ·ΠΌ, Π₯удоТСствСнноС ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅"); for(int i=0;i<=roman.length;i++) { roman[i].print(roman[i]); } } } 

I could not create a function that receives the values ​​of the entered books (for example, a function that recognizes the smallest number of pages, etc. I tried (taken as a comment)) help create a function when I try gives NPE

  • 1) format the code 2) try to do at least something yourself, clearly describing exactly what you can’t do - michael_best
  • How did you start to create? - Roman C

1 answer 1

  import java.util.Objects; public class Roman implements Comparable<Roman> { private final String author; private final String name; private final Integer data; private final Integer size; private final String genre; public Roman(String author, String name, int data, int size, String genre) { this.author = author; this.name = name; this.data = data; this.size = size; this.genre = genre; } public String getAuthor() { return author; } public String getName() { return name; } public int getData() { return data; } public int getSize() { return size; } public String getGenre() { return genre; } @Override public int hashCode() { int hash = 3; hash = 53 * hash + Objects.hashCode(this.author); hash = 53 * hash + Objects.hashCode(this.name); hash = 53 * hash + Objects.hashCode(this.data); hash = 53 * hash + Objects.hashCode(this.size); hash = 53 * hash + Objects.hashCode(this.genre); return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Roman other = (Roman) obj; if (!Objects.equals(this.author, other.author)) { return false; } if (!Objects.equals(this.name, other.name)) { return false; } if (!Objects.equals(this.genre, other.genre)) { return false; } if (!Objects.equals(this.data, other.data)) { return false; } if (!Objects.equals(this.size, other.size)) { return false; } return true; } @Override public String toString() { return "Roman{" + "author=" + author + ", name=" + name + ", data=" + data + ", size=" + size + ", genre=" + genre + '}'; } @Override public int compareTo(Roman roman) { return this.data.compareTo(roman.getData()); } } public static void main(String[] args) { TreeSet<Roman>roman = new TreeSet<>(); roman.add(new Roman("Иван Π‘Π΅Ρ€Π³Π΅Π΅Π²ΠΈΡ‡ Π’ΡƒΡ€Π³Π΅Π½Π΅Π²", "ΠžΡ‚Ρ†Ρ‹ ΠΈ Π΄Π΅Ρ‚ΠΈ", 1862, 300, "Π›ΡŽΠ±ΠΎΠ²Π½Ρ‹ΠΉ Ρ€ΠΎΠΌΠ°Π½, Ѐилософский Ρ€ΠΎΠΌΠ°Π½, ΠŸΠΎΠ»ΠΈΡ‚ΠΈΡ‡Π΅ΡΠΊΠ°Ρ фантастика")); roman.add(new Roman("Иван Π‘Π΅Ρ€Π³Π΅Π΅Π²ΠΈΡ‡ Π’ΡƒΡ€Π³Π΅Π½Π΅Π²", "ΠœΡƒΠΌΡƒ", 1854, 35, "Π₯удоТСствСнноС ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅")); roman.add(new Roman("Иван Π‘Π΅Ρ€Π³Π΅Π΅Π²ΠΈΡ‡ Π’ΡƒΡ€Π³Π΅Π½Π΅Π²", "Записки ΠΎΡ…ΠΎΡ‚Π½ΠΈΠΊΠ°", 1852, 50, "Π₯удоТСствСнноС ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅")); roman.add(new Roman("Π›Π΅Π² НиколаСвич Волстой", "Π’ΠΎΠΉΠ½Π° ΠΈ ΠΌΠΈΡ€", 1867, 1274, "Π ΠΎΠΌΠ°Π½, Π›ΡŽΠ±ΠΎΠ²Π½Ρ‹ΠΉ Ρ€ΠΎΠΌΠ°Π½, ВоСнная ΠΏΡ€ΠΎΠ·Π°, Π˜ΡΡ‚ΠΎΡ€ΠΈΡ‡Π΅ΡΠΊΠΈΠΉ ΠΆΠ°Π½Ρ€, Ѐилософский Ρ€ΠΎΠΌΠ°Π½")); roman.add(new Roman("Π›Π΅Π² НиколаСвич Волстой", "Анна ΠšΠ°Ρ€Π΅Π½ΠΈΠ½Π°", 1877, 776, "Π ΠΎΠΌΠ°Π½, Π Π΅Π°Π»ΠΈΠ·ΠΌ, Π₯удоТСствСнноС ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅")); //Collections.sort(roman); System.out.println(roman.first()); } 

Your Roman class cannot define a book with the smallest year (we are not talking about static methods now), because the class is a kind of β€œdrawing” for creating objects. Creating an object through the operator new, each time you create an independent entity. Each of the objects of the Roman class contains information about only one book that is stored in the fields of the object, i.e. each object knows only its own year. Therefore, it is impossible to find out about the year in other books within the object. The Main method in the Main class creates a collection to which it adds many objects. Now our collection stores information about a variety of books, which means that within the collection you can make a comparison. In this case, I just sort the books by year. This is not the only option, just want to show how it works. If you have questions - ask. And more ... In the Roman class you have a huge amount of standard code (constructor getters, methods toString, equals, hashcode). To not write this look in the direction of lombok. This library dynamically generates these methods and is managed using annotations. Moreover, you can easily use a builder instead of a not very successful designer. There are logging and many other nice "buns"

  • Thank you very grateful - Jamaka
  • It's my pleasure. If something is not clear - ask, do not hesitate ... - Dmitry