diff options
| -rw-r--r-- | java/08-runnable-jar/.gitignore | 2 | ||||
| -rw-r--r-- | java/08-runnable-jar/README | 14 | ||||
| -rw-r--r-- | java/08-runnable-jar/src/Account.java | 26 | ||||
| -rw-r--r-- | java/08-runnable-jar/src/AccountApp.java | 16 | ||||
| -rw-r--r-- | java/08-runnable-jar/src/AccountManager.java | 39 |
5 files changed, 97 insertions, 0 deletions
diff --git a/java/08-runnable-jar/.gitignore b/java/08-runnable-jar/.gitignore new file mode 100644 index 0000000..e493dd6 --- /dev/null +++ b/java/08-runnable-jar/.gitignore @@ -0,0 +1,2 @@ +bin +dist diff --git a/java/08-runnable-jar/README b/java/08-runnable-jar/README new file mode 100644 index 0000000..7746431 --- /dev/null +++ b/java/08-runnable-jar/README @@ -0,0 +1,14 @@ +# NOTE: +# On Windows, +# 1. Use \ as the directory seperator. + +# Compile: +mkdir bin +javac --source-path src/ -d bin/ src/*.java + +# Build jar file: +mkdir dist +jar cfe dist/my-accounts-app.jar AccountApp -C bin . + +# Run +java -jar dist/my-accounts-app.jar diff --git a/java/08-runnable-jar/src/Account.java b/java/08-runnable-jar/src/Account.java new file mode 100644 index 0000000..4b26eca --- /dev/null +++ b/java/08-runnable-jar/src/Account.java @@ -0,0 +1,26 @@ +public class Account { + + /* Declaring fields - represent state */ + + int accountNumber; + double balance; + + public Account(int accountNumber, double balance) { + this.accountNumber = accountNumber; + this.balance = balance; + } + + public int getAccountNumber() { + return accountNumber; + } + public void setAccountNumber(int accountNumber) { + this.accountNumber = accountNumber; + } + public double getBalance() { + return balance; + } + public void setBalance(double balance) { + this.balance = balance; + } + +} diff --git a/java/08-runnable-jar/src/AccountApp.java b/java/08-runnable-jar/src/AccountApp.java new file mode 100644 index 0000000..3399770 --- /dev/null +++ b/java/08-runnable-jar/src/AccountApp.java @@ -0,0 +1,16 @@ +public class AccountApp { + + /** + * @param args + */ + public static void main(String[] args) { + AccountManager accountManager = new AccountManager(); + + System.out.println(accountManager.getBalance(2)); + + accountManager.deposit(2, 2.0); + + System.out.println(accountManager.getBalance(2)); + } + +} diff --git a/java/08-runnable-jar/src/AccountManager.java b/java/08-runnable-jar/src/AccountManager.java new file mode 100644 index 0000000..5a3368c --- /dev/null +++ b/java/08-runnable-jar/src/AccountManager.java @@ -0,0 +1,39 @@ +import java.util.HashMap; +import java.util.Map; + +public class AccountManager { + + private Map<Integer, Account> accounts = new HashMap<>(); + + /* + This is a called a constructor. It gets executed when an object of this class is + created. + */ + public AccountManager() { + // For testing, create some account objects. + // In a real systems, account data will be in a storage system like a database. + + Account account = new Account(1, 10.0); + accounts.put(account.getAccountNumber(), account); + + account = new Account(2, 20.0); + accounts.put(account.getAccountNumber(), account); + + account = new Account(3, 10.0); + accounts.put(account.getAccountNumber(), account); + } + + public void withdraw(int accountId, double amount) { + Account account = accounts.get(accountId); + account.setBalance(account.getBalance() - amount); + } + + public void deposit(int accountId, double amount) { + Account account = accounts.get(accountId); + account.setBalance(account.getBalance() + amount); + } + + public double getBalance(int accountId) { + return accounts.get(accountId).getBalance(); + } +} |
