Buen día a todos, estoy en clases de POO en Java, tengo el siguiente ejercicio:
¿Solamente pones un POJO? Trata de
analizar el enunciado. Primero identifica entidades:
- Libro (nombre, autor o autores, año de edición, ¿edición de lujo?)
- Autor (nombres)
- Colección (mas de 1 libro)
Dado lo anterior deducimos que:
Autor depende de Libro. Por lo que un Libro no puede existir sin un Autor. Así mismo, Un libro puede tener uno o más autores[/b]:
public class Author {
private Integer id; // puede servir de algo
private String names;
public Author() {
}
public Author(Integer id, String names) {
this.id = id;
this.names = names;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
}
public class Book {
private String isbn;
private String name;
private List<Author> authors;
private Date editionDate;
private Boolean deluxeEdition;
public Book() {
}
public Book(String isbn, String name, List<Author> authors, Date editionDate, Boolean deluxeEdition) {
this.isbn = isbn;
this.name = name;
this.authors = authors;
this.editionDate = editionDate;
this.deluxeEdition = deluxeEdition;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Author> getAuthors() {
return authors;
}
public void setAuthors(List<Author> authors) {
this.authors = authors;
}
public Date getEditionDate() {
return date;
}
public void setEditionDate(Date editionDate) {
this.editionDate = editionDate;
}
public Boolean isdDeluxeEdition() {
return deluxeEdition;
}
public void setDeluxeEdition(Boolean deluxeEdition) {
this.deluxeEdition = deluxeEdition;
}
}
Y la colección:
public class Collection {
private final NavigableMap<Integer, Book> collection = new TreeMap<>();
public Book add(Book book) {
Integer newId = null; // numero que ocupa el libro en la coleccion
if(collection.isEmpty()) { newId = 1; }
else { newId = collection.lastKey(); }
return collection.put(newId, book);
}
public Book remove(Integer id) {
return collection.remove(id);
}
public Book get(Integer id) {
return collection.get(id);
}
public void showAll() {
for(Entry.Map<Integer, Book> entry : collection.entrySet()) {
System.out.println("Posición del libro: "+entry.getKey());
System.out.println("Nombre del libro: "entry.getValue().getName());
}
}
}
Saludos.
PD: Si vas a colocar código otra vez, utiliza las etiquetas GeSHi. Si no sabes que son, lee las reglas del foro.
Muchas gracias por tu respuesta,
Gacias, estoy terminando y hay una linea que no me ha dejado al compilar, me sigue saliendo error y no se por que es:
public void showAll() {
for(Map.Entry<Integer, Libros> entry : coleccion.entrySet()) {
System.out.println("Posicion del libro: "+entry.getKey());
System.out.println("Nombre del libro: "+entry.getValue().getNombre());
}
Me sigue saliendo "package Map does not exist"
Map.Entry<K,V> es una interface parte de Map. Lo que debes hacer es importar la interface Map y la clase HashMap así:
import java.util.Map;
import java.util.HashMap;
Saludos.
1
1) Por segunda vez te digo, si vas a publicar código utiliza las etiquetas GeSHi. Caso contrario, reportaré el tema a los moderadores.
2) No sé de donde te salen esos errores. A mí me compila perfectamente:
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.Map;
public class Collection {
private NavigableMap<Integer, Book> collection = new TreeMap <>();
public Book add(Book book){
Integer newId = (collection.isEmpty()) ? 1 : collection.lastKey();
return collection.put(newId, book);
}
public Book remove(Integer id) {
return collection.remove(id);
}
public Book get(Integer id) {
return collection.get(id);
}
public void showAll() {
for(Map.Entry<Integer, Book> entry : collection.entrySet()) {
System.out.println("Posicion del libro: "+entry.getKey());
System.out.println("Nombre del libro: "+entry.getValue());
}
}
}