Advertisement
Guest User

ObjectTeachingExample

a guest
May 24th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. /**
  2.  * Created by GianDavid on 22.05.2017.
  3.  */
  4.  
  5. import javafx.application.Application;
  6. import javafx.stage.Stage;
  7.  
  8. class PriceCalc  {
  9.  
  10.     public static void main(String[] args) {
  11.         // The execution of the program starts here.
  12.        
  13.         MyBankAccount account1 = new MyBankAccount(1, "owner1", 0);
  14.         MyBankAccount account2 = new MyBankAccount(2, "owner2", 0);
  15.  
  16.         account1.depositMoney(30);
  17.         account2.depositMoney(500);
  18.  
  19.         account1.withdrawMoney(100); // this returns false because the account only has 30 'money' in it.
  20.         account1.withdrawMoney(10); // this returns true because the account has more than 10 'money' it.
  21.  
  22.         int amountOfMoneyLeftOnAccount1 = account1.money; // this will be set to 20 because the account has 30-10=20 'money' in it.
  23.         System.out.println(account1.owner + " has " + amountOfMoneyLeftOnAccount1 + " money left on account " + account1.id + "."); // This will print out: owner1 has 20 money left on account 1.
  24.        
  25.         // The execution of the program ends here.
  26.     }
  27. }
  28.  
  29. class MyBankAccount {  // MyBankAccount is the name of the class
  30.     // Fields/Variables/Attributes
  31.     int id;
  32.     String owner;
  33.     int money;
  34.  
  35.     // Constructor
  36.     MyBankAccount(int initId, String initOwner, int initMoney) {
  37.         id = initId;
  38.         owner = initOwner;
  39.         money = initMoney;
  40.     } // End of constructor
  41.  
  42.     // Methods/Functions
  43.     void depositMoney(int newMoney) {
  44.         money = money + newMoney;
  45.     } // End of depositMoney
  46.  
  47.     Boolean withdrawMoney(int withdrawnMoney) {
  48.         if (money - withdrawnMoney < 0) {  // Checks if there's enough money in the account)
  49.             return false;
  50.         } else {
  51.             money = money - withdrawnMoney;
  52.             return true;
  53.         } // End of else clause
  54.     } // End of withdrawnMoney
  55. } // End of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement