I need a little help writing the java files for the following UML diagram: In th
ID: 3710368 • Letter: I
Question
I need a little help writing the java files for the following UML diagram:
In the library file the checkin has to check in a book when it's returned. If it belongs in the library and it's being returned, it's status has to be updated, and the patron and due date has to be reset, as well as determining if any fines are due if it's overdue. This needs to update the patron's balance if there are any fines. The return for this method has to indicate success or failure. So, if the book is not in the library, it has to return false. The checkout portion has to allow for checkout with updated status and patron. It also sets the due date 10 days from today. The return also has to do the same thing as in check in. The fines for being overdue are 50 cents per day. The search books has to make use of the constants and return an array of the books found. If none, the array has to have a length of zero.
Driver +main(args Strin Librar AUTHOR SEARCH : char = 'A' +OVERDUE SEARCH : char= 'O' +PATRON SEARCH char'P +TITLE SEARCH: charT -books : Book -patrons : Patron[] +Library(books : Book [], patrons Patron []) +checkin(book : Book): boolean +checkout(book : Book, patron : Patron): boolean +determineFine(book Book): double +searchBooks(key : Object, type : char): Book [I DateUtils +interval(from Date, to date): int +addDays (date: Date, days: int) Date Book +AVAILABLE : int = 1 -UNAVAILABLE : int = 2 -title : String -author String -isbn String -pages : int -year int -status int -due Date -patron : Patron +Book(title : String, author : String, isbn String year : int, pages: int) +checkin) +checkout(patron : Patron, due : Date) +equals(other : Object) : boolean +getX0 +tostringo: String Patron -name String -email String -idNumber int balance double +Patron(name String, email : String, idNumber int, balance double) +adjustBalance(amount : double): double +equals(other : Object) : boolean +tostringo: StringExplanation / Answer
import java.util.Date;
/**
*
* @author pc
*/
public class Library {
public static final char AUTHOR_SEARCH ='A';
public static final char OVERDUE_SEARCH ='O';
public static final char PATRON_SEARCH ='P';
public static final char TITLE_SEARCH ='T';
private Book[] books;
private Patron[] patrons;
public Library(Book[] books,Patron[] patrons)
{
this.books=books;
this.patrons=patrons;
}
public boolean checkin(Book book)
{
for(int i=0;i<books.length;i++)
{
if(books[i].equals(book))
{
double x=determineFine(book);
book.getPatron().adjustBalance(x);
books[i].checkin();
return true;
}
}
return false;
}
public boolean checkout(Book book,Patron patron)
{
int i,j;
for(i=0;i<books.length;i++)
{
if(books[i].equals(book))
{
break;
}
}
for(j=0;j<patrons.length;j++)
{
if(patrons[j].equals(patron))
{
break;
}
}
if(i==books.length || j==patrons.length)
{
return false;
}
DateUtils d=new DateUtils();
Date date=new Date();
date=d.addDays(date, 10);
book.checkout(patron, date);
return true;
}
public double determineFine(Book book)
{
Date d=new Date();
DateUtils du=new DateUtils();
int x=du.interval(d,book.getDue());
return x*50;
}
public Book[] searchBooks(Object key,char type)
{
Book[] boks=new Book[1000];
int index=0;
for(int i=0;i<books.length;i++)
{
if(type==Library.AUTHOR_SEARCH && books[i].getAuthor().equalsIgnoreCase((String)key))
{
boks[index++]=books[i];
}
else if(type==Library.OVERDUE_SEARCH)
{
Date d=new Date();
DateUtils du=new DateUtils();
int days=du.interval(d, books[i].getDue());
if(days>0)
{
boks[index++]=books[i];
}
}
else if(type==Library.PATRON_SEARCH && books[i].getPatron().equals(key))
{
boks[index++]=books[i];
}
else if(type==Library.TITLE_SEARCH && books[i].getTitle().equalsIgnoreCase((String)key))
{
boks[index++]=books[i];
}
}
return boks;
}
}
public class Driver {
public static void main(String[] args) {
Book b1=new Book("C++","xyz","hed",209,1998);
Book b2=new Book("C","xyz","hed",229,1991);
Book b3=new Book("Android","xyz","hed",224,1980);
Book b4=new Book("Java","xyz","hed",201,1997);
Book[] books={b1,b2,b3};
Patron p1=new Patron("Shivam","Shivamatmail.com",12,29);
Patron p2=new Patron("Shivani","Shivani&atmail.com",17,500);
Patron p3=new Patron("Ram","Ramatmail.com",13,59);
Patron[] patrons={p1,p2};
Library l=new Library(books,patrons);
l.checkout(b1, p2);
l.checkout(b2, p1);
l.checkin(b1);
l.checkin(b2);
}
}
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
*
* @author pc
*/
public class DateUtils {
public int interval(Date from,Date to)
{
try{
return (int)ChronoUnit.DAYS.between(from.toInstant(), to.toInstant());
}catch(Exception e)
{
e.printStackTrace();
}
return 0;
}
public Date addDays(Date date,int days)
{
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DATE, days);
return cal.getTime();
}
}
import java.util.Objects;
/**
*
* @author pc
*/
public class Patron {
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Patron other = (Patron) obj;
if (this.idNumber != other.idNumber) {
return false;
}
if (Double.doubleToLongBits(this.balance) != Double.doubleToLongBits(other.balance)) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.email, other.email)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Patron{" + "name=" + name + ", email=" + email + ", idNumber=" + idNumber + ", balance=" + balance + '}';
}
private String name;
private String email;
private int idNumber;
private double balance;
public Patron(String name,String email,int idNumber,double balance)
{
this.name=name;
this.balance=balance;
this.email=email;
this.idNumber=idNumber;
}
public double adjustBalance(double amount)
{
this.balance=this.balance+amount;
return this.balance;
}
}
import java.util.Date;
import java.util.Objects;
/**
*
* @author pc
*/
public class Book {
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Book other = (Book) obj;
if (this.pages != other.pages) {
return false;
}
if (this.year != other.year) {
return false;
}
if (this.status != other.status) {
return false;
}
if (!Objects.equals(this.title, other.title)) {
return false;
}
if (!Objects.equals(this.author, other.author)) {
return false;
}
if (!Objects.equals(this.isbn, other.isbn)) {
return false;
}
if (!Objects.equals(this.due, other.due)) {
return false;
}
if (!Objects.equals(this.patron, other.patron)) {
return false;
}
return true;
}
public void checkin()
{
this.status=Book.AVAILABLE;
patron=null;
due=null;
}
public void checkout(Patron patron,Date due)
{
this.patron=patron;
this.due=due;
this.status=Book.UNAVAILABLE;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
public int getPages() {
return pages;
}
public int getYear() {
return year;
}
public int getStatus() {
return status;
}
public Date getDue() {
return due;
}
public Patron getPatron() {
return patron;
}
@Override
public String toString() {
return "Book{" + "title=" + title + ", author=" + author + ", isbn=" + isbn + ", pages=" + pages + ", year=" + year + ", status=" + status + ", due=" + due + ", patron=" + patron + '}';
}
public Book(String title, String author, String isbn, int pages, int year) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.pages = pages;
this.year = year;
}
public static final int AVAILABLE =1;
public static final int UNAVAILABLE=2;
private String title;
private String author,isbn;
private int pages;
private int year,status;
private Date due;
private Patron patron;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.