Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming Question. I am trying to write a menu program with a starting balance of 1000$ and then after choosing a money amount and

Java Programming Question. I am trying to write a menu program with a starting balance of 1000$ and then after choosing a money amount and option to either deposit or withdraw, I want the total balance to be printe, but I want the previous balances to be added until the program is stopped. For example: if I enter 45 and D, the output should be 1045, and then when asked again, I enter 60 and W, the output should be 985. But I cannot figure out how to incorporate the previous balance and set the starting balance at 1000. My code is below.

import java.util.Scanner; public class ATM { public static void main(String[] args) { int result = 0, balance = 1000, money; char opt; Scanner sc = new Scanner(System.in);

Menu(); //function menu is called System.out.println("Enter money amount and ATM option"); money = sc.nextInt(); balance = sc.nextInt(); opt = sc.next().charAt(0); do { switch (opt) { case 'D': result = Deposit(balance, money);//call the function System.out.println("Balance = $" + result); break; case 'W': result = Withdrawal(balance, money); System.out.println("Balance = $" + result); break; } //switch System.out.println("Enter money amount and ATM option"); money = sc.nextInt(); balance = sc.nextInt(); opt = sc.next().charAt(0); }//do-while while (opt != 'q'); }//main public static void Menu() { System.out.println(" \t 'D' for deposit"); System.out.println(" \t 'W' for withdrawal"); System.out.println(" \t 'q' to stop the program"); } public static int Deposit(int balance, int money) { return balance + money; } public static int Withdrawal(int balance, int money) { return balance - money; } }//class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions