BM

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.6 kB
3
Indexable
Never
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<>();
    }

    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;
            }
        }
        this.books.add(book);
        System.out.println("Added successfully.");
        return true;
    }

    /**
     * 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()) {
                return books.get(i);
            }
        }
        System.out.println("Invalid ID!");
        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.");
                return;
            }
        }
        System.out.println("Invalid ID!");
        return;
    }

    /**
     * update this.books to be sorted by price from high -> low
     */
    public void sortDescByPrice () {
        // TODO: your code here
        if (this.books.size() == 0) {
            System.out.println("(empty)");
        }
        boolean checkout = false;
        do {
            checkout = false;
            for(int i=0; i<this.books.size()-1; i++) {
                if(books.get(i).getPrice() < books.get(i+1).getPrice()) {
                    Collections.swap(books, i, i+1);
                    checkout = true;
                }
            }
        } while (checkout);
        for(int i=0; i<this.books.size(); i++) {
            System.out.println(this.books.get(i).toString());
        }
    }

    /**
     * return all books having name contains keyword (case in-sensitive)
     */
    public ArrayList<Book> searchByName (String keyword){
        // TODO: your code here
        ArrayList<Book> matches = new ArrayList<>();
        if(this.books.size() == 0) {
            System.out.println("(empty)");
        }
        for (int i=0; i<this.books.size(); i++) {
            if(books.get(i).getName().equalsIgnoreCase(keyword)) {
                matches.add(books.get(i));
            }
        }
        return matches;
    }

    /**
     * write this.books to file books.txt in required format
     */
    public void saveToFile () throws FileNotFoundException {
            // TODO: your code here
        PrintWriter writer = new PrintWriter("books.txt");
        //writer.
    }
}