-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
68 lines (56 loc) · 1.93 KB
/
BankAccount.java
File metadata and controls
68 lines (56 loc) · 1.93 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
public class BankAccount {
private String accountNumber;
private double balance;
// Changing more than one value ones
public void changeValues( String accountNumber, double initialBalance){
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// Constructor to initialize account number and balance
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// Set an account number
public void setAccountNumber(String accountNumber) {
if (accountNumber.length() !=10){
return;
}
this.accountNumber = accountNumber;
}
// Getter for account number
public String getAccountNumber() {
return accountNumber;
}
// Getter for balance
public double getBalance() {
return balance;
}
// Method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited $" + amount);
}
}
// Method to withdraw money
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn $" + amount);
} else {
System.out.println("Withdrawal failed. Insufficient balance.");
}
}
// Main method to demonstrate encapsulation
public static void main(String[] args) {
BankAccount account = new BankAccount("A12345", 1000.0);
// Accessing data through getters
System.out.println("Account Number: " + account.getAccountNumber());
System.out.println("Initial Balance: $" + account.getBalance());
// Modifying data through methods
account.deposit(500.0);
account.withdraw(200.0);
System.out.println("Final Balance: $" + account.getBalance());
}
}