-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAccount.java
202 lines (170 loc) · 6.71 KB
/
Account.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Sinan Kamil
* Nov 7, 2018
* Account.java
* a password protected account for a bank, includes name, pin, balance and time
* no dependencies
*/
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.Random;
public class Account
{
//class variables
private static Scanner systemIn = new Scanner(System.in);
//instance variables
private String firstName;
private String lastName;
private String pin;
private double balance;
private LocalDateTime dateCreated;
/* Account constructor default
*
*/
Account()
{
//call the other constructor but use a value of 0 for the balance and null for name
//basically the "this" is being replaced by the current class which is Account
//yay for reusing code!
this(null,null,0.0);
}
/* Account constructor with no initial balance
*
*/
Account(String firstName, String lastName)
{
//call the other constructor but use a value of 0 for the balance
//basically the "this" is being replaced by the current class which is Account
//yay for reusing code!
this(firstName, lastName, 0.0);
}
/* Account constructor with an initial balance
*
*/
Account(String firstName, String lastName, double initialBalance)
{
//set first and last name
this.firstName = firstName;
this.lastName = lastName;
//set balance to initial amount
balance = initialBalance;
//set date created to today
dateCreated = LocalDateTime.now();
System.out.println("Your bank account has been created!!");
System.out.printf("Your current balance: $%.2f%n",balance);
System.out.println("Date/Time created: " + dateCreated);
//generate a new pin number for this account
generateNewPin();
}
/* method checkBalance() prints account balance if pin is entered correctly
*
*/
public void checkBalance()
{
if(verifyPin())
System.out.printf("Your current balance: $%.2f%n",balance);
else
System.out.println("Unable to view balance.");
}
/* method generateNewPin() creates a new random 4 digit pin number
* sets pin and outputs to screen
*/
private void generateNewPin()
{
//create secureRandom object
Random randomGenerator = new Random();
//generate four random numbers 0-9 converted to a String (maybe a loop would be better??)
String num1 = Integer.toString(randomGenerator.nextInt(10));
String num2 = Integer.toString(randomGenerator.nextInt(10));
String num3 = Integer.toString(randomGenerator.nextInt(10));
String num4 = Integer.toString(randomGenerator.nextInt(10));
//combine (concatenate) all four number Strings into 1 String
pin = num1 + num2 + num3 + num4;
//output
System.out.printf("Your new pin number is %s.%n",pin);
}
/* method getFirstName() returns the first name of the current account
*
*/
public String getFirstName()
{
return firstName;
}
/* method getLastName() returns the last name of the current account
*
*/
public String getLastName()
{
return lastName;
}
/* method printInfo() prints account name
*/
public void printInfo()
{
System.out.printf("Account: %s %s%n",firstName,lastName);
}
/* method verifyPin() asks user to enter pin number
* checks entry with actual pin
* returns true if correct, false otherwise
* user has three tries to enter correctly
*/
private boolean verifyPin()
{
int maxTimes = 3;
int times = 0;
//loops until pin entered correctly or max tries reached
do
{
System.out.print("Enter your pin number: ");
if(systemIn.next().equals(pin))
return true;
else
{
times++;
System.out.print("Invalid. ");
}
}while(times < maxTimes);
System.out.println("You have entered your pin incorrectly too many times.");
return false;
}
public void withdrawal(){
if (verifyPin() == true){//makes sure that pin was entered correctly
System.out.println("Please enter the amount you want to withdrawal!");//prints the statement
int money = systemIn.nextInt();
while (money < 0 || money > balance){//makes sure the amount is bigger than 0 and that them money requested is less than the balance
System.out.println("Amount Invaild");//prints invaild if the amount bigger or the money needed is less than the balance
System.out.println("Enter (1) try again OR (2) Main Menu! ");//picks one to either try again or back to main menu
money = systemIn.nextInt();//allows to pick one or two
if (money == 1) {//if choice one
System.out.println("Please enter the amount you want to withdrawal!");//it will print this statement
money = systemIn.nextInt();//allows them to enter
}
else if (money == 2) {//if choices two
return;//returns to the main menu
}
money = 0;//set money equals 0
}
balance = balance - money;//then the math formula if money withdrawal is subtracted from the to balance
}
}
public void deposite(){
if(verifyPin() == true){//makes sure that pin was entered correctly
System.out.println("Please enter the amount you want to Deposit!");//prints the statement
int money = systemIn.nextInt();//allows them to enter a number
while (money < 0){//makes sure the amount is bigger than 0
System.out.println("Amount Invaild");//prints invaild if not bigger than zero
System.out.println("Enter (1) try again OR (2) Main Menu! ");//picks one to either try again or back to main menu
money = systemIn.nextInt();//allows to pick one or two
if (money == 1) {//if choice one
System.out.println("Please enter the amount you want to Deposit!");//it will print this statement
money = systemIn.nextInt();//allows them to enter
}
else if (money == 2) {//if choices two
return;//returns to the main menu
}
money = 0;//set money equals 0
}
balance = balance + money;//then the math formula if money deposited then add to balance
}
}
}