0

I would like to remove hard coding of dates from the following segment of jsp:

    if ("1".equals(formFields.getValue("latefeesweek"))){
        formFields.mapDataToSession("latefeesdate","06/10/2013");
        formFields.mapDataToSession("latefeesdatedisplay","June 10, 2013 Prior to 8:00 A.M.(Central Time)");
    }else if ("2".equals(formFields.getValue("latefeesweek"))){
        formFields.mapDataToSession("latefeesdate","06/17/2013");
        formFields.mapDataToSession("latefeesdatedisplay","June 17, 2013 Prior to 8:00 A.M.(Central Time)");
    }else{
        formFields.mapDataToSession("latefeesdate","07/08/2013");
        formFields.mapDataToSession("latefeesdatedisplay","July 08, 2013 Prior to 8:00 A.M.(Central Time)");
    }

and replace with something like this:

     if ("1".equals(formFields.getValue("latefeesweek"))){
        formFields.mapDataToSession("latefeesdate", week1);
        formFields.mapDataToSession("latefeesdatedisplay",week1display);
    }else if ("2".equals(formFields.getValue("latefeesweek"))){
        formFields.mapDataToSession("latefeesdate",week2);
        formFields.mapDataToSession("latefeesdatedisplay",week2display);
    }else{
        formFields.mapDataToSession("latefeesdate",week3);
        formFields.mapDataToSession("latefeesdatedisplay",week3display);
    }

I intend to define week1, week2, week3, week1display, week2display, week3display in a properties file (shop standard) and use these fields in an html via $ff.week1.

I am looking for proper syntax.

Thank you for your guidance.

Pete
  • 3,246
  • 4
  • 24
  • 43
shykitten
  • 35
  • 6
  • 3
    You should consider putting a descriptive title of the problem – 0x9BD0 Apr 02 '14 at 19:51
  • Is this Java or JavaScript? – gen_Eric Apr 02 '14 at 19:52
  • Javascript. Sorry for not being more specific. I wan to assign the value from variable defined in my .properties file to the formfield. Does that make sense? I am not sure of syntax. I'm thinking I can embed a getValue within the mapDataToSession. formFields.mapDataToSession("latefeesdate","06/10/2013"); Yes? No? – shykitten Apr 02 '14 at 19:52
  • formFields.mapDataToSession("latefeesdate", formFields.getValue(week1)); – shykitten Apr 02 '14 at 19:59

2 Answers2

0

you could just create a var and hold the strings at the top of the code

var week1display = dd/MM/yyyy
var week2display = dd/MM/yyyy
var week3display = dd/MM/yyyy

   if ("1".equals(formFields.getValue("latefeesweek"))){
        formFields.mapDataToSession("latefeesdate", week1);
        formFields.mapDataToSession("latefeesdatedisplay",week1display);
    }else if ("2".equals(formFields.getValue("latefeesweek"))){
        formFields.mapDataToSession("latefeesdate",week2);
        formFields.mapDataToSession("latefeesdatedisplay",week2display);
    }else{
        formFields.mapDataToSession("latefeesdate",week3);
        formFields.mapDataToSession("latefeesdatedisplay",week3display);
    }

or alternatively you could possible store the date (if you are using and sort of file persistence method)

var x = new Date();

HERE is a link providing information on comparing Date()'s.

Hope this helps.

Community
  • 1
  • 1
7888
  • 72
  • 1
  • 1
  • 6
0

As suggested by The-Doctor, using a var to hold them is the solution. However, you can generalize it a bit more by using an array of objects. This solution will allow you to add multiple dates, etc... So in the future, all you have to do is edit the list of dates.

Your software can now support more than 3 notices for late dates

// Create our object
function LateFee(startWeekDate) {
  this.startOfWeek = startWeekDate;
}

// Define a method to convert the date to a string
LateFee.prototype.toString = function() {
  return [this.startOfWeek.getDate(),
          this.startOfWeek.getMonth(),
          this.startOfWeek.getFullYear()].join('/');
}

// Define a method to convert date to a notice
LateFee.prototype.notice = function() {
  var monthOfYear = ["January",  "February",  "March",  "April",  "May",  
                     "June",  "July",  "August",  "September",  "October",  
                     "November",  "December"];
  return [monthOfYear[this.startOfWeek.getMonth()], 
          this.startOfWeek.getDate(), 
          this.startOfWeek.getFullYear(),
          "Prior to 8:00 A.M. (Central Time)"].join(' ');
}

// List of late dates
var lateDates = [new LateFee(new Date(2014, 06, 05)),
                 new LateFee(new Date(2014, 06, 15)),
                 new LateFee(new Date(2014, 06, 20))];

// Current late date
var lateFeesWeek = parseInt(formFields.getValue("latefeesweek"), 10) - 1;

// Try to pick a late date from the list. 
// If the index doesn't exist, pick the last one.
var lateDate = lateDates[lateFeesWeek] || lateDates[lateDates.length - 1];

// Do the stuff you have to do.
formFields.mapDataToSession("latefeesdate", lateDate.toString());
formFields.mapDataToSession("latefeesdatedisplay", lateDate.notice());
Pete
  • 3,246
  • 4
  • 24
  • 43