View Full Version : Code Implementation At A Certain Date
WildChild
12-10-2006, 03:47 PM
I need to program some code that will compare the current month of the year with that of a customers birth date, if they are the same i want to discount £5 off their monthly bill.
Where would i start on this?
I think you are going to need to supply a bit more information.
Is this a homework assignment? If so, is the current month of the year, and the customers birthdate inputed from the user?
WildChild
12-10-2006, 04:06 PM
Yes it is an assignment, but i am not looking for a solution, just help finding one myself.
The current month of the year is not inputted by the user, i think this would be better if the program updates this by itself.
The user does input the customers birthday "customersBirthday".
The customers birthday is set via:
setCustomersBirthday(customersBirthday);
With the usual:
public void setCustomersBirthday(String inCustomersBirthday)
{
customersBirthday = inCustomersBirthday;
}
Do you think it would be better to run CustomersBirthday as a Integer instead of string?
I'm thinking that to get this working i will have to:
- Get the program to have a "date format" - so it knows the current month of the year
- Setup some form of validation so that the CustomersBirthday can be compared to the "date format", and if it is the same month discount £5 from the monthly bill.
There is a class in the java.util package called GregorianCalendar (http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html), it may be helpful for keeping track of the current Calendar date
WildChild
12-10-2006, 04:26 PM
Unfortunately I have no idea what to do with that, i realise i could create a whole new class and use it, but it seems alittle excessive for such a small program.
Other than that I have come up with this code using the java.util.Date & java.util.SimpleDateFormat from an example found on the net.
public static void main(String[] args) throws ParseException {
DateFormat df = new SimpleDateFormat("YYYY-MM-DD");
// Get current month of year
Date d1 = df.parse("02");
// Get customers birthday month
Date d2 = df.parse("03");
String relation;
if (d1.equals(d2))
relation = "the same month as customers birthday" && -5000 from monthlyRate
else if (d1.before(d2))
relation = "before";
else
relation = "after";
}
I realise this will not work like this, but i understand how it could work, can u help me out anymore? am i able just to compare the month not the date and year?
rpgfan3233
12-10-2006, 05:13 PM
I recommend Mink's suggestion: java.util.GregorianCalendar (http://java.sun.com/j2se/1.5.0/docs/api/java/util/GregorianCalendar.html).
An example to compare two dates:
import java.util.Calendar;
import java.util.GregorianCalendar;
class BirthCompare {
public static void main (String[] args) {
// GregorianCalendar(int year, int month, int day [, int hour, int minute [, int second] ] )
GregorianCalendar birthDate = new GregorianCalendar(1982, 1, 28);
GregorianCalendar currentDate = new GregorianCalendar();
double monthlyBill = 31.86;
if (birthDate.get(Calendar.MONTH) == currentDate.get(Calendar.MONTH))
monthlyBill -= 5.0;
System.out.println(String.valueOf((char)156) + " " + Double.toString(monthlyBill)); // output a pound sign in front. change it to 163 if using Unicode
}
}
That isn't tested, but it should work.
I believe that when get() returns the Month, it begins at 0 for Jan and 11 for Dec, so if you instantiate birthDate with month 1 (January) the class interprets it as February.
rpgfan3233
12-10-2006, 05:51 PM
I believe that when get() returns the Month, it begins at 0 for Jan and 11 for Dec, so if you instantiate birthDate with month 1 (January) the class interprets it as February.
Thanks for pointing that out. That is definitely crucial to know! :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.