BakeMeMuffins92
BakeMeMuffins92

Reputation: 67

Java Bank Interest/Savings Calculator

I am very new to Java and am trying to create a Bank Savings and Interest calculator. I have created the basic calculator but upon adding some new features to this calculator, I seemed to have screwed it up and it no longer performs any of the tasks it previously would.

A few things I am trying to accomplish with this calculator: 1. Choose your type of account (Savings, CD, or Checking) and pass to my Account object that extends the JFrame.

  1. Prompt user for the original balance, interest rate and period length. Pass these values to the Account object (this is the part I can't figure out).

  2. Print the final balance after Calculate button is clicked.

  3. Ask user "Want to open new account? Yes/No" If yes, loop back. If no, exit.

I am lacking in some basic Java knowledge which is why I am stuck now. ANY help is appreciated!

Account.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Account extends JFrame {

private int period;
private int balance;
private int rate;
private String printstring;

@Override
public String toString() {
    return String.format("Period: " + period + ", Balance: " + balance);
}

public int getPeriod() {
    return period;
}

public void setPeriod(int period) {
    this.period = period;
}

public int getBalance() {
    return balance;
}

public void setBalance(int balance) {
    this.balance = balance;
}

public int getRate() {
    return rate;
}

public void setRate(int rate) {
    this.rate = rate;
}

public String getPrintstring() {
    return printstring;
}

public void setPrintString(String printstring) {
    this.printstring = printstring;
}

public void calculate() {
    for ( int i = 0; i<period; i++)
{
    balance = (balance * rate) + balance;

}
}
}

Banker.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Banker {

// Array for type of bank account

private static void createAndShowGUI() {

    // Declare strings for period, balance, rate
    String period;
    String balance;
    String rate;

    // Prompt user for input
    period = JOptionPane.showInputDialog(null, "Number of periods (length):");
    balance = JOptionPane.showInputDialog(null, "Beginning balance:");
    rate = JOptionPane.showInputDialog(null, "Interest rate (use decimal, example: .05     = 5%):");

    // Make Calculate button
    JButton calculate = new JButton("Calculate");

    // Make 3 Labels
    JLabel blabel = new JLabel("Period: " + period);
    JLabel plabel = new JLabel("Balance: " + balance);
    JLabel flabel = new JLabel("Final Balance: " + balance);

    // Setup window with flow layout and exit on close
    JFrame frame = new JFrame("Interest Savings Calculator Plus");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Add combo box, calc button and labels
    frame.add(calculate);
    frame.add(plabel);
    frame.add(blabel);
    frame.add(flabel);
    frame.pack();
    frame.setVisible(true);


    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {

String[] accttype = { "Checking", "Savings", "CD" }; // Array of bank acct types
String input = (String) JOptionPane.showInputDialog(null, "Choose account...",
    "Choose bank account type", JOptionPane.QUESTION_MESSAGE, null,
    accttype, // Array of acct types
    accttype[0]); // First choice
System.out.println(input);


    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
            createAndShowGUI();
        }



}

Upvotes: 1

Views: 4582

Answers (2)

Frank
Frank

Reputation: 15641

I only have a general advice for you...

You are working with Money, well please don't use float and/or double as a datatype.

In float and double types it is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly.

For example, suppose you have €1.03 and you spend 0.42 How much money do you have left?

System.out.println(1.03 - .42); prints out 0.6100000000000001.

The right way to solve this problem is to use BigDecimal as a datatype, and it has all the methods like add, divide, ... and so on. One pitfall, BigDecimal is immutable don't forget about that when you do your calculations.

Upvotes: 1

Grambot
Grambot

Reputation: 4524

Just as you prompt for Account type you'll want to prompt and capture the input for the values you need:

String input = JOptionPane.showInputDialog("Question:");

This gives you a String which you need converted to a number:

double value;
try {
  value = Double.parseDouble(input);
} catch (NumberFormatException e) {
  //Handle the exception
}

You can do this for all the inputs you need. After that pass them to your Account object.

Account account = new Account();
account.setBalance(value);

I'm hoping you can extrapolate your needs from there! Good luck.

Upvotes: 0

Related Questions