Hey all. I am having some problems in my project work. I am trying to this 'transaction' class in which the user enters his personal details.Now all I want to do is that when I create a new account for the user, I must also know the date when the account was created so that if the account expiry after for eg. 3 months I issue this reminder. I think this has to do with the gregorian calander, but I don't Know how to even tackle it. Thanks dudes. Here is my code.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Transactions
{
     // create an empty list to hold Cars
     ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();

     public Transactions (ArrayList<GymUser> Tran)
     {
         UserListIn = Tran;
     }
     static void addTransaction(ArrayList<User> UserListIn)
    {
 String tempMemberShipID;
        String tempName;
        String tempSurname;
        String tempAge;
        String tempAddress;
        String tempPhone;
        String tempDuration;
        String tempFrom;
        String tempTo;
        String tempTypeOfMembership;
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details to process Transaction");
        System.out.println("----------------------------------------------------------------- ");
        System.out.println("Enter the membership number");
        tempMemberShipID = keyboard.next();
        System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.next();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.next();
        System.out.print("Type of membership:");
        tempTypeOfMembership = keyboard.next();
        System.out.print("Membership Duration:");
        tempDuration = keyboard.next();
        System.out.print("From:");
        tempFrom = keyboard.next();
        System.out.print("To:");
        tempTo = keyboard.next();

//adding in array list.
        UserListIn.add(new User(tempMemberShipID, tempName, tempSurname, tempAddress, tempPhone, tempAge,tempTypeOfMembership,tempDuration,tempFrom,tempTo));

}

//Now this is the class that contains constructor and methods.
import java.io.*;
public class User implements Serializable
{
        //these are instance variables
        private String m_UserrID;
        private String m_UserName;
        private String m_UserID;
        private String m_UserSurname;
        private String m_UserAddress;
        private String m_UserPhone;
        private String m_UserAge;
        private String m_UserType;
        private String m_UserDuration;
        private static String m_UserFrom;
        private String m_UserTo;
        //this is the constructor
        public User(String UserrID,String UserName,String UserSurname, String UserAddress, String UserPhone, String UserAge,String UserType,String UserDuration,String UserFrom,String UserTo)
        {
            m_UserrID =  UserrID;
            m_UserName = UserName;
            m_UserSurname = UserSurname;
            m_UserAddress = UserAddress;
            m_UserPhone = UserPhone;
            m_UserAge= UserAge;
            m_UserType = UserType;
            m_UserDuration = UserDuration;
            m_UserFrom = UserFrom;
            m_UserTo = UserTo;
        }


        public String getM_ID()
       {
           return m_UserrID;
       }
       // method to get the membership duration
       public String getDuration()
       {
           return m_UserDuration;
       }

       // method to get the name
       public String getName()
       {
           return m_UserName;
       }

       //method to set name
       public void setName(String UserName)
       { 
            m_UserName = UserName;
       }



        //method to return age
        public String getAge()
        {
            return m_UserAge;
            }

          //method to set age
        public void setAge(String UserAge)
        {
            m_UserAge =UserAge;
            }   


        //method to return address
        public String getAddress()
        {
            return m_UserAddress;
            }


         //method to set address
        public void setAddress(String UserAddress)
        {
            m_UserAddress=  UserAddress;
            }

        //method to return phone
        public String getPhone()
        {
            return m_UserPhone;
        }

        //method to set phone
        public void setPhone(String UserPhone)
        {
            m_UserPhone = UserPhone;
        }

        //method to return suname
        public String getSurname()
        {
            return m_UserSurname;
        }

        //method to set surname
        public void setSurname(String UserSurname)
        {
            m_UserSurname = UserSurname;
        }

        //method to get name
        public String getID()
        {
            return m_UserID;

        }

          public String getType()
       {
           return m_UserType;
       }

         public static String getFrom()
       {
           return m_UserFrom;
       }

         public String getTo()
       {
           return m_UserTo;
       }
}

Recommended Answers

All 3 Replies

1) use code tags
2) read the documentation for Calendar, it's quite good (and do NOT explicitly instantiate a GregorianCalendar, Java will pick the correct Calendar subclass for the Locale you're executing under automatically if you use the right factory method).

something like this you are looking for?

Calendar calExpired = Calendar.getInstance();
	calExpired.add(Calendar.MONTH, -3);
	
	Date date_lastlogin = null; //get it from your db or log file...
	Calendar cal_lastlogin = Calendar.getInstance();
	cal_lastlogin.setTime(date_lastlogin);
	if(cal_lastlogin.before(calExpired))
		System.out.println("date expired expired!");

First of all thanks already for your help.I don't know if I am tackling the problem correctly. I tried to alter the code, but I don't know if it is logically correct and the most important think if it works as required. Sorry not to include code tags but I still can't manage to use them.Promise that next time I'll try my best to use them. Thanks.

//class that contains constructor and methods.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class User implements Serializable
{
        //these are instance variables
        private String m_UserrID;
        private String m_UserName;
        private String m_UserID;
        private String m_UserSurname;
        private String m_UserAddress;
        private String m_UserPhone;
        private String m_UserAge;
        private String m_UserType;
        private String m_UserDuration;
        private static String m_UserFrom;
        private String m_UserTo;

        private GregorianCalendar calendar;

        //this is the constructor
        public User(String UserrID,String UserName,String UserSurname, String UserAddress, String UserPhone, String UserAge,String UserType,String UserDuration,String UserFrom,String UserTo)
        {
            m_UserrID =  UserrID;
            m_UserName = UserName;
            m_UserSurname = UserSurname;
            m_UserAddress = UserAddress;
            m_UserPhone = UserPhone;
            m_UserAge= UserAge;
            m_UserType = UserType;
            m_UserDuration = UserDuration;
            m_UserFrom = UserFrom;
            m_UserTo = UserTo;

         //   calendar = new GregorianCalendar ( da, m, y, hr, min);

        }


        public String getM_ID()
       {
           return m_UserrID;
       }
       // method to get the membership duration
       public String getDuration()
       {
           return m_UserDuration;
       }

       // method to get the name
       public String getName()
       {
           return m_UserName;
       }

       //method to set name
       public void setName(String UserName)
       { 
            m_UserName = UserName;
       }



        //method to return age
        public String getAge()
        {
            return m_UserAge;
            }

          //method to set age
        public void setAge(String UserAge)
        {
            m_UserAge =UserAge;
            }   


        //method to return address
        public String getAddress()
        {
            return m_UserAddress;
            }


         //method to set address
        public void setAddress(String UserAddress)
        {
            m_UserAddress=  UserAddress;
            }

        //method to return phone
        public String getPhone()
        {
            return m_UserPhone;
        }

        //method to set phone
        public void setPhone(String UserPhone)
        {
            m_UserPhone = UserPhone;
        }

        //method to return suname
        public String getSurname()
        {
            return m_UserSurname;
        }

        //method to set surname
        public void setSurname(String UserSurname)
        {
            m_UserSurname = UserSurname;
        }

        //method to get name
        public String getID()
        {
            return m_UserID;

        }

          public String getType()
       {
           return m_UserType;
       }

         public static String getFrom()
       {
           return m_UserFrom;
       }

         public String getTo()
       {
           return m_UserTo;
       }
}


import java.io.*;         // required for handling the IOExceptions
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.*;

class Transactions
{
     // create an empty list to hold Cars
     ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();

     public Transactions (ArrayList<GymUser> Tran)
     {
         UserListIn = Tran;
     }

     static void addTransaction(ArrayList<User> UserListIn)
    {
        String tempMemberShipID;
        String tempName;
        String tempSurname;
        String tempAge;
        String tempAddress;
        String tempPhone;
        String tempDuration;
        String tempFrom;
        String tempTo;
        String tempTypeOfMembership;

        String longDate;
        int date;
        int month;
        int year;
        int hour;
        int minute;



        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");

        System.out.println("Please enter the member's Personal Details to process Transaction");
        System.out.println("----------------------------------------------------------------- ");
        System.out.println("Enter the membership number");
        tempMemberShipID = keyboard.next();
        System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.next();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.next();
        System.out.print("Type of membership:");
        tempTypeOfMembership = keyboard.next();
        System.out.print("Membership Duration:");
        tempDuration = keyboard.next();
        System.out.print("From(YYYY-MM-dd): ");
        tempFrom = keyboard.next();

        //parsing date
        Date   newDate;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        System.out.print("  " + tempFrom + " parses as ");
        try { 
            newDate = formatter.parse(tempFrom);
           System.out.println(newDate + ".");
        } catch (ParseException e) {
             System.out.println("Unparseable using " + formatter + ".");
        }    


        System.out.print("To(DD/MM/YYYY): ");
        tempTo = keyboard.next();

        //parsing date
        Date   EndDate;
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
        System.out.print("  " + tempTo + " parses as ");
        try { 
            EndDate = formatter2.parse(tempTo);
           System.out.println(EndDate + ".");
        } catch (ParseException e) {
             System.out.println("Unparseable using " + formatter + ".");
        }    




        UserListIn.add(new User(tempMemberShipID, tempName, tempSurname, tempAddress, tempPhone, tempAge,tempTypeOfMembership,tempDuration,tempFrom,tempTo));


    }

//method of expiry 
    static void Expiry()
    {
        Calendar calExpired = Calendar.getInstance();
        calExpired.add(Calendar.MONTH, -3);

        Date EndDate = null; //get it from your db or log file...
        Calendar cal_lastlogin = Calendar.getInstance();
        if(cal_lastlogin.before(calExpired))
        System.out.println("Account to be Renewed!");
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.