0

So for a program im currently writing, there are different membership levels in the form of cards for customers and a coupon for each level depending on how long they've held the card for and how much they've spent in a year

Ive done the total spent portion but im having a big of trouble with the date difference.

Below are the two related classes.

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.util.*;

class Date {

    LocalDate createdDate = LocalDate.of(2000, Month.MAY, 19);
    LocalDate today = LocalDate.now();

    public void Date() {

        Period difference = Period.between(createdDate, today);
        int years = difference.getYears();

        System.out.println("The created date is: " + createdDate);
        System.out.println("Difference between created date and current date is: " + difference.getYears() + " years");

    } 
    }

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.util.*;

class silverCard {

    double coupon = 0;

    public void calCoup() {

        Date date = new Date();

        cardDetails card = new cardDetails(0001, "Adam Gong", 7000);

        if (card.getBalance() < 3500) {
            coupon = 0.04;
        } else if (card.getBalance() > 3500 && date.getYears() < 3){
            coupon = 0.05;
        }

        double totalCoupon = card.getBalance() - (card.getBalance() * coupon);

        System.out.println("Your coupon for silver card is: " + totalCoupon);
    }
}

The error comes from the line

} else if (card.getBalance() > 3500 && date.getYears() < 3){

in which the complier doesnt see the method getYears. Im aware that getYears in the date class isnt a method but im unsure of how i would call it from the silverCard class. Ive created a Date object and used it to call getYears. Ive also tried calling period.between and period difference to no luck

Thankyou :)

Andrew Van
  • 49
  • 7
  • 1
    This is what happens if you create classes with names that duplicate common Java classes. – Robby Cornelissen Aug 05 '19 at 10:15
  • 1
    @RobbyCornelissen ... and then import entire packages that include those common (and outdated) Java classes. – deHaar Aug 05 '19 at 10:16
  • Is it just me, or is anyone else missing the point in this custom `Date` class, too? It seems to be totally superfluent/obsolete and misleading, since it has two `LocalDate`s as its class attributes. – deHaar Aug 05 '19 at 10:30

2 Answers2

0

Rename your Date class because there is a Date class in java libraries already.

and add the method to this class:

public long getYears() {
    return Period.between(createdDate, today).getYears();
}
Andrzej Jaromin
  • 269
  • 2
  • 6
0

You can try out below solution . You can use SimpleDateFormat instead of LocalDate . You can get the difference in dates for diff variable.

public void getDiffBitweenTwoDates()
  throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
    Date firstDate = sdf.parse("08/15/2019");
    Date secondDate = sdf.parse("08/22/2019");

    long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
    long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);

}

If you still wants to go with LocalDateTime you can try out below way..

    LocalDateTime now = LocalDateTime.now();
    LocalDateTime mins= now.minusMinutes(10);

    Duration duration = Duration.between(now, mins);
    long diff = Math.abs(duration.toMinutes());
Lahiru Wijesekara
  • 623
  • 1
  • 10
  • 22