Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Bangko sa Java SIMPLIFIED
- class BankAccount {
- private String accountNumber;
- private String accountHolderName;
- private double balance;
- BankAccount(String accountNumber, String accountHolderName, double balance)
- {
- this.accountNumber = accountNumber;
- this.accountHolderName = accountHolderName;
- this.balance = balance;
- }
- // Getter methods to access encapsulated data
- String getAccountNumber()
- {
- return accountNumber;
- }
- String getAccountHolderName()
- {
- return accountHolderName;
- }
- double balance()
- {
- return balance;
- }
- // Setter methods to modify encapsulated data
- void setAccountNumber(String accountNumber)
- {
- this.accountNumber = accountNumber;
- }
- void setAccountHolderName(String accountHolderName)
- {
- this.accountHolderName = accountHolderName;
- }
- void setBalance(double balance)
- {
- this.balance = balance;
- }
- void deposit(double amount)
- {
- if (amount <= 0)
- {
- System.out.println("Error: Deposit amount must be positive.");
- return; // Deposit not positive then return/exit method
- }
- balance += amount;
- System.out.println("Deposit: " + amount);
- }
- void withdraw(double amount)
- {
- if (amount > 0)
- {
- if (balance >= amount) //Balance greater than/equal amount
- {
- balance -= amount; //same as (balance = balance - amount;)
- System.out.println("Withdrawal: " + amount);
- } else {
- System.out.println("Error: Insufficient funds.");
- }
- } else {
- System.out.println("Error: Withdrawal amount must be positive.");
- }
- }
- void displayAccountInfo() {
- System.out.println("Account Number: " + accountNumber);
- System.out.println("Account Holder Name: " + accountHolderName);
- System.out.printf("Balance: Php %6.2f\n\n", balance);
- }
- public static void main(String[] args) {
- BankAccount account = new BankAccount("696924069", "Padullon Gaudenz", 1000.00);
- account.displayAccountInfo();
- System.out.printf("Try to deposit negative values\n");
- account.deposit(-9000);
- account.displayAccountInfo();
- System.out.printf("\nTry to withdraw Php9000\n");
- account.withdraw(90);
- account.displayAccountInfo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement