2010-10-14 21 views
0

Je travaille sur une application réseau pour ma classe. Fondamentalement, je dois écrire java et jsp pour faire un site qui donne la sortie. JSP ci-dessous (qui est précédé d'une page. JSP qui demande le solde, le taux et la période). J'écris le Java pour cela maintenant et j'essaye de faire une table html avec la classe Java.Programmation d'un tableau Java de compte d'épargne pour afficher un incrément de 2

Le problème que j'ai est de faire en sorte que la table montre l'écart-type de 2 pour le taux (lignes) et la période (colonnes). Au lieu de simplement ajouter toutes les informations pour toutes les périodes et les taux. Comment puis-je le réduire?

Note: Je sais que le code dans ma table est vraiment pas correct du tout, je pense que tout le reste est OK si

Mon code à ce jour:

package SavingAcct; 

    import java.text.*; 

    public class savingsAccount { 

private double rate; 
private double currentBalance; 
private int term; 

public savingsAccount() { 

    this.rate = 0.00; 
    this.currentBalance = 0.00; 
    this.term = 0; 


} 

public savingsAccount(double rate, double currentBalance, int term) { 

    this.rate = rate; 
    this.currentBalance = currentBalance; 
    this.term = term; 


} 

/** 
* @return the term 
*/ 
public int getTerm() { 
    return term; 
} 

/** 
* @param term the term to set 
*/ 
public void setTerm(int term) { 
    this.term = term; 
} 

/** 
* @return the rate 
*/ 
public double getRate() { 
    return rate; 
} 

/** 
* @param rate the rate to set 
*/ 
public void setRate(double rate) { 
    this.rate = rate; 
} 

/** 
* @return the balance 
*/ 
public double getBalance() { 

    return currentBalance; 
} 

/** 
* @param balance the balance to set 
*/ 
public void setBalance(double balance) { 
    this.currentBalance = balance; 
} 

public String doSavingsAccount() { 
    String htmlSavingsTable = ""; 


    NumberFormat cf = NumberFormat.getCurrencyInstance(); 
    NumberFormat pf = NumberFormat.getPercentInstance(); 

    //start the html table 
    htmlSavingsTable = "<table border='2'>"; 
    //create a table heading 
    htmlSavingsTable += "<tr>"; 
    htmlSavingsTable += "<td><b> - </b></td>"; 
    htmlSavingsTable += "<td><b>" + (term-2) + "</b></td>"; 
    htmlSavingsTable += "<td><b>" + (term-1) + "</b></td>"; 
    htmlSavingsTable += "<td><b>" + term + "</b></td>"; 
    htmlSavingsTable += "<td><b>" + (term+1) + "</b></td>"; 
    htmlSavingsTable += "<td><b>" + (term+2) + "</b></td>"; 
    htmlSavingsTable += "</tr>"; 

    for (double rate = this.getRate()-2; rate <= getRate()+2;){ 


     // start html table row for 
     htmlSavingsTable += "<tr>"; 
     // add rate to row 
     htmlSavingsTable += "<td><b>" + pf.format(rate-2) + "</b></td>"; 
     // add monthly payment to row 
     htmlSavingsTable += "<td><b>" + cf.format(getNewBalance())+  "</b></td>"; 
     // end the row 
     htmlSavingsTable += "</tr>"; 


    } 
    // end the table 
    htmlSavingsTable += "</table>"; 

    return htmlSavingsTable; 

} 

public double getNewBalance() { 
    double newBalance; 
    newBalance = currentBalance * (Math.pow((1+rate), term)); 
    return newBalance; 
} 


    } 
+0

donc quand la table est faite (par exemple je durée fixée à 12) l'en-tête lu 10 11 12 13 14. Si le taux est fixé à 5%, les lignes seront de 3% 4% 5% 6% 7%. Le nouveauBalance montrera pour le contenu des tables. –

Répondre

0

Vous devez incrémenter rate dans votre boucle for pour un début, puis besoin d'itérer sur chaque term possible, et passez term et rate à la fonction getNewBalance().

Edit: Code

package acme.savingacct; 

import java.text.NumberFormat; 

public class SavingsAccount { 
    private transient double startingRate; 
    private transient double currentBalance; 
    private transient int startingTerm; 

    public SavingsAccount(final double startingRate, 
      final double currentBalance, final int startingTerm) { 
     this.startingRate = startingRate; 
     this.currentBalance = currentBalance; 
     this.startingTerm = startingTerm; 
    } 

    public SavingsAccount() { 
     this.startingRate = 0.0; 
     this.currentBalance = 0.0; 
     this.startingTerm = 0; 
    } 

    /** 
    * @return the term 
    */ 
    public final int getStartingTerm() { 
     return startingTerm; 
    } 

    /** 
    * @param term 
    *   the term to set 
    */ 
    public void setStartingTerm(final int term) { 
     this.startingTerm = term; 
    } 

    /** 
    * @return the rate 
    */ 
    public final double getStartingRate() { 
     return startingRate; 
    } 

    /** 
    * @param rate 
    *   the rate to set 
    */ 
    public void setStartingRate(final double rate) { 
     this.startingRate = rate; 
    } 

    /** 
    * @return the balance 
    */ 
    public final double getBalance() { 
     return currentBalance; 
    } 

    /** 
    * @param balance 
    *   the balance to set 
    */ 
    public void setBalance(final double balance) { 
     this.currentBalance = balance; 
    } 

    @Override 
    public String toString() { 
     final StringBuffer htmlSavingsTable = new StringBuffer(1024); 
     final NumberFormat currFmt = NumberFormat.getCurrencyInstance(); 
     final NumberFormat pctFmt = NumberFormat.getPercentInstance(); 

     // start the html table & create a table heading 
     htmlSavingsTable.append("<table border='2'>\n <tr>\n" 
       + " <th> - </th>\n"); 

     for (int term = getStartingTerm(); term < getStartingTerm() + 5; term++) { 
      htmlSavingsTable.append(" <th>" + term + "</th>\n"); 
     } 

     htmlSavingsTable.append(" </tr>\n"); 

     for (double rate = getStartingRate(); rate < getStartingRate() + 5.0; rate++) { 
      // start html table row for rate & add rate to row 
      htmlSavingsTable.append(" <tr>\n <th>" 
        + pctFmt.format(rate/100.0) + "</th>\n"); 

      // add monthly payment to row 
      for (int term = getStartingTerm(); term < getStartingTerm() + 5; term++) { 
       htmlSavingsTable 
         .append(" <td>" 
           + currFmt.format(getNewBalance(rate, term)) 
           + "</td>\n"); 
      } 

      // end the row 
      htmlSavingsTable.append(" </tr>\n"); 
     } 

     // end the table 
     htmlSavingsTable.append("</table>"); 

     return htmlSavingsTable.toString(); 
    } 

    /** 
    * @param aRate 
    *   the rate 
    * @param aTerm 
    *   the term 
    * @return the calculated balance 
    */ 
    public double getNewBalance(final double aRate, final int aTerm) { 
     double newBalance; 
     newBalance = currentBalance * (Math.pow((1 + aRate/100.0), aTerm)); 
     return newBalance; 
    } 

    /** 
    * For testing purpose .. 
    * 
    * @param args 
    */ 
    public static void main(final String[] args) { 
     final SavingsAccount sacc = new SavingsAccount(5.0, 1000.0, 12); 
     System.out.println(sacc); 
    } 

} 

sortie:

alt text

+0

merci qui a aidé beaucoup, bien que j'ai toujours du mal à obtenir mon .jsp à imprimer réellement la table –