كلاس productرا براي نگهداري اطلاعات محصولات توليدي يك كارخانه بنويسيد كه در آن نام كالا، تاريخ توليد و
تاريخ انقضا نگهداري شود. از اين كلاس اشيائي با نامها و تاريخهاي تصادفي ساخته و مشخص كنيد كه كداميك از آنها
تاريخ مصرفشان گذشته و كدام را هنوز مي توان مصرف كرد.
package HW3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
*
*/
public class Product {

String name;
Date prdDate;
Date expDate;

public Product() {
}

public Product(String name) {
this.name = name;
}

public Product(String name, Date prdDate, Date expDate) {
this.name = name;
this.prdDate = prdDate;
this.expDate = expDate;
}

public boolean IsExpired() {
if (expDate.compareTo(Date.getToday()) == 1) {//
return true;
}
return false;
}

void Put() {
System.out.println("\nProductName:"+name);
System.out.println(prdDate.toString());
System.out.println(expDate.toString());
}
}

class Date {

static Date getToday() {
Date today = new Date();
Calendar cl = new GregorianCalendar();
today.set(cl.get(Calendar.DAY_OF_MONTH), cl.get(Calendar.MONTH), cl.get(Calendar.YEAR));
return today;
}
int d, m, y;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//-----------------------------------------------------
// This Part Gets The Day & Mounth & Year Of Our Date .
//-----------------------------------------------------

void get() throws IOException {
System.out.print("Please Enter The Day ");
String s = in.readLine();
d = Integer.parseInt(s);
while (d > 31 || d < 1) {
System.out.println("ERROR !! ");
System.out.println("Your Day Is Out Of Range , Please Enter Another Value ");
s = in.readLine();
d = Integer.parseInt(s);
}
System.out.print("Please Enter The Mounth ");
s = in.readLine();
m = Integer.parseInt(s);
while (m > 12 || m < 1) {
System.out.println("ERROR !! ");
System.out.println("Your Mounth Is Out Of Range , Please Enter Another Value ");
s = in.readLine();
m = Integer.parseInt(s);
}
System.out.print("Please Enter The Year ");
s = in.readLine();
y = Integer.parseInt(s);
while (y < 1370) {
System.out.println("ERROR !! ");
System.out.println("Your Year Is Out Of Range , Please Enter Another Value ");
s = in.readLine();
y = Integer.parseInt(s);
}
}

@Override
public String toString() {
return y + "/" + m + "/" + d;
}
//----------------------------
// This Part Print The Date !!
//----------------------------


void put() {
System.out.println("Date: " + d + " - " + m + " - " + y);
}
//-------------------------------------------
// This Part Compare 2 Date With Each Other .
//-------------------------------------------

int compareTo(Date b) {
int x = 0;
if (y < b.y) {
x = 1;
return x;
}
if (y > b.y) {
x = 2;
return x;
}
if (y == b.y) {
if (m < b.m) {
x = 1;
return x;
}
if (m > b.m) {
x = 2;
return x;
}
if (m == b.m) {
if (d < b.d) {
x = 1;
return x;
}
if (d > b.d) {
x = 2;
return x;
}
if (d == b.d) {
x = 0;
return x;
}
}

}
return x;
}
//--------------------------------------
//This Part Set A Date With Your Value .
//--------------------------------------

void set(int D, int M, int Y) {
d = D;
m = M;
y = Y;
}

void Date(int D, int M, int Y) {
set(D, M, Y);
}
//-----------------------------
//This Part Set Add Up 2 Date .
//-----------------------------

void add(Date A) {
int i = 0, j = 0;
d = A.d + d;
while (d > 31) {
d = d - 30;
i++;
}
m = m + i;
m = A.m + m;
while (m > 12) {
m = m - 12;
j++;
}
y = y + j;
y = A.y + y;
}
//---------------------------
//This Part subtract 2 Date .
//---------------------------

void sub(Date B) {
if (y > B.y) {
if (d > B.d) {
d = d - B.d;
}
if (d < B.d) {
m--;
d = d + 31;
d = d - B.d;
}
if (m > B.m) {
m = m - B.m;
}
if (m < B.m) {
y--;
m = m + 12;
m = m - B.m;
}
}
if (y < B.y) {
if (B.d > d) {
B.d = B.d - d;
}
if (B.d < d) {
B.m--;
B.d = B.d + 31;
B.d = B.d - d;
}
if (B.m > m) {
B.m = B.m - m;
}
if (B.m < m) {
B.y--;
B.m = B.m + 12;
B.m = B.m - m;
}
}
if (y == B.y) {
if (m > B.m) {
if (d > B.d) {
d = d - B.d;
}
if (d < B.d) {
m--;
d = d + 31;
d = d - B.d;
}
}
if (m < B.m) {
if (B.d > d) {
B.d = B.d - d;
}
if (B.d < d) {
B.m--;
B.d = B.d + 31;
B.d = B.d - d;
}
}
if (m == B.m) {
if (d > B.d) {
d = d - B.d;
}
if (d < B.d) {
B.d = B.d - d;
}
if (d == B.d) {
d = 0;
m = 0;
y = 0;
}
}
}
}

public static Date generateRandomDate() {
Date newDate = new Date();
newDate.d = (int) Math.random() * 30;
newDate.m = (int) Math.random() * 12;
newDate.y = (int) Math.random() * 5 + 1388;
return newDate;
}
}