Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JAVA code for ISP rate. NOT WORKING!!

Status
Not open for further replies.

aarobonob

Programmer
Aug 18, 2001
1
US
Hi. I am tr ying to write a function for JAVA that will caluclate a rate for my local ISP for the day's sign up. the final rate after taxes is 21.05. If the days left is less than 15, we add the rest of the amount plus 21.05 to it. if its more than 15 days left, then its only that pro-rated amount. But anyways, I can get the system date and I use a substring to get "Mar" for the current month. then i have an If statment that is supposed to set the numOfDays to 31 if "Mar". well, its not working. the month is set to "Mar" but the if statement isn't working.

here's the code
import java.util.*;

public class counter {

public static double convertStringtoDouble(String s) {
Double doubleObject = Double.valueOf(s);
return doubleObject.doubleValue();
}



public static void main(String argv[]) {

//Define all variables
String dateString="",month="",dayString="",yearString="";
int year;
double dialup=19.95,dialUpPrice,numOfDays=0;
//System Date
Date today = new Date();
dateString=today.toString();
//Figures out month<string>, day<double> for calculations.
month = dateString.substring(4,7);
month = month.toString();
dayString = dateString.substring(8,10);
double day = convertStringtoDouble(dayString);
yearString = dateString.substring(24,28);
double yeardouble= convertStringtoDouble(yearString);
year = (int)yeardouble;
//if month is J,M,M,J,A,O,D numOfDays is 31
if (month==&quot;Jan&quot; || month==&quot;Mar&quot; || month == &quot;May&quot; || month==&quot;Jul&quot; || month == &quot;Aug&quot; || month == &quot;Oct&quot; || month == &quot;Dec&quot;)
{
numOfDays=31;
}
//else check for leap years in 2004 and 2008 (after this, java will be obsolete)
else if (month==&quot;Feb&quot; && (year == 2004 || year == 2008))
{
numOfDays=29;
}
else if (month==&quot;Feb&quot;){
numOfDays=28;}
else{
numOfDays=30;
}
//not sure about this part. hopeuflly this computes teh amount.
double tempPrice=dialup / numOfDays * (numOfDays-day);
if (day < 15) {
dialUpPrice=tempPrice+(tempPrice*0.055);
}
else {
dialUpPrice = dialup+tempPrice+((dialup+tempPrice)*0.055);
}
//prints out the dialup price and just for testing the num of days
System.out.println (dialUpPrice);
System.out.println (numOfDays);
}

}

tahnx alot for your help.
aarons
 
If I were you I'd use a GregorianCalendar for this calculation. This will eliminate the need for parsing the date string (which is messy and error prone).

It's also a bad idea to use double or float for money as you will always encounter rounding errors.

Hardcoding values like 0.055 all over your program is a bad idea.

Looks like you are not sure how to even do the calculation. Your comments amount to closing your eyes and crossing your fingers and hoping the right answer magically gets spit out. Maybe you need to figure out how to solve the problem before you try to code it?

Lastly, I'm laughing at your silly comment about Java being obsolete in 6 years. Maybe you ought to rewrite your program in C or C++ (better yet VB or C#) if that is your poor opinion of it.

Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top