English

You have a saving account in a bank with some balance amount in your account. Now, you want to perform the following tasks, as per your choice. The tasks are as under: 1. Money Deposited - Computer Applications

Advertisements
Advertisements

Question

You have a saving account in a bank with some balance amount in your account. Now, you want to perform the following tasks, as per your choice. The tasks are as under: 

1. Money Deposited
2. Money Withdrawn
3. Check balance
0. To quit

Write a menu driven program to take input from the user and perform the above tasks. The program checks the balance before withdrawal and finally displays the current balance after transaction. For an incorrect choice, an appropriate message should be displayed.

Answer in Brief

Solution

import java.util.*;

public class Q15
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        
        System.out.println("Enter 1 to deposit money");
        System.out.println("Enter 2 to withdraw money");
        System.out.println("Enter 3 to check balance");
        System.out.println("Enter 0 to quit");
        
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        double amt = 0.0;
        double bal = 5000.0; // Initial balance of account
        
        switch (ch) {
            case 0:
                System.out.println("Thank you");
                break;
                
            case 1:
                System.out.print("Enter deposit amount : ");
                amt = in.nextDouble();
                bal += amt;
                System.out.println("Money deposited.");
                System.out.print("Total balance = " + bal);
                break;
                
            case 2:
                System.out.print("Enter withdrawal amount : ");
                amt = in.nextDouble();
                if (amt > bal) {
                    System.out.println("Insufficient balance.");
                }
                else {
                    bal -= amt;
                    System.out.println("Money withdrawn.");
                    System.out.print("Total balance = " + bal);
                }
                break;
                
            case 3:
                System.out.print("Total balance = " + bal);
                break;
                
            default:
                System.out.println("Invalid request.");
        }
    }
}
shaalaa.com
  Is there an error in this question or solution?
Chapter 1.08: Conditional Statements in Java - EXERCISES [Page 102]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 1.08 Conditional Statements in Java
EXERCISES | Q VI. 15. | Page 102
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×