
Som bilden antyder... Jag ska använda en "class" fil och en main fil... Problemet jag har just nu är att jag inte vet var jag ska lägga if satserna som gör så att man går från tex januari 31 till 1feb om man väljer att gå "nextDate".
Här är koden för både main och classen..
main
Code
public class DateMain {
public static void main (String[] args)
{
Date d1 = new Date(2011, 01, 31);
Date d2 = new Date(2011, 03, 01);
System.out.println("Date objects d1 and d2 constructed");
System.out.println("Method setDate called indirectly");
System.out.println();
System.out.println("Using default date format");
System.out.println("d1.toString() = " + d1); // fixa
System.out.println("d2.toString() = " + d2); // fixa
System.out.println();
System.out.println("d1.getYear() = " + d1.getYear());
System.out.println("d1.getMonth() = " + d1.getMonth());
System.out.println("d1.getDay() = " + d1.getDay());
System.out.println();
//System.out.println("d1.compareTo(d2) = " + d1.compareTo(d2)); // fixa
//System.out.println("d2.compareTo(d1) = " + d2.compareTo(d1)); // fixa
//System.out.println("d1.compareTo(d1) = " + d1.compareTo(d1)); // fixa
}
}
class
Code
//implements comparable
public class Date
{
private int theYear;
private int theMonth;
private int theDay;
public Date()
{
this(0, 0, 0);
}
public Date(int y, int m, int d)
{
setDate(y, m, d);
}
public void setDate(int y, int m, int d)
{
theYear = y;
theMonth = m;
theDay = d;
}
public int getYear()
{
return theYear;
}
public int getMonth()
{
return theMonth;
}
public int getDay()
{
return theDay;
}
public String toString()
{
return (theYear + "-" + theMonth + "-" + theDay);
}
}
Mitt försök till classen
Code
public class Date implements Comparable<Date>
{
private int Day;
private int Month;
private int Year;
public void setDate( int theYear, int theMonth, int theDay)
{
Year = ( theYear >= 0000 && theYear <= 9999) ? theYear : 0;
Month =( theMonth >=1 && theMonth <= 12) ? theMonth : 0;
Day = ( theDay >=1 && theDay <= 31) ? theDay : 0;
if ((Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month 12) && Day >32)
{
Day - 31 = Day;
Month + 1 = Month;
}
else if (Month == 4 || Month == 6 || Month == 9 || Month == 11) && Day >31)
{
Day - 30 = Day;
Month + 1 = Month;
}
else if (Month == 2 && Day > 29)
{
Day - 28 = Day;
Month + 1 = Month;
}
if (Month > 12)
{
Year + 1 = Year;
}
}
int getYear()
{
return Year;
}
int getMonth()
{
return Month;
}
int getDay()
{
return Day;
}
Date NextDate()
{
Date nxt = new Date();
nxt.Day = Day + 1;
return nxt;
}
Date prevDate()
{
Date prv = new Date();
prv.Day = Day - 1;
return prv;
}
public int compareTo(Date theDate)
{
if (Date() < theDate())
{
return -1;
} else if (Date() > theDate())
{
return +1;
} else (Date() == theDate())
{
return 0;
}
}
String toString()
{
// Svenska
return "("+ Year + "-" + Month + "-" + Day + ")";
// US
return "("+ Month + "/" + Day + "/" + Year + ")";
}}
Som ni ser vet jag inte var jag ska lägga if satsen :S
This post was edited by bomben on Feb 1 2015 01:41pm