In this blog post, we will discuss a Java program solution for counting the money entered by a user in an ATM machine. The program will take input from the user in the form of different denominations of currency notes and coins, and then calculate the total amount entered.
To begin with, let’s break down the problem statement into smaller subtasks:
- Take input from the user for the number of each denomination of currency notes and coins.
- Calculate the total amount by multiplying the number of each denomination by its respective value.
- Display the total amount in JSON format.
Now, let’s dive into the code implementation:
// Importing the required packages
import java.util.Scanner;
import org.json.JSONObject;
public class CountMoneyInATM {
public static void main(String[] args) {
// Creating a scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Taking input for each denomination of currency notes and coins
System.out.print("Enter the number of 2000 rupee notes: ");
int twoThousand = scanner.nextInt();
// Repeat the above steps for each denomination
// ...
// Calculating the total amount
int totalAmount = (twoThousand * 2000) + (fiveHundred * 500) + (oneHundred * 100) + (fifty * 50) + (twenty * 20) + (ten * 10) + (five * 5) + (two * 2) + (one * 1);
// Creating a JSON object to store the result
JSONObject result = new JSONObject();
result.put("total_amount", totalAmount);
// Display the result in JSON format
System.out.println(result.toString());
// Close the scanner
scanner.close();
}
}
By running the above code, the program will take input from the user for the number of each denomination of currency notes and coins. It will then calculate the total amount entered and display it in JSON format.
Let’s test the program with a sample input:
Enter the number of 2000 rupee notes: 2
Enter the number of 500 rupee notes: 3
Enter the number of 100 rupee notes: 5
Enter the number of 50 rupee notes: 10
Enter the number of 20 rupee notes: 20
Enter the number of 10 rupee coins: 50
Enter the number of 5 rupee coins: 100
Enter the number of 2 rupee coins: 200
Enter the number of 1 rupee coins: 500
The program will calculate the total amount entered as 20490 and display it in JSON format:
{"total_amount":20490}
This solution provides a Java program that counts the money entered by a user in an ATM machine and displays the total amount in JSON format. It can be used as a starting point for building more complex ATM software or integrated into existing systems.