-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatmsys.java
More file actions
153 lines (122 loc) · 5.28 KB
/
atmsys.java
File metadata and controls
153 lines (122 loc) · 5.28 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.util.*;
public class atmsys {
public static Scanner kbd = new Scanner(System.in);
// The checkID method determines if acctNum is a valid account number
// and pwd is the correct password for the account. If the account information
// is valid, the method returns the current account balance, as a string.
// If the account information is invalid, the method returns the string "error".
public static String checkID(String acctNum, String pwd)
{
String result = "error";
// Strings a, b, and c contain the valid account numbers and passwords.
// For each string, the account number is listed first, followed by
// a space, followed by the password for the account, followed by a space,
// followed by the current balance.
String a = "44567 mypassword 5200";
String b = "1234567 anotherpassword 48000";
String c = "4321 betterpassword 96000";
if (acctNum.equals(a.substring(0, a.indexOf(" "))) &&
pwd.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "))))
return result = a.substring(a.lastIndexOf(" ") + 1);
if (acctNum.equals(b.substring(0, b.indexOf(" "))) &&
pwd.equals(b.substring(b.indexOf(" ")+1,b.lastIndexOf(" "))))
return result = b.substring(b.lastIndexOf(" ") + 1);
if (acctNum.equals(c.substring(0, c.indexOf(" "))) &&
pwd.equals(c.substring(c.indexOf(" ") + 1,c.lastIndexOf(" "))))
return result = c.substring(c.lastIndexOf(" ") + 1);
return result;
}
public static int menu()
{
int menuChoice;
do
{
System.out.print("\nPlease Choose From the Following Options:"
+ "\n 1. Display Balance \n 2. Deposit"
+ "\n 3. Withdraw\n 4. Log Out\n\n");
menuChoice = kbd.nextInt();
if (menuChoice < 1 || menuChoice > 4){
System.out.println("error");
}
}while (menuChoice < 1 || menuChoice > 4);
return menuChoice;
}
public static void displayBalance(double x)
{
System.out.printf("\nYour Current Balance is Rs.%.2f\n", x);
}
public static double deposit(double x, double y)
{
double depositAmt = y, currentBal = x;
double newBalance = depositAmt + currentBal;
System.out.printf("Your New Balance is Rs.%.2f\n", newBalance);
return newBalance;
}
public static double withdraw(double x, double y)
{
double withdrawAmt = y, currentBal = x, newBalance;
newBalance = currentBal - withdrawAmt;
System.out.printf("Your New Balance is Rs.%.2f\n",newBalance);
return newBalance;
}
public static void main(String[] args) {
String accNum, pass, origBal = "error";
int count = 0, menuOption = 0;
double depositAmt = 0, withdrawAmt = 0, currentBal=0;
boolean foundNonDigit;
//loop that will count the number of login attempts
//you make and will exit program if it is more than 3.
//as long as oriBal equals an error.
do{
foundNonDigit = false;
System.out.println("Please Enter Your Account Number: ");
accNum = kbd.next();
System.out.println("Enter Your Password: ");
pass = kbd.next();
origBal = checkID(accNum, pass);
count++;
if (count >= 3 && origBal.equals("error")){
System.out.print("Maximum Login Attempts Reached.");
System.exit(0);
}
if (!(origBal.equals("error"))){
System.out.println("\nYour New Balance is: Rs. "+ origBal);
}
else
System.out.println(origBal);
}while(origBal.equals("error"));
currentBal=Double.parseDouble(origBal);
//this loop will keep track of the options that
//the user inputs in for the menu. and will
//give the option of deposit, withdraw, or logout.
while (menuOption != 4)
{
menuOption=menu();
switch (menuOption)
{
case 1:
displayBalance(currentBal);
break;
case 2:
System.out.print("\nEnter Amount You Wish to Deposit: Rs. ");
depositAmt = kbd.nextDouble();
currentBal=deposit(depositAmt, currentBal);
break;
case 3:
System.out.print("\nEnter Amount You Wish to Withdrawl: Rs. ");
withdrawAmt = kbd.nextDouble();
while(withdrawAmt>currentBal){
System.out.print("ERROR: INSUFFICIENT FUNDS!! "
+ "PLEASE ENTER A DIFFERENT AMOUNT: Rs.");
withdrawAmt = kbd.nextDouble();
}
currentBal = withdraw(currentBal, withdrawAmt);
break;
case 4:
System.out.print("\nThank For Using My atmsys. Have a Nice Day. Good-Bye!");
System.exit(0);
break;
}
}
}
}