-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountDetails.java
More file actions
122 lines (116 loc) · 4.1 KB
/
Copy pathAccountDetails.java
File metadata and controls
122 lines (116 loc) · 4.1 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
import java.util.*;
class AD
{
private String customerID;
private int accountNumber;
private double initBalance;
void setInitBalance(double initBalance){this.initBalance=initBalance;}
double getInitBalance(){return initBalance;}
void readAccountDetails()
{
Scanner s = new Scanner(System.in);
while(true)
{
try {
System.out.println("Enter CustomerId:");
customerID = s.next();
if (!(Character.isLetter(customerID.charAt(0)) && Character.isDigit(customerID.charAt(1)) && Character.isDigit(customerID.charAt(2))
&& Character.isDigit(customerID.charAt(3))))
throw new Exception("Customer ID must start with a letter and should be followed by three digits");
}
catch(Exception e) {
System.out.println(e);continue;}
try {
System.out.println("Enter accountNumber:");
accountNumber = s.nextInt();
if (String.valueOf(accountNumber).length() != 5)
throw new Exception("Account no must be of five digits");
}
catch(Exception e) {
System.out.println(e);
continue;}
try{
System.out.println("Enter InitialBalance:");
initBalance=s.nextDouble();
setInitBalance(initBalance);
if (initBalance <= 1000)
throw new Exception("Initial balance must be above $1000");
break;
}
catch(Exception e) {System.out.println(e);}
}
}
void deposit()
{
Scanner s = new Scanner(System.in);
double amount;
while(true) {
try {
System.out.println("ENTER AMOUNT TO DEPOSIT ");
amount = s.nextDouble();
if (getInitBalance() + amount > 5000)
throw new InvalidAmount('D', amount);
else {
setInitBalance(initBalance += amount);
System.out.println("Yes,your amount is deposited ,current balance : " + getInitBalance());
break;
}
} catch (InvalidAmount i) {
System.out.println(i);
}
}
}
void withdraw()
{Scanner s = new Scanner(System.in);
double withdrawAmount;
while(true) {
try{
System.out.println("ENTER AMOUNT TO WITHDRAW ");
withdrawAmount= s.nextDouble();
if (getInitBalance() - withdrawAmount < 1000)
throw new InvalidAmount('W', withdrawAmount);
else {
setInitBalance(initBalance -= withdrawAmount);
System.out.println("Yes,your amount is " + withdrawAmount + " ,current balance : " + getInitBalance());
}
break;
}
catch (InvalidAmount i) {
System.out.println(i);
}
}
}
void displayAccountDetails()
{
System.out.println(" ACCOUNT_DETAILS ");
System.out.println("CUSTOMER ID :"+customerID+"\nACCOUNT_NUMBER :"+accountNumber+"\nINITIAL BALANCE :"+initBalance);
}
}
class InvalidAmount extends Exception
{
char transactionType;
double amount;
InvalidAmount(char ttype,double amount)
{
this.transactionType=ttype;
this.amount=amount;
}
public String toString()
{
if(transactionType=='D')
return "Sorry,account balance is more than 5000,you cannot deposit "+amount;
if(transactionType=='W')
return "Sorry,account balance is less than "+amount+" you cannot withdraw "+amount;
return "1";
}
}
public class AccountDetails
{
public static void main(String[] args)
{
AD ad= new AD();
ad.readAccountDetails();
ad.displayAccountDetails();
ad.deposit();
ad.withdraw();
}}