Advertisement
Rahmadnet

BASIC OOP

Jun 27th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. * BASIC OOP
  2.  
  3. package Account;
  4.  
  5. public class TestAccount
  6. {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         Account a1 = new Account(1234, 99.99);
  11.         System.out.println(a1);
  12.         Account a2 = new Account(8888);
  13.         System.out.println(a2);
  14.        
  15.         a1.setBalance(88.88);
  16.         System.out.println(a1);
  17.         System.out.println("The account Number is : " + a1.getAccountNumber());
  18.         System.out.println("The balance is : " + a1.getBalance());
  19.        
  20.         a1.credit(10);
  21.         System.out.println(a1);
  22.         a1.debit(5);
  23.         System.out.println(a1);
  24.         System.out.println(a1);
  25.         a1.debit(500);
  26.         System.out.println(a1);
  27.  
  28.        
  29.     }
  30.  
  31. }
  32.  
  33. public class Account
  34. {
  35.     private int accountNumber;
  36.     private double balance;
  37.    
  38.     public Account(int accountNumber, double balance)
  39.     {
  40.         this.accountNumber = accountNumber;
  41.         this.balance = balance;
  42.     }
  43.    
  44.     public Account (int accountNumber)
  45.     {
  46.         this.accountNumber = accountNumber;
  47.         this.balance = 0.0;
  48.     }
  49.     public int getAccountNumber()
  50.     {
  51.         return this.accountNumber;
  52.     }
  53.    
  54.     public double getBalance()
  55.     {
  56.         return this.balance;
  57.     }
  58.    
  59.     public void setBalance(double balance)
  60.     {
  61.         this.balance = balance;
  62.     }
  63.    
  64.     public void credit(double amount)
  65.     {
  66.         balance += amount;
  67.     }
  68.    
  69.     public void debit (double amount)
  70.     {
  71.         if(balance < amount)
  72.         {
  73.             System.out.println("amount withdraw ex current balance!");
  74.         }else
  75.         {
  76.             balance -= amount;
  77.         }
  78.     }
  79.    
  80.     public String toString()
  81.     {
  82.         return String.format("A/C no:%d, Balance =%.2f", accountNumber, balance);
  83.     }
  84.    
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement