Main
unknown
plain_text
3 years ago
3.8 kB
4
Indexable
import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO: your code here //call method loadFromFile BookManager manager = new BookManager(); manager.loadFromFile(); //show Menu & get user inputs Scanner scan = new Scanner(System.in); int choice = 1; while (choice != 0) { try { System.out.println("---------"); System.out.println("1. list all books"); System.out.println("2. add a new book"); System.out.println("3. edit book"); System.out.println("4. delete a book"); System.out.println("5. search books by name"); System.out.println("6. sort books descending by price"); System.out.println(""); System.out.println("0. save & exit"); System.out.println("---------"); System.out.print("Your option: "); choice = Integer.parseInt(scan.nextLine()); } catch (InputMismatchException e) { } switch (choice) { case 1: //printBooks manager.printBooks(manager.getBooks()); break; case 2: //add(Book book) System.out.print("Enter book id: "); int id = Integer.parseInt(scan.nextLine()); System.out.print("Enter book name: "); String name = scan.nextLine(); System.out.print("Enter book price: "); Double price = Double.parseDouble(scan.nextLine()); Book book = new Book(id, name, price); manager.add(book); break; case 3: //Book getBookById System.out.print("Enter book id: "); id = Integer.parseInt(scan.nextLine()); if (manager.getBookById(id) != null) { Book book3 = manager.getBookById(id); System.out.print("Enter book name: "); book3.name = scan.nextLine(); System.out.print("Enter book price: "); book3.price = Double.parseDouble(scan.nextLine()); System.out.println("Updated successfully."); } break; case 4: //remove(Book book) System.out.print("Enter book id: "); id = Integer.parseInt(scan.nextLine()); name = ""; price = 0.0; Book book4 = new Book(id, name, price); manager.remove(book4); break; case 5: //ArrayList<Book> System.out.print("Enter keyword: "); String keyword = scan.nextLine(); manager.printBooks(manager.searchByName(keyword)); break; case 6: //sortDescByPrice System.out.println("After sorting: "); manager.sortDescByPrice (); break; case 0: System.out.println("Saving to file..."); System.out.println("Bye!"); break; default: System.out.println("Invalid option!"); break; } } } }
Editor is loading...