Git Product home page Git Product logo

savings-account's Introduction

savings-account

This program will calculate the balance of a savings account at the end of a period of time. It will use a for loop to accomplish this.

import java.util.Scanner;
public class SavingsAccountBalance {
  public static void main(String[] arg) {

    //Ask the user for starting balance, monthly interest rate, and # of months since the account was created.
    System.out.println("This program will calculate the balance of your savings account. Please enter your starting"
            + " balance: ");
    Scanner sc = new Scanner(System.in);
    double startingBalance = sc.nextDouble();
    System.out.println("Please enter your monthly interest rate (as a whole number): ");
    double interestRate = sc.nextDouble();
    System.out.println("Please enter the number of months since the account has been established: ");
    int monthsPassed = sc.nextInt();
    //Declare the necessary variables for the for loop.
    double monthlyDeposit, monthlyWithdrawal, monthlyInterest;
    double accountBalance = startingBalance;
    double totalInterestEarned = 0, totalMoneyWithdrawn = 0, totalMoneyDeposited  = 0;

    //Begin the for loop: ask for amount deposited, amount withdrawn, and calculate the interest.
    for (int i = monthsPassed; i > 0; i--) {
        //Add the amount deposited to the account balance.
        System.out.println("Please enter the amount deposited this month: ");
        monthlyDeposit = sc.nextDouble();
        accountBalance += monthlyDeposit;

        //Subtract the amount withdrawn from the account balance.
        System.out.println("Please enter the amount withdrawn this month: ");
        monthlyWithdrawal = sc.nextDouble();
        accountBalance -= monthlyWithdrawal;

        //Calculate monthly interest by turning it into a decimal and multiplying it by the account balance.
        monthlyInterest = (interestRate / 100) * accountBalance;
        accountBalance += monthlyInterest;
        System.out.println("Your calculated monthly interest is $" + monthlyInterest + ", and your account" +
                " balance thus far is $" + accountBalance);

        totalInterestEarned += monthlyInterest;
        totalMoneyWithdrawn += monthlyWithdrawal;
        totalMoneyDeposited += monthlyDeposit;

    }

    //Display the total amount of deposits made, total amount of withdrawals made, and total interest earned.
    System.out.println("Your total money withdrawn amounted to $" + totalMoneyWithdrawn +
            "\nYour total money deposited amounted to $" + totalMoneyDeposited +
            "\nYour total amount of interest earned is: $" + totalInterestEarned);
  }
}

savings-account's People

Contributors

awadasma99 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.