Last active
March 9, 2023 22:33
-
-
Save ansisec/e383b620660f9594bf93cb61ac1a3307 to your computer and use it in GitHub Desktop.
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
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 { | |
Library lib; | |
public LibraryUI(Library 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"); | |
lib.addBook(title,authors); | |
} | |
void findBook() { | |
int bookId = PAInput.readInt("ID of the book to search: "); | |
Book book = lib.findBook(bookId); | |
if (book == null) | |
System.out.println("Book not found"); | |
else | |
System.out.println("Book found: "+book); | |
} | |
void removeBook() { | |
int codigo = PAInput.readInt("ID of the book to remove: "); | |
boolean deleted = lib.removeBook(codigo); | |
if (!deleted) | |
System.out.println("Book not found"); | |
else | |
System.out.println("Book deleted"); | |
} | |
public void start() { | |
while (true) { | |
switch (PAInput.chooseOption("Library Manager - "+lib.getName(), | |
"Add new book","Search book","Remove book","Show books", | |
"Quit")) { | |
case 1: | |
addBook(); | |
break; | |
case 2: | |
findBook(); | |
break; | |
case 3: | |
removeBook(); | |
break; | |
case 4: | |
System.out.println(lib.toString()); | |
break; | |
case 5: | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment