//prompt for date variables
String day1 = JOptionPane.showInputDialog(
"<html>Interest Compounded Daily Function<br>" +
"Please enter today's day:");
String month1 = JOptionPane.showInputDialog(
"<html>Interest Compounded Daily Function<br>" +
"Please enter today's month (in number form):");
String year1 = JOptionPane.showInputDialog(
"<html>Interest Compounded Daily Function<br>" +
"Please enter today's year:");
String day2 = JOptionPane.showInputDialog(
"<html>Interest Compounded Daily Function<br>" +
"Please the future day:");
String month2 = JOptionPane.showInputDialog(
"<html>Interest Compounded Daily Function<br>" +
"Please the future month (in number form):");
String year2 = JOptionPane.showInputDialog(
"<html>Interest Compounded Daily Function<br>" +
"Please the future year:");
//convert date variables:
int d1 = Integer.parseInt(day1);
int m1 = Integer.parseInt(month1);
int y1 = Integer.parseInt(year1);
int d2 = Integer.parseInt(day2);
int m2 = Integer.parseInt(month2);
int y2 = Integer.parseInt(year2);
//declare calculation variables
int sumyd1 = 0, summd = 0, sumdd= 0, sumnd = 0;
int yeardiff=0;
int monthdiff=0;
//set yeardiff and monthdiff values
if(m2>m1){
monthdiff=m2-m1;
yeardiff=y2-y1;
}
else if (m1>m2){
monthdiff=(m2+12)-m1;
yeardiff=(y2-1)-y1;
}
else{
monthdiff=0;
yeardiff=y2-y1;
}
int daysinamonth=0;
//daysinamonth values, for when d1>d2
if(((m2-1)==1)|((m2-1)==3)|((m2-1)==5)|((m2-1)==7)
|((m2-1)==8)|((m2-1)==10)|((m2-1)==0)){
daysinamonth=31;
}
else if(((m2-1)==4)|((m2-1)==6)|((m2-1)==9)|((m2-1)==11)){
daysinamonth=30;
}
else if(((m2-1)==2)&((y1%4)==0)&((y1%100)!=0)
| ((y1%400)==0)){
daysinamonth=29;
}
else{
daysinamonth=28;
}
//calculate number of days, for years
if ((y2>y1)&((m2>m1)|(m2==m1))){
for (int counter=1; counter<=yeardiff; counter++){
if((((y1+counter)%4)==0)&(((y1+counter)%100)!=0)
| (((y1+counter)%400)==0)){
sumyd1=366+sumyd1;
}
else{
sumyd1=365+sumyd1;
}//end else
}//end for loop
}//end if statement
else if ((y2>y1+1)&(m1>m2)){
for (int counter=0; counter<yeardiff; counter++){
if((((y1+counter)%4)==0)&(((y1+counter)%100)!=0)
| (((y1+counter)%400)==0)){
sumyd1=366+sumyd1;
}
else{
sumyd1=365+sumyd1;
}
}//end for loop
}//end if statement
else if((y2==y1+1)&(m1>m2)){
sumyd1=0;
}
else{
sumyd1=0;
}
//calculate number of days, for months
if((m2>m1)&(d2>d1)){
for (int counter=1; counter<=monthdiff; counter++,m1++){
if((m1==1)|(m1==3)|(m1==5)|(m1==7)
|(m1==8)|(m1==10)|(m1==12)){
summd=31+summd;
}
else if((m1==4)|(m1==6)|(m1==9)|(m1==11)){
summd=30+summd;
}
else if ((m1==2)&((((y1)%4)==0)&(((y1)%100)!=0)
| (((y1)%400)==0))){
summd=29+summd;
}
else{
summd=28+summd;
}//end else
}//end for loop
}//end if for months m2>m1 and d2>d1
//if m2>m1 and d1>d2
else if((m2>m1)&(d1>d2)){
for (int counter=1; counter<monthdiff; counter++,m1++){
if((m1==1)|(m1==3)|(m1==5)|(m1==7)
|(m1==8)|(m1==10)|(m1==12)){
summd=31+summd;
}
else if((m1==4)|(m1==6)|(m1==9)|(m1==11)){
summd=30+summd;
}
else if ((m1==2)&((((y1)%4)==0)&(((y1)%100)!=0)
| (((y1)%400)==0))){
summd=29+summd;
}
else{
summd=28+summd;
}//end else
}//end for loop
}//end if months m2>m1 and d1>d2
//if m1>m2 and d2>d1
else if ((m1>m2)&(d2>d1)){
for (int counter=1; counter<=monthdiff; counter++, m1++){
if((m1==1)|(m1==3)|(m1==5)|(m1==7)
|(m1==8)|(m1==10)|(m1==12)){
summd=31+summd;
}
else if((m1==4)|(m1==6)|(m1==9)|(m1==11)){
summd=30+summd;
}
else if ((m1==2)&((((y1)%4)==0)&(((y1)%100)!=0)
| (((y1)%400)==0))){
summd=29+summd;
}
else{
summd=28+summd;
}
if (m1==12){
m1=0;
}
}
}//end else
//if m1>m2 and d1>d2
else if ((m1>m2)&(d1>d2)){
for (int counter=1; counter<monthdiff; counter++, m1++){
if((m1==1)|(m1==3)|(m1==5)|(m1==7)
|(m1==8)|(m1==10)|(m1==12)){
summd=31+summd;
}
else if((m1==4)|(m1==6)|(m1==9)|(m1==11)){
summd=30+summd;
}
else if ((m1==2)&((((y1)%4)==0)&(((y1)%100)!=0)
| (((y1)%400)==0))){
summd=29+summd;
}
else{
summd=28+summd;
}
if (m1==12){
m1=0;
}
}
}//end else
else{
summd=0;
}//end else
//calculate number of days for days
if ((d2>d1)){
sumdd=d2-d1;
}
else if (d2==d1){
sumdd=0;
}
else if (d1>d2){
sumdd=(daysinamonth-d1)+d2;
}
sumnd=sumyd1+summd+sumdd;
JOptionPane.showMessageDialog(null, sumnd);
please let me know if the code does not display properly

New Topic/Question
Reply




MultiQuote





|