Created
February 28, 2024 10:53
-
-
Save ansisec/4fc66a3aa2fffaa6c24fba622b7c809d to your computer and use it in GitHub Desktop.
Aula 04 - Ficheiros Auxiliares
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Aula 04 - Ficheiros Auxiliares |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LibraryUI { | |
ILibrary lib; | |
public LibraryUI(ILibrary lib) { | |
this.lib = lib; | |
} | |
void addBook() { | |
String title = PAInput.readString("Book title: ",false); | |
String author; | |
ArrayList<String> authors = new ArrayList<>(); | |
do { | |
author = PAInput.readString("Name of one author [\'exit\' to finish]: ",false); | |
if (author!=null && !author.equalsIgnoreCase("exit")) | |
authors.add(author); | |
} while (!author.equalsIgnoreCase("exit")); | |
if (authors.isEmpty()) | |
authors.add("Author unknown"); | |
int id = lib.addBook(title,authors); | |
if (id<0) | |
System.out.println("Error adding this new book"); | |
else | |
System.out.printf("The ID of this new book is: %d\n",id); | |
} | |
void findBook() throws CloneNotSupportedException { | |
String title = PAInput.readString("Title of the book to search: ",false); | |
Book book = lib.findBook(title); | |
if (book == null) | |
System.out.println("Book not found"); | |
else | |
System.out.println("Book found: "+book); | |
} | |
void findBookById() throws CloneNotSupportedException { | |
int id = PAInput.readInt("Id of the book to search: "); | |
Book book = lib.findBook(id); | |
if (book == null) | |
System.out.println("Book not found"); | |
else | |
System.out.println("Book found: "+book); | |
} | |
void removeBook() { | |
String title = PAInput.readString("Title of the book to remove: ",false); | |
boolean deleted = lib.removeBook(title); | |
if (!deleted) | |
System.out.println("Book not found"); | |
else | |
System.out.println("Book deleted"); | |
} | |
void removeBookById() { | |
int id = PAInput.readInt("Id of the book to remove: "); | |
boolean deleted = lib.removeBook(id); | |
if (!deleted) | |
System.out.println("Book not found"); | |
else | |
System.out.println("Book deleted"); | |
} | |
public void start() throws CloneNotSupportedException { | |
while (true) { | |
switch (PAInput.chooseOption("LibraryList Manager - "+lib.getName(), | |
"Add new book", | |
"Search book (by title)", "Search book (by id)", | |
"Remove book (by title)", "Remove book (by id)", | |
"Show books", "Quit" | |
)) { | |
case 1: | |
addBook(); | |
break; | |
case 2: | |
findBook(); | |
break; | |
case 3: | |
findBookById(); | |
break; | |
case 4: | |
removeBook(); | |
break; | |
case 5: | |
removeBookById(); | |
break; | |
case 6: | |
System.out.println(lib.toString()); | |
break; | |
case 7: | |
return; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment