המחלקה date

public class date

{

private int day;

private int year;

private int month;

public date(int day,int month, int year)

{

this.day=day;

this.year=year;

this.month=month;

}

public date(date d1)

{

this.day=d1.day;

this.year=d1.year;

this.month=d1.month;

}

public int getday()

{

return this.day;

}

public int getyear()

{

return this.year;

}

public int getmonth()

{

return this.month;

}

public void setday(int z)

{

this.day=z;

}

public void setyear(int z)

{

this.year=z;

}

public void setmonth(int z)

{

this.month=z;

}

public String toString()

{

String str="(<"+day+">,"+"<"+month+">,"+"<"+year+">)";

return str;

}

}


המחלקה passport

public class passport

{

private String name;

private int number;

private date expirydate;

public passport(String name, int number, date expirydate )

{

this.name=name;

this.number=number;

this.expirydate= new date(expirydate);

}

public passport(passport p1 )

{

this.name=p1.name;

this.number=p1.number;

this.expirydate= new date(p1.expirydate);

}

public String toString()

{

String str="name:"+name+" pass.num: "+number+" Exp date: "+expirydate;

return str;

}

public boolean isValid (date dateCheked)

{

boolean check=true;

if ((dateCheked.getyear()- this.expirydate.getyear())<0)

{

return check;

}

else if ((dateCheked.getmonth()- this.expirydate.getmonth())<0)

{

return check;

}

else if ((dateCheked.getday()- this.expirydate.getday())<=0)

{

return check;

}

check=false;

return check;

}

public void setExpiryDate(date d1)

{

this.expirydate.setday(d1.getday());

this.expirydate.setmonth(d1.getmonth());

this.expirydate.setyear(d1.getyear());

}

}


המחלקה traveler

public class traveler

{

private passport passport;

private boolean hasPaid;

public traveler(passport passport, boolean hasPaid )

{

this.passport=new passport(passport);

this.hasPaid=hasPaid;

}

public void pay()

{

this.hasPaid=true;

}

boolean hasPaid()

{

return hasPaid;

}

public String toString()

{

String str= passport+"Has paid:"+hasPaid;

return str;

}

public boolean checkTravel(date travelDate)

{

boolean check=false;

if ((this.hasPaid) &(this.passport.isValid(travelDate)) )

check=true;

return check;

}

}

תכנית ראשית

import java.util.*;

public class run

{

/**

* @param args

*/

public static void main(String[] args)

{

System.out.print("הכנס שם");

Scanner in = new Scanner(System.in);

String name=in.next();

System.out.print("הכנס מספר דרכון");

int number= in.nextInt();

System.out.print("הכנס תאריך תפוגה: יום");

int dateDay=in.nextInt();

System.out.print("הכנס תאריך תפוגה: חודש");

int dateMonth=in.nextInt();

System.out.print("הכנס תאריך תפוגה: שנה");

int dateYear=in.nextInt();

date expiryDate= new date(dateDay,dateMonth, dateYear);

passport passport= new passport(name, number, expiryDate);

boolean hasPaid=true;

traveler traveler=new traveler(passport, hasPaid);

System.out.print(traveler);

}

}