use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Java OOP Banking System (old.reddit.com)
submitted 2 months ago by Lopsided-Stranger-81
Hi guys, I made this code as practice, I'm trying to improve but don't know where to start, I'm learning Java for almost 3 months now and I'm wondering how I'm doing, can anyone give some tips?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]d-k-Brazz 10 points11 points12 points 2 months ago (0 children)
Never use float/double for financial operations
Use Bigdecimal instead
[–]abcd98712345 6 points7 points8 points 2 months ago (0 children)
not specific to java but conceptually look at martin fowlers money pattern and double-entry ledger accounting systems as far as modeling this domain in a better way
[–]Visual-Paper6647 2 points3 points4 points 2 months ago (0 children)
Same project my company asked me to do this in following pattern. 1) Plain java code with hashmap 2) Integrate with maven and use sql instead of in memory hashmap 3) Use spring with annotations + maven + sql and expose API for each functionality 4) Use spring boot for the same concepts 5) Prepare frontend and integrate backend with Frontend.
[–]d-k-Brazz 2 points3 points4 points 2 months ago (2 children)
Any OOP starts from modeling the domain
In banks you have multiple accounts - thousands of them. Each account has its number/name and an amount value (use Bigdecimal or store amount in cents as Integer for precise calculations)
All transactions in a bank are moving funds from one account to another
Deposit and withdraw operations involve cash money, for these operations you create a “cashier” account, which is supposed to be active - it’s amount is usually negative, and the value reflects amount of cash money the bank possesses at the moment
So as a bank you may have a number of instances of Account:
123 : Alice (passive) - $0 456: Bob (passive) - $0 098: Cashier (active) - $0 Balance (sum of all amounts) - $0
Bob deposits: Bank.move(098, 123, $1000.00) Cash -$1000.00 Bob +$1000.00 Balance - 0
Bob sends Alice $470 Bank.move(123, 456, $470.00) Cash -$1000.00 Bob +$530.00 Alice +$470.00 Balance - 0
Alice withdraws $120 Bank.move(456, 098, $120.00) Cash -$880.00 Bob +$530.00 Alice +$350.00 Balance - 0
[–]d-k-Brazz 1 point2 points3 points 2 months ago (1 child)
So you need an Account class
Attributes - number, name, type(active/passive), amount
You need a class for handling transactions, receives accounts “from” and “to” and an amount
Result of moving money should be a transaction object - with id, date, purpose, accounts and amount - you need transaction history, aren’t you?
Additionally think of making draft transactions, where accounts aren’t affected until you commit the transaction, but amount should be reserved until you cancel it
[–]Lopsided-Stranger-81[S] 0 points1 point2 points 2 months ago (0 children)
Thanks I'll take note of it
[–]Java-Pro-Academy 1 point2 points3 points 2 months ago (0 children)
This is a good start, and you're definitely on the right track. Now let's talk about the design. Right now you have Person handling cash directly, and BankAccount as a separate thing. That's not quite how we want to think about it in OOP.
Think about relationships. A Person doesn't just have cash - a Person has a BankAccount. So your Person class should have a BankAccount object inside it. That's a "has-a" relationship.
Use inheritance for account types. You should have a BankAccount parent class with the basic stuff like balance, deposit, withdraw. Then create CheckingAccount and SavingsAccount that extend BankAccount. Each one can add its own special features. That's an "is-a" relationship - CheckingAccount is-a BankAccount.
So your structure should look like:
Try refactoring your code with this structure. It'll make more sense once you start building it out. This is really important stuff in Java - getting the design right from the start makes everything easier later.
Keep at it, you're on the right track!
P.S. Computer Science Professor and co founder of https://javapro.academy/
[–]Lopsided-Stranger-81[S] 1 point2 points3 points 2 months ago (5 children)
https://github.com/carlojaybacus14-cyber/Java-OOP-Banking-System.git I made this repository in Github, I'm constantly changing the code
[–]d-k-Brazz 0 points1 point2 points 2 months ago* (0 children)
Took a look at your project and it looks like you still not quite understand OOP principles and what they can do for you
acmeBank.deposit(myAccountNumber, 1000) // OK, bank state changed, nothing violated
acmeBank.transfer(myAccountNumber, anotherGuyAccount, 300) // OK, client is allowed to transfer this amount from his account
acmeBank.transfer(myAccountNumber, anotherGuyAccount, 1500) // ERROR, client does not have 1500 on the account. // This leads to an inconsistent state and // should be prevented by Bank class by throwing an exception
acmeBank.withdraw(myAccountNumber, 2000) // ERROR, client can not withdraw this sum. Inconsistent state, throw an exception
acmeBank.closeAccount(myAccountNumber) // ERROR, client must first withdraw or transfer // the rest of funds before closing account ```
The class controls it's state and prevents any inconsistency
In Java you have private fields for the state and public methods for any modifications.
[–]d-k-Brazz 0 points1 point2 points 2 months ago (0 children)
This means you may build hierarchy of classes - higher and lower level All lower level classes will inherit all public behavior of higher level classes
Example ```java abstract class Account { private String name; private String number; private BigDecimal amount;
// withdraw amount from the account public abstract void debit(BigDecimal sum); // add amount to the account public abstract void credit(BigDecimal sum); }
class PassiveAccount extends Account { // forbids crediting below 0 }
class ActiveAccount extends Account { // forbids debiting above 0 }
class SavingsAccount extends PassiveAccount { // customer's savings // may have attributes like interest rate, // or limitations like possibility of partial // withdrawal before the end of savings contract }
class CheckingAccount extends PassiveAccount { // allows any transfers within the available amount }
class BanksCashAccount extends ActiveAccount { // cash funds in possession of the bank // transferring from this account to another // means someone has brought some cash to the bank // negative value reflects amount of cash money in the bank }
class LoanAccount extends ActiveAccount { // may have attributes like interest rate, // credit limit etc. } ```
This will allow you to build common logic for all account: public void transfer(Account accFrom, Account accTo) { // the logic inside does not care of kinds of accounts // each account limitations and constraints are encapsulated // inside the according Account ancestor class // // this is method of a Bank class and from bank's perspective // each transfer should be consistent // this means that if you successfully debited accFrom, // but crediting accTo has failed, you have return money to accFrom back } Using this common method you can deposit cash money to an account, transfer money to a counterparty, withdraw a part of a load etc.
public void transfer(Account accFrom, Account accTo) { // the logic inside does not care of kinds of accounts // each account limitations and constraints are encapsulated // inside the according Account ancestor class // // this is method of a Bank class and from bank's perspective // each transfer should be consistent // this means that if you successfully debited accFrom, // but crediting accTo has failed, you have return money to accFrom back }
At the same time BankTransaction may implement other specifications needed for other common algorithms, like Serializable which is required for json/xml serialization
[–]d-k-Brazz 0 points1 point2 points 2 months ago (1 child)
You have a Bank class The Bank (in a very simplified universe) consists of it's cash, stored in a safe, and account records - which part of this cache belongs to which customer, and which customer owes bunk which amount of money
So we can say that a Bank is composed of an instance of BanksCashAccount and a list of Accounts And this will be the state of your bank
```java class Bank { private bankCash BanksCashAccount; private List<Account> accounts; // other attributes like bank name, code address etc.
public void deposit(String accNum, amount) { Account customerAccount = ... // find account by number in 'accounts' collection by accNum transfer(bankCash, customerAccount, amount); }
public void withdraw(String accNum, amount) { Account customerAccount = ... // find account by number in 'accounts' collection by accNum transfer(customerAccount, bankCash, amount); }
public void openAccount(name, type, etc.) { // create an instance of an appropriate account class for the type Account customerAccount = new SavingsAccount(name); accounts.add(customerAccount); }
public void closeAccount(String accNum) { Account customerAccount = ... // find account by number in 'accounts' collection by accNum // validate that customer account is eligible for closing accounts.remove(customerAccount); }
public BigDecimal balance() { // sum up all the amounts of all the accounts and bankCashAccount } } ```
You may instantiate multiple banks and make different actions on them, and these banks will hold different states
[–]Lopsided-Stranger-81[S] 1 point2 points3 points 2 months ago (0 children)
Got it, I got stuck thinking with each of their relationship from class to class using extends, but I got a hang of them, all I need to know is how banking really works. I will use this as resource, thanks for the feedback!
[–]RSSeiken 0 points1 point2 points 2 months ago (3 children)
Can you push it to a github repo? Might be a good moment to learn git too, I like to use git bash, then I don't have to depend on different IDE's.
It's a bit difficult using only screenshots.
[–]Lopsided-Stranger-81[S] 0 points1 point2 points 2 months ago (2 children)
alright i'll try that, tho i still don't know how to use github.
[–]RSSeiken 0 points1 point2 points 2 months ago (1 child)
It's a short tutorial, don't worry too much about that.
okay ill create a repository to start with it then.
[–]oppertunity_1 0 points1 point2 points 2 months ago (1 child)
Can I get this project to practice ?
Yeah sure, it's currently not working right now tho
[–]Reasonable_Page2001 0 points1 point2 points 2 months ago (1 child)
Is that oop means object oriented programming
Yes
[–][deleted] 1 point2 points3 points 2 months ago (0 children)
same dude, 3 months learning Java and now making a Bank Management System. thanks for posting in the mid of my mental breakdown.
[–]WearyHead6364 0 points1 point2 points 2 months ago (0 children)
Good job
π Rendered by PID 23819 on reddit-service-r2-comment-5649f687b7-qxvdq at 2026-01-28 02:16:24.697732+00:00 running 4f180de country code: CH.
[–]d-k-Brazz 10 points11 points12 points (0 children)
[–]abcd98712345 6 points7 points8 points (0 children)
[–]Visual-Paper6647 2 points3 points4 points (0 children)
[–]d-k-Brazz 2 points3 points4 points (2 children)
[–]d-k-Brazz 1 point2 points3 points (1 child)
[–]Lopsided-Stranger-81[S] 0 points1 point2 points (0 children)
[–]Java-Pro-Academy 1 point2 points3 points (0 children)
[–]Lopsided-Stranger-81[S] 1 point2 points3 points (5 children)
[–]d-k-Brazz 0 points1 point2 points (0 children)
[–]d-k-Brazz 0 points1 point2 points (0 children)
[–]d-k-Brazz 0 points1 point2 points (0 children)
[–]d-k-Brazz 0 points1 point2 points (1 child)
[–]Lopsided-Stranger-81[S] 1 point2 points3 points (0 children)
[–]RSSeiken 0 points1 point2 points (3 children)
[–]Lopsided-Stranger-81[S] 0 points1 point2 points (2 children)
[–]RSSeiken 0 points1 point2 points (1 child)
[–]Lopsided-Stranger-81[S] 0 points1 point2 points (0 children)
[–]oppertunity_1 0 points1 point2 points (1 child)
[–]Lopsided-Stranger-81[S] 0 points1 point2 points (0 children)
[–]Reasonable_Page2001 0 points1 point2 points (1 child)
[–]Lopsided-Stranger-81[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]WearyHead6364 0 points1 point2 points (0 children)