Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7d7090b

Browse files
atm simulation task
1 parent 0370566 commit 7d7090b

File tree

7 files changed

+169
-0
lines changed

7 files changed

+169
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store

‎Mini-Projects/Project-01/ATM Simulation/.idea/.gitignore‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Mini-Projects/Project-01/ATM Simulation/.idea/misc.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Mini-Projects/Project-01/ATM Simulation/.idea/modules.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// ATM Simulation Program
2+
// Objective:
3+
// Write a Java program that simulates a basic ATM machine. The program should allow a user to
4+
// perform banking operations such as checking their balance, depositing money, withdrawing
5+
// money, and exiting the system. The program should make use of methods, method overloading,
6+
// primitive data types, conditional logic, loops, and scanners for taking inputs.
7+
8+
import java.util.Scanner;
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
Scanner scanner = new Scanner(System.in);
13+
double balance = 1000.0;
14+
System.out.println("Welcome to the ATM System!");
15+
16+
while (true) {
17+
System.out.println("1. Check Balance");
18+
System.out.println("2. Deposit Money");
19+
System.out.println("3. Withdraw Money");
20+
System.out.println("4. Exit");
21+
System.out.print("Choose an option (1, 2, 3, 4): ");
22+
23+
// Validate user input for menu option
24+
if (scanner.hasNextInt()) {
25+
int option = scanner.nextInt();
26+
switch (option) {
27+
case 1:
28+
checkBalance(balance);
29+
break;
30+
31+
case 2:
32+
System.out.print("Enter the amount to deposit: ");
33+
// Validate if input is int or double for deposit
34+
if (scanner.hasNextDouble()) {
35+
double depositAmount = scanner.nextDouble();
36+
balance = depositMoney(depositAmount, balance); // Deposit double
37+
} else if (scanner.hasNextInt()) {
38+
int depositAmount = scanner.nextInt();
39+
balance = depositMoney(depositAmount, balance); // Deposit int
40+
} else {
41+
System.out.println("Invalid input! Please enter a valid number.");
42+
scanner.next();
43+
}
44+
break;
45+
46+
case 3:
47+
System.out.print("Enter the amount to withdraw: ");
48+
// Validate if input is a double for withdrawal
49+
if (scanner.hasNextDouble()) {
50+
double withdrawAmount = scanner.nextDouble();
51+
balance = withdrawMoney(withdrawAmount, balance);
52+
} else {
53+
System.out.println("Invalid input! Please enter a valid number.");
54+
scanner.next();
55+
}
56+
break;
57+
58+
case 4:
59+
System.out.println("Thank you for using the ATM!");
60+
scanner.close();
61+
return;
62+
63+
default:
64+
// Handle invalid menu option
65+
System.out.println("Please choose a valid option!");
66+
}
67+
} else {
68+
System.out.println("Invalid input! Please enter a number (1, 2, 3, or 4).");
69+
scanner.next();
70+
}
71+
}
72+
}
73+
// Method to display current balance
74+
public static void checkBalance(double balance) {
75+
System.out.println("Your current balance is: $" + balance);
76+
}
77+
78+
// Method to deposit an integer amount (method overloading)
79+
public static double depositMoney(int amount, double balance) {
80+
if (amount > 0) {
81+
balance += amount;
82+
System.out.println("Amount deposited successfully. Your new balance is: $" + balance);
83+
} else {
84+
System.out.println("Please enter a valid deposit amount!");
85+
}
86+
return balance;
87+
}
88+
89+
// Method to deposit a double amount (method overloading)
90+
public static double depositMoney(double amount, double balance) {
91+
if (amount > 0) {
92+
balance += amount;
93+
System.out.println("Amount deposited successfully. Your new balance is: $" + balance);
94+
} else {
95+
System.out.println("Please enter a valid deposit amount!");
96+
}
97+
return balance;
98+
}
99+
100+
// Method to withdraw money
101+
public static double withdrawMoney(double amount, double balance) {
102+
if (amount <= 0) {
103+
System.out.println("Please enter a valid withdrawal amount!");
104+
} else if (amount > balance) {
105+
System.out.println("Insufficient balance!");
106+
} else {
107+
balance -= amount;
108+
System.out.println("Amount withdrawn successfully. Your new balance is: $" + balance);
109+
}
110+
return balance;
111+
}
112+
}
268 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /