Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


herbeck
java
1 Answer(s)      4 years and 9 months ago
Posted in : Swing AWT



Advertisement
View Answers

November 30, 2009 at 11:42 AM


Hi Friend,

Try the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Savings {

public double balance;
public double interestRate;
public double totalInterest;
public double totalDeposit;
public double totalWithdraw;

public Savings(double a, double b){
balance = a;
interestRate = b;
totalDeposit = 0.00;
totalWithdraw = 0.00;
totalInterest = 0.00;
}
public void setInterestRate(double b){
interestRate = b;
}
public void setBalance(double a){
balance = a;
}
public double getInterestRate(){
return interestRate;
}
public double getBalance(){
return balance;
}
public void deposit(double amount1){
totalDeposit += amount1;
balance += amount1;
}
public void withdraw( double amount2 ){
totalWithdraw += amount2;
balance -= amount2;
}
public void computeInterest(){
totalInterest += balance * ((interestRate/100)/12);
balance += balance * ((interestRate/100)/12);
}
public boolean equals(Savings account){
if(interestRate == account.interestRate && balance == account.balance) {
return true;
}
else {
return false;
}
}
}
public class SaveTest extends JFrame{
JLabel label1,label2,label3;
JTextField text1,text2,text3;
JButton b1;
JPanel panel;
public SaveTest(){
panel=new JPanel(new GridLayout(4,2));
label1=new JLabel("Enter the month:");
label2=new JLabel("Enter the balance:");
label3=new JLabel("Enter the Annual Interest");
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
b1=new JButton("Balance");

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
int month=Integer.parseInt(value1);
String value2=text2.getText();
double dep=Double.parseDouble(value2);
String value3=text3.getText();
double intRate=Double.parseDouble(value3);
Savings account = new Savings(dep, intRate);
for(int i=1;i<=month;i++){

String depo =JOptionPane.showInputDialog(null,"Please enter the deposited amount: ");
double deposited=Double.parseDouble(depo);
double totalDep=account.totalDeposit+deposited;
JOptionPane.showMessageDialog(null,"You have deposited: "+totalDep);
account.deposit(deposited);
account.computeInterest();
String with=JOptionPane.showInputDialog(null,"Please enter the withdrawal amount: ");
double withdrawl=Double.parseDouble(with);
account.withdraw(withdrawl);
account.computeInterest();
}
JOptionPane.showMessageDialog(null," Total amount deposited: " + account.totalDeposit + "\n" +" Total amount withdrawn: " + account.totalWithdraw + "\n" +" Final balance: " + account.balance + "\n" +" Total Interest: " + account.totalInterest);
}
});
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(b1);
add(panel);
pack();
setVisible(true);

}
public static void main(String []args){
SaveTest test=new SaveTest();
}
}

Thanks


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com