BookManager

 avatar
unknown
plain_text
3 years ago
4.3 kB
7
Indexable
import java.io.*;
import java.util.*;

public class BookManager {
    // TODO: your code here
    // attribute books
    ArrayList<Book> books;

    public BookManager() {
        // TODO: your code here
        this.books = new ArrayList<Book>();
    }

    public ArrayList<Book> getBooks() {
        // TODO: your code here
        return books;
    }

    /**
     * update this.books by reading books from file books.txt
     */
    public void loadFromFile() {
        // TODO: your code here
        try {
            File file = new File("books.txt");
            Scanner scan = new Scanner(file);
            System.out.println("Loading books...");
            String book;
            while (scan.hasNextLine()) {
                book = scan.nextLine();
                if (book.length()>50) {
                    books.add(new Book(Integer.parseInt(book.substring(0, 6).trim()), book.substring(6, 51).trim(), Double.parseDouble(book.substring(50).trim())));
                }
            }
        } catch(FileNotFoundException e) {
            System.out.println("Books not found");
        }
    }

    /**
     * print books (one/line) in required format
     */
    public void printBooks(ArrayList<Book> books) {
        // TODO: your code here
        if (this.books.size() == 0) {
            System.out.println("(empty)");
        } else {
            String heading = String.format("%-5s %-45s %-10s", "ID", "Name", "Price");
            System.out.println(heading);
            for (int i=0; i<books.size(); i++) {
                System.out.println(books.get(i).toString());
           }
        }
    }

    /**
     * if book.id is not duplicated, add book to this.books
     * return whether added or not
     */
    public boolean add(Book book) {
        // TODO: your code here

        for (int i=0; i<this.books.size(); i++) {
            if (books.get(i).getId() == book.id) {
                System.out.println("Duplicated ID!");
                return false;
            } else {
                this.books.add(book);
                System.out.println("Added successfully.");
                return true;
            }
        }
        return false;
    }

    /**
     * return book specified by id, null if not found
     */
    public Book getBookById(int id) {
        // TODO: your code here

        for (int i=0; i<this.books.size(); i++) {
            if (id == books.get(i).getId()) {
                Scanner scan = new Scanner(System.in);
                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);
                this.books.add(book);
                System.out.println("Updated successfully.");
                return books.get(i);
            } else {
                System.out.println("Invalid ID!");
                return null;
            }
        }
            return null;
    }

    /**
     * remove book from this.books
     */
    public void remove(Book book) {
        // TODO: your code here

        for (int i = 0; i < this.books.size(); i++) {
            if (books.get(i).getId() == book.id) {
                this.books.remove(i);
                System.out.println("Deleted successfully.");
            } else if (books.get(i).getId() != book.id) {
                System.out.println("Invalid ID!");
            }
            break;
        }
    }

    /**
     * update this.books to be sorted by price from high -> low
     */
    public void sortDescByPrice () {
        // TODO: your code here
    }

    /**
     * return all books having name contains keyword (case in-sensitive)
     */
    public ArrayList<Book> searchByName (String keyword){
        ArrayList<Book> matches = new ArrayList<>();

        // TODO: your code here

        return matches;
    }

    /**
     * write this.books to file books.txt in required format
     */
    public void saveToFile () {
            // TODO: your code here
    }
}
Editor is loading...