-7

I have an assignment about methods, and 2 variables of Date type:

  • _borrowedDate(which is related to a Date method as well)
  • _returnDate (which is also a Date type)

I also have an int MAX_DAYS = 30

I need to set up a borrowdate, then accept a returndate, and if the days of the return date are greater then 30 (MAX_DAYS), compared to the borrowed date, I'm supposed to output true.

public class Book {
    private String _title;
    private String _author;
    private int _yearPublished;
    private int _noOfPages;
    private boolean _borrowed;
    private String _studentName;
    private Date _borrowedDate;
    private Date _returnDate;
    final int MAX_DAYS = 30;
    final int FINE = 5;
    final int MIN_YEAR =1800;
    final int MAX_YEAR = 2018;
    final int DEFAULT_YEAR = 2000;
    int d1;
    int days;

    public Book( String title, String author, int yearPublished, int noOfPages)
    {
        if (yearPublished <MIN_YEAR || yearPublished >MAX_YEAR){
            _yearPublished = DEFAULT_YEAR;
            _title = title;
            _author = author;
            _noOfPages = noOfPages;}
        else{
            _title = title;
            _author = author;
            _yearPublished = yearPublished;
            _noOfPages = noOfPages;

        }
    }

    public Book ( Book other) {
        _title = other._title;
        _author = other._author;
        _yearPublished = other._yearPublished;
        _noOfPages = other._noOfPages;
        _borrowed = other._borrowed;
        _studentName = other._studentName;
        _borrowedDate = other._borrowedDate;
        _returnDate = other._returnDate;

    }

    public boolean equal(Book compare){
        return ((_title == compare._title)&&(_author==compare._author)&&(_yearPublished==compare._yearPublished)
            &&(_noOfPages==compare._noOfPages)&&(_borrowed==compare._borrowed)&&(_studentName==compare._studentName)
            &&(_borrowedDate==compare._borrowedDate)&&(_returnDate==compare._returnDate));
    }

    boolean olderBook(Book other){
        return other._yearPublished > _yearPublished;
    }

    public int getYear(){
        return _yearPublished;
    }

    public int getPages(){
        return _noOfPages;
    }

    public String getTitle(){
        return _title;
    }

    public String getAuthor(){
        return _author;
    }

    public boolean getBorrowed(){
        return _borrowed;
    }

    public String toString(){
        return "Title:"+_title+"Author:"+_author+ "Year:"+_yearPublished+","+_noOfPages+"pages";
    }

    private  int calculateDate(int day, int month,int year){
        if(month<3){
            year--;
            month += 12;
        }
        return  365*year+year/4-year/100+year/400+((month+1)*306)/10+(day-62);
    }

    public void borrowBook(String name, Date d){
        _studentName = name;
        _borrowedDate = new Date(d);
        _borrowed = true;
    }

    public boolean returnBook(Date d){
        _returnDate = new Date(d);
        _studentName = null;

        return (_returnDate.after(_borrowedDate));
    }

    public int howLongBorrowed(Date d){
        return 5;   
    }

    public void setYear(int n){
        if(n > MAX_YEAR || n<MIN_YEAR){
            _yearPublished = _yearPublished;
        }
        else
            _yearPublished = n;
    }

    public Date getBorrowedDate(){
        return _borrowedDate;   
    }

    public String getStudentName(){
        return _studentName;
    }

    public Date getReturnDate(){
        return _returnDate;
    }

    public void setTitle(String s){
        _title = s;
    }

    public void setAuthor(String s){
        _author = s;
    }

    public void setPages(int n){
        _noOfPages = n;
    }

    public boolean sameAuthor(Book other){
        return other._author.equals(_author);
    }
}

The code is yet been completed, just having trouble with this specific thing. I've tried the obvious >, which didn't work because of different variable types.

.after works but I need to specifically do 30 days after, and that's about it.

whatever
  • 110
  • 4
  • 3
    Welcome to Stack Overflow! Please take the [tour], look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Assignments aren't usually arbitrary; your instructor, tutorial, or course will have covered the necessary topics to make it possible for you to do this. **Review your course materials, class notes, etc.,** and try to do the work. *If* you run into a *specific* problem, research it thoroughly, [search thoroughly here](/help/searching), and if you're still stuck post your code and a description of the problem. People will be glad to help. – T.J. Crowder Mar 24 '18 at 09:08
  • 3
    Rather than *describing* your code, please show it *as* code - and show what you've tried so far, and what's gone wrong. You should also consider that a `Date` value is just "a number of milliseconds since the Unix epoch". Does your number of "days" really mean 24 hour periods? If so, are you intending to take the time of day into account? This sounds like the kind of system where you'd want an actual date ("March 24th 2018" etc) rather than a point in time. – Jon Skeet Mar 24 '18 at 09:09

1 Answers1

2

If you're using Java 8 or later, better use LocalDate instead of java.util.Date:

LocalDate today = LocalDate.now();
LocalDate later = today.plusDays(30);

Compare LocalDate objects with methods isBefore, isEqual, and isAfter.

If you're using earlier version or you just prefer using Date for some reason, you need a Calendar instance to do that:

Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 30);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
ailav
  • 186
  • 8
  • Thank you for your answer. But is there a way to to do it inside the method it self? for the public boolean return date for example. Thank you – Rondolf Gondolf Mar 24 '18 at 11:48
  • Please, explain which method exactly and what it should do, cause you didn't include the name and it's a little hard to understand what you're trying to achieve exactly. – ailav Mar 24 '18 at 12:21
  • First, i have to use Borrowbook. I set there Date object(from another class). then i use the returnbook method, which accepts another date object. And with that, i need to calculate the number days that have passed from the borrow book Date, and if its higher then MAX_DAYS(30), i need to return true. – Rondolf Gondolf Mar 24 '18 at 13:22
  • 1
    You have the method, you now know how to use the `after` method, as well as how to add days to your `Date` object. What have you tried till now to resolve your problem? – ailav Mar 24 '18 at 14:40
  • 1
    To be clear… There should no longer be any need to ever use `Date` & `Calendar` classes again. They are confusing, poorly designed, and troublesome. They were supplanted by the *java.time* classes with [JSR 310](https://jcp.org/en/jsr/detail?id=310). To inter-operate with old code, you can convert between the legacy and modern classes by calling new methods added to the old classes. – Basil Bourque Mar 25 '18 at 04:23
  • Thank you, ive managed to solve the issue. – Rondolf Gondolf Mar 30 '18 at 11:16