-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryGUI.java
More file actions
173 lines (147 loc) · 5.99 KB
/
LibraryGUI.java
File metadata and controls
173 lines (147 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package Books;
import java.util.Scanner;
/*
* Se encarga de interactuar directamente con el usuario y el resto de objetos.
*
*/
public class LibraryGUI {
public static final String[] comandos = {"ver_libros","vender_libro","ver_libro","ver_autor","ingresos", "agregar_libro", "agregar_autor", "salir"};
private Library lib = new Library();
private Scanner inputDetector;
public LibraryGUI(Scanner in) {
inputDetector = in;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
LibraryGUI Interface = new LibraryGUI(in);
Interface.askLoadDirs();
while(true) {
System.out.print("Ingrese la operacion a realizar: ");
String command = in.nextLine();
if(command.equals("ver_libros")) Interface.displayBooks();
else if(command.equals("vender_libro")) Interface.displaySell();
else if(command.equals("ver_libro")) Interface.displayBook();
else if(command.equals("ingresos")) Interface.displayIncome();
else if(command.equals("ver_autor")) Interface.displayAuthor();
else if(command.equals("agregar_autor")) Interface.displayAddAuthor();
else if(command.equals("agregar_libro")) Interface.displayAddBook();
else if(command.equals("salir")) {
System.out.println("La sesión se cerró exitosamente.");
break;
}
}
in.close();
}
public void askLoadDirs() {
boolean loadSuccess = false;
while(!loadSuccess) {
System.out.print("Ingrese la direccion del archivo que contenga la informacion de los autores: ");
String actualDir = inputDetector.nextLine();
loadSuccess = lib.loadAuthors("src/Books/"+ actualDir);
}
loadSuccess = false;
System.out.println("Se cargo exitosamente el archivo");
while(!loadSuccess) {
System.out.print("Ingrese la direccion del archivo que contenga la informacion de los libros: ");
String actualDir = inputDetector.nextLine();
loadSuccess = lib.loadBooks("src/Books/"+ actualDir);
}
System.out.println("Se cargo exitosamente el archivo");
System.out.println("--------------------------------");
System.out.println("Comandos disponibles para usar la bibloteca: ");
for(String comando : comandos) {
System.out.println(comando);
}
System.out.println("--------------------------------");
}
public void displayBooks() {
Book[] allBooks = lib.showAllBooks();
System.out.print("\n\nActualmente la bibloteca dispone de los siguientes libros:\n\n");
for(Book book : allBooks) {
System.out.println(book.getName() + " by " + book.getAuthorNames());
}
}
public void displayBook() {
System.out.print("Ingrese el nombre del libro que busca: ");
String bookName = inputDetector.nextLine();
Book book = lib.findBook(bookName);
if(book == null) {
System.out.println("No se encontró el libro que buscabas.");
}
else {
System.out.println(" Titulo: " + book.getName());
System.out.println(" Autores: " + book.getAuthorNames());
System.out.println(" Precio: " + book.getPrice());
System.out.println(" Stock: " + book.getQty());
}
}
public void displaySell() {
System.out.print("Ingrese el nombre del libro que desea vender: ");
String bookName = inputDetector.nextLine();
Book book = lib.findBook(bookName);
if(book == null) System.out.println("No se encontró el libro que buscabas.");
else {
lib.sellBook(bookName);
System.out.println("Se vendio el libro titulado " + bookName + "a un precio de " + book.getPrice() + "[Unidades restantes : " + book.getQty() + "]");
}
}
public void displayIncome() {
System.out.println("La bibloteca generó " + lib.showEarnings() + "hasta el momento.");
}
public void displayAuthor() {
System.out.print("Ingrese el nombre del autor: ");
String authorName = inputDetector.nextLine();
Author author = lib.findAuthor(authorName);
if(author == null) System.out.println("No se encontró el autor que buscabas.");
else {
System.out.println(" Nombre completo: " + authorName);
System.out.println(" Email: " + author.getEmail());
System.out.println(" Genero: " + (author.getGender() == 'm' ? "Masculino" : "Femenino"));
}
}
public void displayAddAuthor() {
while(true) {
System.out.print("\n\nAgregar autor\n\n\n");
System.out.print("Ingrese el nombre completo: ");
String authorName = inputDetector.nextLine();
if(lib.findAuthor(authorName) == null) {
System.out.print("Ingrese el email: ");
String email = inputDetector.nextLine();
System.out.print("Ingrese el genero[m/f]: ");
String g = inputDetector.nextLine();
lib.addAuthor(authorName, email, g.charAt(0));
System.out.println("Se agrego el autor correctamente.");
break;
}
else {
System.out.println("Ya existe el autor que se desea agregar.");
System.out.print("Desea agregar otro autor?[S/N]");
String choice = inputDetector.nextLine();
if(choice.equals("N")) break;
}
}
}
public void displayAddBook() {
System.out.print("\n\nAgregar libro\n\n\n");
System.out.print("Ingrese el titulo: ");
String bookName = inputDetector.nextLine();
if(lib.findBook(bookName) != null) {
System.out.println("Ya existe un libro con ese titulo");
}
else {
System.out.print("Ingrese los nombres de los autores separados por coma: ");
String[] authors = inputDetector.nextLine().split(",");
Author[] foundAuthors = new Author[authors.length];
for(int i = 0; i < authors.length; i++) {
Author actualAuthor = lib.findAuthor(authors[i]);
if(actualAuthor != null) foundAuthors[i] = actualAuthor;
}
System.out.print("Ingrese el precio del libro: ");
double price = Double.parseDouble(inputDetector.nextLine());
System.out.print("Ingrese el nuevo stock: ");
int stock = Integer.parseInt(inputDetector.nextLine());
lib.addBook(bookName, foundAuthors, price, stock);
System.out.println("Libro agregado correctamente.");
}
}
}