Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by GianDavid on 22.05.2017.
- */
- import javafx.application.Application;
- import javafx.stage.Stage;
- class PriceCalc {
- public static void main(String[] args) {
- // The execution of the program starts here.
- MyBankAccount account1 = new MyBankAccount(1, "owner1", 0);
- MyBankAccount account2 = new MyBankAccount(2, "owner2", 0);
- account1.depositMoney(30);
- account2.depositMoney(500);
- account1.withdrawMoney(100); // this returns false because the account only has 30 'money' in it.
- account1.withdrawMoney(10); // this returns true because the account has more than 10 'money' it.
- int amountOfMoneyLeftOnAccount1 = account1.money; // this will be set to 20 because the account has 30-10=20 'money' in it.
- 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.
- // The execution of the program ends here.
- }
- }
- class MyBankAccount { // MyBankAccount is the name of the class
- // Fields/Variables/Attributes
- int id;
- String owner;
- int money;
- // Constructor
- MyBankAccount(int initId, String initOwner, int initMoney) {
- id = initId;
- owner = initOwner;
- money = initMoney;
- } // End of constructor
- // Methods/Functions
- void depositMoney(int newMoney) {
- money = money + newMoney;
- } // End of depositMoney
- Boolean withdrawMoney(int withdrawnMoney) {
- if (money - withdrawnMoney < 0) { // Checks if there's enough money in the account)
- return false;
- } else {
- money = money - withdrawnMoney;
- return true;
- } // End of else clause
- } // End of withdrawnMoney
- } // End of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement