Implementing OOP!

Implementing OOP! Hello! I've been learning Java now for about two months and have this personal-finance application that saves data to a csv file and can add transactions, delete, sum by periods, etc, and am currently delving into object-oriented programming! I'm reading about Abstraction, Encapsulation, Innheritance and Polymorphism and am getting it on the technical front. Now, I would like to try and make my code more OOP- if that makes sense. By doing it on a code I'm familiar with I think I will better understand it, but I'm not sure where to start! Would anyone like to review my code and point me in the right direction to start? If I have to do some things over that's okay, originally my code was 400+ lines long but I managed to split it into classes somewhat. What suggestions do you have? Thank you very much! I put my repo to private, so I am not confident the link works but if it doesn't and you would like to take a look, please let me know! https://github.com/De1ora/Personal-finance-OOP.git I'm a beginner at git and github and this is my first time putting the repo to private!
6 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @De1ora! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
De1ora
De1oraOP2mo ago
GitHub
GitHub - De1ora/Personal-finance-OOP: Attempting to make my little ...
Attempting to make my little personal-finance application more object oriented. - GitHub - De1ora/Personal-finance-OOP: Attempting to make my little personal-finance application more object oriented.
noComment | Kez
noComment | Kez2mo ago
I recommend you post an actual example of where you struggle, users will be more likely to help, when they don't have to click through 4 classes/ 400 loc. Maybe there is a specific case that exemplifies where you struggle.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Tomasm21
Tomasm212mo ago
You apply static methods and call it through a class name well. Simple transactions through static methods work well. Everything is for a single user using static fields. To approach object oriented programming try to create an object directly. Consider what fields and methods can you hold there. You are holding public static double balance = 0.0; inside the AccountManager which has main(). In AccountManager you have 4 operations to deposit, to withdraw, to display transactions according selected option - year, month, day and to exit. You could create a class. The BankAccount class. And to encapsulate there balance and any other fields - name surname and other details. And some or all your operations like - deposit, withdraw, show balance:
public class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
this.balance = initialBalance;
}

public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds.");
}
}

public double getBalance() {
return balance;
}
}
public class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
this.balance = initialBalance;
}

public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds.");
}
}

public double getBalance() {
return balance;
}
}
Then in your AccountManager in main() you can use it:
BankAccount account = new BankAccount(100.0);
account.deposit(50.0);
System.out.println(account.getBalance()); // 150.0
BankAccount account = new BankAccount(100.0);
account.deposit(50.0);
System.out.println(account.getBalance()); // 150.0
For example:
case 2:
System.out.print("Enter amount to withdraw: ");
double withdraw;
try {
withdraw = scanner.nextDouble();
account.withdraw(withdraw);
} else if (withdraw <= 0) {
System.out.println("Invalid input. Withdrawal must be positive.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter a valid number.");
scanner.next(); // clear the invalid input
}
break;
case 2:
System.out.print("Enter amount to withdraw: ");
double withdraw;
try {
withdraw = scanner.nextDouble();
account.withdraw(withdraw);
} else if (withdraw <= 0) {
System.out.println("Invalid input. Withdrawal must be positive.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter a valid number.");
scanner.next(); // clear the invalid input
}
break;
If you are interested then I know how to apply inheritance and polymorphism for your TransactionManager to calculate sum.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server