-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankAccount.java
106 lines (93 loc) · 2.75 KB
/
BankAccount.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
package account;
import java.util.Random;
/**
* This class demonstrates a basic Bank Account instance
* @author Bisrat W Tadesse
*
*/
public class BankAccount {
private String account_number; //account number
private long checking_balance; //checking balance
private long saving_balance; //saving balance
private static int numberOfAccounts = 0; //holds the number accounts
private static long total_depsit_accross_accounts = 0; //holds the total
//amount deposited from any account type
/**
* This constructor initializes the instant variables, increments the
* number of accounts by one, and calls the private method that generates
* an account number and assigns it to the account_number variable
*/
public BankAccount(){
this.account_number = this.generateAccountNumber();
this.checking_balance = 0;
this.saving_balance = 0;
BankAccount.numberOfAccounts++;
}
/**
* This method returns the checking balance of the account
* @return checking balance
*/
public long getCheckingBalance(){
return this.checking_balance;
}
/**
* This method returns the saving balance of the account
* @return saving balance
*/
public long getSavingBalance(){
return this.saving_balance;
}
/**
* This method allows the account holder to deposit money to their savings
* account
* @param amt amount to be deposited
*/
public void depositMoneyOnSavingAcc(long amt){
this.saving_balance += amt;
BankAccount.total_depsit_accross_accounts += amt;
}
/**
* This method allows the account holders to deposit money into their
* checking account
* @param amt amount to be deposited
*/
public void depositMoneyOnCheckingAcc(long amt){
this.checking_balance += amt;
BankAccount.total_depsit_accross_accounts += amt;
}
/**
* This method allows account holders to withdraw money from their checking
* account if they have sufficient balance.
* @param amt amount to be withdrawn
* @return the amount withdrawn and the remaining balance
*/
public String withdrawMoneyFromChecking(long amt){
if(amt > this.checking_balance)
return "Insufficient balance";
else{
this.checking_balance -= amt;
return "You withdrawed: " + "$" +amt + "Your balance is :"
+ this.checking_balance;
}
}
/**
* This method returns the total checking and saving amount
* @return sum of saving and checking amount
*/
public long getTotalCeckingAndSavingAmount(){
return this.saving_balance + this.checking_balance;
}
/*
* This method generates a ten digit random account number. It uses the
* Random object
*/
private String generateAccountNumber(){
String str="";
Random rand = new Random();
for(int i = 0; i < 10; i++){
int index = rand.nextInt(8)+ 1;
str += index;
}
return str;
}
}