Вы находитесь на странице: 1из 5

PEMROGRAMAN BERORIENTASI OBJEK LANJUTAN

TUGAS II

“LIBRARY BOOK”
Dosen: Dr. Sawaluddin, M.IT

Disusun oleh:

Muhammad Yuda Pratama(161402019)

Steven Leonardy (161402073)

Muhammad Romzi Humam (161402085)

PROGRAM STUDI TEKNOLOGI INFORMASI

FAKULTAS ILMU KOMPUTER DAN TEKNOLOGI INFORMASI

UNIVERSITAS SUMATERA UTARA

2018
/*
Chapter 7 Sample Development: Library Overdue Checker
File: BookTracker.java
*/
import java.util.*;
class BookTracker {
public static final int ERROR = -1;
private List<LibraryBook> books;
public BookTracker(){ books = new LinkedList<LibraryBook>(); }
public void add(LibraryBook book){ books.add(book); }
public double getCharge(){
return getCharge(new GregorianCalendar()); //set today as due date
}
public double getCharge(GregorianCalendar returnDate){
if (books.isEmpty()) {
return ERROR;
} else {
return totalCharge(returnDate);
}
}
public String getList(){
StringBuffer result = new StringBuffer("");
String lineSeparator = System.getProperty("line.separator");
for (LibraryBook book: books){
result.append(book.toString() + lineSeparator);
}
return result.toString();
}
private double totalCharge(GregorianCalendar returnDate){
double totalCharge = 0.0;
for (LibraryBook book: books){
totalCharge += book.computeCharge(returnDate);
}
return totalCharge;
}
}

/*
Chapter 7 Library Overdue Checker
Step 1 LibraryBook class
File: LibraryBook.java
*/
import java.util.*;
class LibraryBook {
private static final double CHARGE_PER_DAY = 0.50;
private static final double MAX_CHARGE = 50.00;
private static final String DEFAULT_TITLE = "Title unknown";
private static final double MILLISEC_TO_DAY = 1.0 / 1000 / 60 / 60 / 24;
private GregorianCalendar dueDate;
private String title;
private double chargePerDay;
private double maximumCharge;
public LibraryBook(GregorianCalendar dueDate){
this(dueDate, CHARGE_PER_DAY);
}
public LibraryBook(GregorianCalendar dueDate, double chargePerDay){
this(dueDate, chargePerDay, MAX_CHARGE);
}
public LibraryBook(GregorianCalendar dueDate, double chargePerDay, double maximumCharge){
this(dueDate, chargePerDay, maximumCharge, DEFAULT_TITLE);
}
public LibraryBook(GregorianCalendar dueDate, double chargePerDay, double maximumCharge, String title){
setDueDate(dueDate); setChargePerDay(chargePerDay); setMaximumCharge(maximumCharge); setTitle(title);
}
public double getChargePerDay(){ return chargePerDay; }
public GregorianCalendar getDueDate(){ return dueDate; }
public double getMaxCharge(){ return maximumCharge; }
public String getTitle(){ return title; }
public void setChargePerDay(double charge){ chargePerDay = charge; }
public void setDueDate(GregorianCalendar date){ dueDate = date; }
public void setMaximumCharge(double charge){ maximumCharge = charge; }
public void setTitle(String title){ this.title = title; }
public String toString(){ return String.format( "%-30s $%5.2f $%7.2f %4$tm/%4$td/%4$ty", getTitle(),
getChargePerDay(), getMaxCharge(), dueDate.getTime()); }
/*
public double computeCharge( GregorianCalendar returnDate){
return 1.00; //Stub method for Step 2
}
*/
public double computeCharge(GregorianCalendar returnDate){
double charge = 0.0;
long dueTime = dueDate.getTimeInMillis();
long returnTime = returnDate.getTimeInMillis();
long diff = returnTime - dueTime;
if (diff > 0){
charge = chargePerDay * diff * MILLISEC_TO_DAY;
if (charge > maximumCharge){
charge = maximumCharge;
}
}
return charge;
}
}

/*
Chapter 7 Library Overdue Checker
*/
import java.util.*;
class OverdueChecker {
private static enum Response {YES, NO}
private static final String DATE_SEPARATOR = "/";
private Scanner scanner;
private BookTracker bookTracker;
public OverdueChecker() {
scanner = new Scanner(System.in);
scanner.useDelimiter(System.getProperty("line.separator"));
bookTracker = new BookTracker();
}
public static void main(String[] args){
OverdueChecker checker = new OverdueChecker();
checker.start();
}
public void start(){
GregorianCalendar returnDate;
String table;
double charge;
Response response;
inputBooks();
table = bookTracker.getList();
System.out.println(table);
System.out.println("\nNow check the over due charges...\n");
//try different return dates
do {
//read return date
returnDate = readDate("\nReturn Date: ");
charge = bookTracker.getCharge(returnDate);
displayTotalCharge(charge);
response = prompt("\nRun Again (yes/no)? ");
} while (response == Response.YES);
System.out.println( "\n\nThank you for using Library Overdue Checker");
}
private LibraryBook createBook(String title, double chargePerDay, double maxCharge, GregorianCalendar
dueDate){
if (dueDate == null){
dueDate = new GregorianCalendar(); //set today as due date
}
LibraryBook book = new LibraryBook(dueDate);
if (title.length() > 0){ book.setTitle(title); }
if (chargePerDay > 0.0){ book.setChargePerDay(chargePerDay); }
if (maxCharge > 0.0){ book.setMaximumCharge(maxCharge); }
return book;
}
private void display(String text){ System.out.print(text); }
private void displayTotalCharge(double charge){
System.out.format("\nTOTAL CHARGE:\t $%8.2f", charge);
}
private void inputBooks(){
double chargePerDay, maxCharge;
String title;
GregorianCalendar dueDate;
LibraryBook book;
//Keeps on reading input from a console //until stopped by the end user
while (isContinue()) {
System.out.println("\n");
title = readString("Title : ");
chargePerDay = readDouble("Charge per day: ");
maxCharge = readDouble("Maximum charge: ");
dueDate = readDate ("Due Date : ");
book = createBook(title, chargePerDay, maxCharge, dueDate);
bookTracker.add(book);
}
}
private boolean isContinue() {
Response response = prompt("\nMore books to enter (y/n)?");
return (response == Response.YES);
}
private Response prompt(String question){
String input;
Response response = Response.NO;
System.out.print(question + " (Yes - y; No - n): ");
input = scanner.next();
if (input.equals("Y") || input.equals("y")) { response = Response.YES; }
return response;
}
private double readDouble(String prompt){
display(prompt);
return scanner.nextDouble();
}
private GregorianCalendar readDate( String prompt){
GregorianCalendar cal;
String yearStr, monthStr, dayStr, line;
int sep1, sep2;
display(prompt);
line = scanner.next();
if (line.length() == 0){ cal = null; } else {
sep1 = line.indexOf(DATE_SEPARATOR);
sep2 = line.lastIndexOf(DATE_SEPARATOR);
monthStr = line.substring(0, sep1);
dayStr = line.substring(sep1 + 1, sep2);
yearStr = line.substring(sep2 + 1, line.length());
cal = new GregorianCalendar(Integer.parseInt(yearStr), Integer.parseInt(monthStr)-1,
Integer.parseInt(dayStr));
}
return cal;
}
private String readString(String prompt){
display(prompt);
return scanner.next();
}
}

Вам также может понравиться