티스토리 뷰

반응형

 

 

 

Hackerrank challenges Day 11을 진행해보자.

 

강의 소스코드부터 확인해보자.

package day11;

import java.util.HashMap;
import java.util.Map;

public class LibraryCatalogue {
    // Properties/Fields/Global Variables
    Map<String, Book> bookCollection = new HashMap<String, Book>();
    int currentDay = 0;
    int lengthOfCheckeoutPeriod = 7;
    double initialLateFee = 0.50;
    double feePerLateDay = 1.00;

    // Constructors
    public LibraryCatalogue(Map<String, Book> collection) {
        this.bookCollection = collection;
    }

    public LibraryCatalogue(Map<String, Book> collection, int lengthOfCheckoutPeriod,
                            double initialLateFee, double feePerLateDay) {
        this.bookCollection = collection;
        this.lengthOfCheckeoutPeriod = lengthOfCheckoutPeriod;
        this.initialLateFee = initialLateFee;
        this.feePerLateDay = feePerLateDay;
    }

    // Getters
    public int getCurrentDay() {
        return this.currentDay;
    }

    public Map<String, Book> getBookCollection() {
        return this.bookCollection;
    }

    public Book getBook(String bookTitle) {
        return getBookCollection().get(bookTitle);
    }

    public int getLengthOfCheckedOutPeriod() {
        return this.lengthOfCheckeoutPeriod;
    }

    public double getInitialLateFee() {
        return this.initialLateFee;
    }

    public double getFeePerLateDay() {
        return this.feePerLateDay;
    }

    // SETTERS
    public void nextDay() {
        currentDay++;
    }

    public void setDay(int day) {
        currentDay = day;
    }

    // INSTANCE METHODS;
    public void checkOutBook(String title) {
        Book book = getBook(title);
        if(book.getIsCheckedOut()) {
            sorryBookAlreadyCheckedOut(book);
        }else {
            book.setIsCheckedOut(true, currentDay);
            System.out.println("You just checked out " + title + ", It is due on day " +
                    (getCurrentDay() + getLengthOfCheckedOutPeriod()) + ".");
        }
    }

    public void returnBook(String title) {
        Book book = getBook(title);
        int daysLate = currentDay - (book.getDayCheckedOut() + getLengthOfCheckedOutPeriod());
        if(daysLate > 0) {
            System.out.println("You owe the library $" + (getInitialLateFee() + daysLate + getFeePerLateDay()) +
                    " because your book is " + daysLate + "days overdue.");
        }else {
            System.out.println("Book Returned. Thank you.");
        }
        book.setIsCheckedOut(false, -1);
    }

    public void sorryBookAlreadyCheckedOut(Book book) {
        System.out.println("Sorry, " + book.getTitle() + " is already checked out. "
        +"It should be back on day " + (book.getDayCheckedOut() + getLengthOfCheckedOutPeriod()) + ".");

    }

    public static void main(String[] args) {
        Map<String, Book> bookCollections = new HashMap<String, Book>();
        Book harry = new Book("HarryPotter", 100, 8888);
        bookCollections.put("HarryPotter", harry);
        LibraryCatalogue lib = new LibraryCatalogue(bookCollections);
        lib.checkOutBook("HarryPotter");
        lib.nextDay();
        lib.nextDay();
        lib.checkOutBook("HarryPotter");
        lib.setDay(17);
        lib.returnBook("HarryPotter");
        lib.checkOutBook("HarryPotter");
    }
}
package day11;

public class Book {
    // Properties, Fields, Global Variables
    String title;
    int pageCount;
    int ISBN;
    boolean isCheckedOut; // whether or not the book is checked out
    int dayCheckedOut = -1;

    public Book(String bookTitle, int bookPageCount, int bookISBN) {
        this.title = bookTitle;
        this.pageCount = bookPageCount;
        this.ISBN = bookISBN;
        this.isCheckedOut = false;
    }

    // Getters -> Instance Methods
    public String getTitle() {
        return this.title;
    }

    public int getPageCount() {
        return this.pageCount;
    }

    public int getISBN() {
        return this.ISBN;
    }

    public boolean getIsCheckedOut() {
        return this.isCheckedOut;
    }

    public int getDayCheckedOut() {
        return this.dayCheckedOut;
    }

    // SETTERS
    public void setIsCheckedOut(boolean newIsCheckedOut, int currentDayCheckedOut) {
        this.isCheckedOut = newIsCheckedOut;
        setDayCheckedOut(currentDayCheckedOut);
    }

    private void setDayCheckedOut(int day) {
        this.dayCheckedOut = day;
    }
}

 

오늘은 두 개의 파일이 필요하다.

책의 정보를 담고 있는 Book.java와

책의 대여 시스템 정보를 담고 있는

LibraryCatalogue.java의 파일이다.

 

연체가 되었을 때, 책을 반납하였을 때, 

책의 연체 기간 지정하는 등의 로직이 나와있다.

 

딱히 코드를 이해하는데에 어려움은 없으나

적은 코드가 아니므로

추후 복습이 필요할 것 같다.

 

 


 

 

이번(혹은 다음주) 주말을 활용해

블로그 내용들도 다시 한번 훑는 시간을 가져야겠다.

 

그럼 다음 포스팅에서 코드 리뷰로 만나자!

😊

반응형