|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +public class Josegs95 { |
| 7 | + public static void main(String[] args) { |
| 8 | + //Ejercicio |
| 9 | + //Forma incorrecta |
| 10 | + IncorrectStudent student1 = new IncorrectStudent(678, "Pepe"); |
| 11 | + student1.addScore(8); |
| 12 | + |
| 13 | + //Forma correcta |
| 14 | + CorrectStudent student2 = new CorrectStudent(551, "Rocío"); |
| 15 | + Exam exam1 = new Exam(); |
| 16 | + exam1.addScore(student2, 8.25); |
| 17 | + |
| 18 | + //Si por ejemplo hay algún problema con un exámen y hay que subir la nota a una |
| 19 | + //o varios estudiantes, es mucho mas claro y fácil de la forma correcta que la incorrecta |
| 20 | + } |
| 21 | + |
| 22 | + public static class IncorrectStudent{ |
| 23 | + private int id; |
| 24 | + private String name; |
| 25 | + private List<Double> scoreList; |
| 26 | + |
| 27 | + public IncorrectStudent(int id, String name) { |
| 28 | + this.id = id; |
| 29 | + this.name = name; |
| 30 | + scoreList = new ArrayList<>(); |
| 31 | + } |
| 32 | + |
| 33 | + public void addScore(double newScore){ |
| 34 | + scoreList.add(newScore); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static class CorrectStudent{ |
| 39 | + private int id; |
| 40 | + private String name; |
| 41 | + |
| 42 | + public CorrectStudent(int id, String name) { |
| 43 | + this.id = id; |
| 44 | + this.name = name; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static class Exam{ |
| 49 | + private Map<CorrectStudent, Double> scores; |
| 50 | + |
| 51 | + public Exam(){ |
| 52 | + scores = new HashMap<>(); |
| 53 | + } |
| 54 | + |
| 55 | + public void addScore(CorrectStudent student, Double score){ |
| 56 | + scores.put(student, score); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + //Reto |
| 61 | + //Si queremos guardar varios datos datos de libros y usuario, debemos crear una |
| 62 | + //clase para ambos, aunque queramos hacerlo mal aposta. |
| 63 | + |
| 64 | + public static class IncorrectLibrary { |
| 65 | + |
| 66 | + private List<Book> bookList; |
| 67 | + private List<User> userList; |
| 68 | + private List<Loan> loanList; |
| 69 | + |
| 70 | + public IncorrectLibrary(){ |
| 71 | + bookList = new ArrayList<>(); |
| 72 | + userList = new ArrayList<>(); |
| 73 | + loanList = new ArrayList<>(); |
| 74 | + } |
| 75 | + |
| 76 | + public void registerBook(String title, String author, int nCopies){ |
| 77 | + //Código para registrar un libro |
| 78 | + } |
| 79 | + |
| 80 | + public void registerUser(Integer id, String name, String email){ |
| 81 | + //Código para registrar un usuario |
| 82 | + } |
| 83 | + |
| 84 | + public void lendBook(Integer idUser, String bookName, boolean returnBook){ |
| 85 | + //Código para prestar un libro a un usuario |
| 86 | + } |
| 87 | + |
| 88 | + public static void returnBook(Integer idUser, String bookName){ |
| 89 | + //Código para prestar un libro a un usuario |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static class CorrectLibrary{ |
| 94 | + private List<Book> bookList; |
| 95 | + private List<User> userList; |
| 96 | + private List<Loan> loanList; |
| 97 | + |
| 98 | + public CorrectLibrary() { |
| 99 | + bookList = new ArrayList<>(); |
| 100 | + userList = new ArrayList<>(); |
| 101 | + loanList = new ArrayList<>(); |
| 102 | + } |
| 103 | + |
| 104 | + public void lendBook(Integer idUser, String bookName){ |
| 105 | + //Código para prestar un libro a un usuario |
| 106 | + } |
| 107 | + |
| 108 | + public void returnBook(Integer idUser, String bookName){ |
| 109 | + //Código para que un usuario devuelva un libro |
| 110 | + } |
| 111 | + |
| 112 | + public boolean registerUser(Integer id, String name, String email){ |
| 113 | + return userList.add(Register.registerUser(id, name, email)); |
| 114 | + } |
| 115 | + |
| 116 | + public boolean registerBook(String title, String author, int nCopies){ |
| 117 | + return bookList.add(Register.registerBook(title, author, nCopies)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static class Register{ |
| 122 | + |
| 123 | + public static Book registerBook(String title, String author, int nCopies){ |
| 124 | + //Código para registrar un libro |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + public static User registerUser(Integer id, String name, String email){ |
| 129 | + //Código para registrar un usuario |
| 130 | + return null; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public static class Book{ |
| 135 | + private String title; |
| 136 | + private String author; |
| 137 | + private Integer nCopies; |
| 138 | + |
| 139 | + public Book(String title, String author, Integer nCopies) { |
| 140 | + this.title = title; |
| 141 | + this.author = author; |
| 142 | + this.nCopies = nCopies; |
| 143 | + } |
| 144 | + |
| 145 | + public String getTitle() { |
| 146 | + return title; |
| 147 | + } |
| 148 | + |
| 149 | + public String getAuthor() { |
| 150 | + return author; |
| 151 | + } |
| 152 | + |
| 153 | + public Integer getnCopies() { |
| 154 | + return nCopies; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public static class User{ |
| 159 | + private int id; |
| 160 | + private String name; |
| 161 | + private String email; |
| 162 | + |
| 163 | + public User(int id, String name, String email) { |
| 164 | + this.id = id; |
| 165 | + this.name = name; |
| 166 | + this.email = email; |
| 167 | + } |
| 168 | + |
| 169 | + public int getId() { |
| 170 | + return id; |
| 171 | + } |
| 172 | + |
| 173 | + public String getName() { |
| 174 | + return name; |
| 175 | + } |
| 176 | + |
| 177 | + public String getEmail() { |
| 178 | + return email; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + public static class Loan{ |
| 183 | + private User user; |
| 184 | + private Book book; |
| 185 | + |
| 186 | + public Loan(User user, Book book) { |
| 187 | + this.user = user; |
| 188 | + this.book = book; |
| 189 | + } |
| 190 | + |
| 191 | + public User getUser() { |
| 192 | + return user; |
| 193 | + } |
| 194 | + |
| 195 | + public Book getBook() { |
| 196 | + return book; |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments