Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bank Account Program

Tags:

java

I am working on making a program to simulate bank transactions. I have to ask the user if they want to deposit, withdrawal, or transfer.

When I deposit a certain amount (for example 1000), it says my balance is 1000. Then I ask to withdrawal a number like 400 it says my balance is -400. After all that I thought maybe I have to check my balance and then it will give me the correct balance of what should be 600, but it says 0. For instance, see this transcript:

screen capture of output

I was thinking because in my code (shown below) I made the balance = 0 but if I take the = 0 away and try to run the program it says it needs to be initialized.

I am stuck and I want to figure it out. Please don't post the entire code corrected. I want to fix it myself and learn!

import java.util.Scanner;

public class BankTransactions {


    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int num;

        do {

            double balance = 0;
            double amount;

            System.out.println("Type Number");
            System.out.println("1. Deposit");
            System.out.println("2. Withdrawal");
            System.out.println("3. Balance");
            System.out.println("4. Exit");
            num = scan.nextInt();

            if (num == 1) {
                System.out.println("Enter amount to deposit: ");
                amount = scan.nextDouble();

                // Add the amount to the balance
                balance += amount;
                System.out.println("Your balance is");
                System.out.println(balance);


            } else if (num == 2) {

                System.out.println("Enter amount to withdrawal: ");
                amount = scan.nextDouble();

                // Remove the amount from the balance
                balance -= amount;
                System.out.println("Your balance is");
                System.out.println(balance);

            } else if (num == 3) {


                System.out.println("Your Balance");
                System.out.println(balance);

            }



        } while (num != 4);

        System.out.println("Good Bye!");

    }
}
like image 770
Morgan Avatar asked Oct 09 '13 14:10

Morgan


People also ask

What are the basic functionalities of a bank account?

In this program, we will add some basic functionalities of a bank account like a deposit of amount, withdrawal of amount, etc. Initially, the program accepts the number of customers we need to add and adds the customer and account details accordingly. Further, it displays the series of menus to operate over the accounts.

What is the C program for bank operation?

C Program For Bank Operation. By Dinesh Thakur. This is C program that asks user to create the bank system criteria through programming. User will use switch statement to create these operation. From this user can select through their choices. User also declares variables for the storing value.

What is a production banking application?

In a production banking application, a database system with Authentication, authorization, security on accessing account data etc. are used. With this program, you will learn how to use class in python and how to use methods in python classes.

How do I open a new bank account?

Opening a new account in a bank usually requires a person to visit the bank, fill out a form, and submit the necessary papers. All of these tasks take up a lot of time and dampen the overall customer experience.


2 Answers

Every time the do{...} while{...} is executed, you are setting balance=0. You should take it out the loop.

double balance = 0;

do{
...
like image 177
luanjot Avatar answered Sep 28 '22 12:09

luanjot


You are initializing balance to 0 within the do loop, so it resets to zero each time around.

Move the line balance = 0 to above the while loop.

like image 45
Tom Swifty Avatar answered Sep 28 '22 13:09

Tom Swifty