-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankApplication.java
More file actions
101 lines (94 loc) · 3.5 KB
/
BankApplication.java
File metadata and controls
101 lines (94 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.Scanner;
public class BankApplication {
static final String USERNAME = "user";
static final String PASSWORD = "1234";
static double accountBalance = 0;
public static boolean authenticateUser(String s1,String s2){
if (USERNAME.equals(s1) && PASSWORD.equals(s2))
return true;
else{
return false;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int attempts = 3;
while(attempts > 0){
System.out.print("username: ");
String username = input.nextLine();
System.out.print("password: ");
String password = input.nextLine();
if(authenticateUser(username,password)){
printMenu();
break;
}
else {
attempts--;
System.out.println("Invalid credentials. Attempts left: " + attempts);
}
if (attempts == 0) {
System.out.println("Authentication failed. Exiting...");
break;
}
}
input.close();
}
public static void printMenu() {
boolean enter = true;
while (enter){
Scanner input = new Scanner(System.in);
System.out.println("\n--- Bank Application ---");
System.out.println("1. Deposit Money");
System.out.println("2. Withdraw Money");
System.out.println("3. Check Account Balance");
System.out.println("4. Exit");
System.out.println("Your choice: ");
int choice = input.nextInt();
switch (choice) {
case 1:
depositMoney();
break;
case 2:
withdrawMoney();
break;
case 3:
checkBalance();
break;
case 4:
enter = false;
input.close();
break;
default:
System.out.println("You dialed the wrong number. Please select a valid transaction ");
break;
}
}
}
public static void depositMoney() {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount to deposit: ");
double deposit = input.nextDouble();
if (deposit > 0){
accountBalance = accountBalance + deposit;
System.out.println("Deposit Succesful. New balance: " + accountBalance);
}
else{
System.out.println("Insufficient amount to deposit. ");
}
}
public static void withdrawMoney() {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount to withdraw: ");
double withdraw = input.nextDouble();
if (accountBalance >= withdraw && withdraw > 0){
accountBalance = accountBalance - withdraw;
System.out.println("Withdraw Succesful. New balance: " + accountBalance);
}
else {
System.out.println("Insufficient Balance. The withdrawal amount can not exceed the account balance.");
}
}
public static void checkBalance(){
System.out.println("Current Balance: " + accountBalance);
}
}