Reputation: 33
I have a homework and here is my task:
Create an object class which has:
The deadline cannot be on Saturday or Sunday - if it happens, the (IllegalArgumentException) exception with a message about inappropriate date (in the format dd.mm.yyyy) is thrown out. Demonstrate the method using appropriate and inappropriate deadlines. Hint: use the getDayOfWeek method to show the day of the week.
I have a problem a really do not understand how to use getDayOfWeek method properly and of course I tried do program from this side but my output is: Deadline@2d554825
I already tried use this method but I really do not understand which datatype it needs to return
public DayOfWeek getDayOfWeek() {
// what should I return?
}
Here is my code:
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Deadline {
private LocalDate deadline;
public Deadline(LocalDate DeadLine) {
this.deadline = DeadLine;
}
public LocalDate getDeadline() {
return deadline;
}
public void setDeadline(LocalDate deadline) {
this.deadline = deadline;
}
public static void main(String[] args){
Deadline first = new Deadline(LocalDate.parse("2017-02-03"));
System.out.println(first);
}
}
Upvotes: 1
Views: 3894
Reputation: 18558
What you need is a proper toString()
method and a localized name of the day of week.
You can achieve that by using the method getDisplayName(TextStyle, Locale)
, which I will show in the code below.
There is another thing that comes up reading your assignment task:
The deadline cannot be on Saturday or Sunday - if it happens, the (IllegalArgumentException) exception with a message about inappropriate date (in the format dd.mm.yyyy) is thrown out.
==> There is not IllegalArgumentException
being thrown, there isn't even any check for invalid weekdays in your code. This Exception
will not magically appear, you have to implement it.
Here is some example solution:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
public class Deadline {
private LocalDate deadline;
public Deadline(LocalDate deadLine) {
// check if the given date is a Saturday or Sunday and throw the desired Exception
if (deadLine.getDayOfWeek() == DayOfWeek.SATURDAY
|| deadLine.getDayOfWeek() == DayOfWeek.SUNDAY) {
throw new IllegalArgumentException("The deadline to be set is not valid ("
+ deadLine.getDayOfWeek()
.getDisplayName(TextStyle.FULL_STANDALONE, Locale.getDefault())
+ ", "
+ deadLine.format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))
+ "). Please provide a deadline which is a weekday.");
} else {
// otherwise just set the deadline
this.deadline = deadLine;
}
}
public LocalDate getDeadline() {
return deadline;
}
public void setDeadline(LocalDate deadline) {
if (deadline.getDayOfWeek() == DayOfWeek.SATURDAY
|| deadline.getDayOfWeek() == DayOfWeek.SUNDAY) {
throw new IllegalArgumentException("The deadline to be set is not valid ("
+ deadline.getDayOfWeek()
.getDisplayName(TextStyle.FULL_STANDALONE, Locale.getDefault())
+ ", "
+ deadline.format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))
+ "). Please provide a deadline which is a weekday.");
} else {
this.deadline = deadline;
}
}
@Override
public String toString() {
return deadline.getDayOfWeek()
.getDisplayName(TextStyle.FULL_STANDALONE, Locale.getDefault())
+ ", "
+ deadline.format(DateTimeFormatter.ISO_LOCAL_DATE);
}
public static void main(String[] args) {
// this is now a Saturday, which will throw the IllegalArgumentException
Deadline first = new Deadline(LocalDate.parse("2017-02-04"));
System.out.println(first.toString());
}
}
Please note that you don't necessarily have to use the localized display name of the enum DayOfWeek
, but it may be useful to do so. You can also just call setDeadline(deadline)
in your parametrized constructor instead of writing the same error handling there, but if you decide not to do, you will have to keep the redundant code.
Upvotes: 0
Reputation: 339
First of all, if you want to know name of day of week, do it like this:
LocalDate a = LocalDate.parse("2017-02-03");
System.out.println(a.getDayOfWeek().name());
This way you can compare given day of week with String such as "SATURDAY" or "SUNDAY".
Second, if you want to do
Deadline first = new Deadline(LocalDate.parse("2017-02-03"));
System.out.println(first);
You need to Override @ToString in your Deadline class. For example:
class Deadline {
...
@Override
public String toString() {
return this.deadline.toString();
}
}
Upvotes: 1