0% found this document useful (0 votes)
56 views

Expense Management Project Details

The document describes an expense management project implemented using object-oriented programming principles in Java. It includes classes for expenses, recurring expenses, regular expenses, users, authentication, and services to manage expenses. It also covers concepts like inheritance, polymorphism, encapsulation, collections, and exception handling.

Uploaded by

Pratham Katariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Expense Management Project Details

The document describes an expense management project implemented using object-oriented programming principles in Java. It includes classes for expenses, recurring expenses, regular expenses, users, authentication, and services to manage expenses. It also covers concepts like inheritance, polymorphism, encapsulation, collections, and exception handling.

Uploaded by

Pratham Katariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Expense management Project Details

Java:
1. Packages and Classes:
a. Packages: You've organized your project into packages (com.example.project11.Model, com.example.project11.Security,
com.example.project11.Service, com.example.project11.Util) to group related classes together and avoid naming conflicts.

b. Classes: Each class represents a blueprint for objects. For example, Expense, RecurringExpense, RegularExpense, Login,
Register, Security, User, ExpenseManager, ExpenseService, and Main.

2. Inheritance and Polymorphism:


a. Inheritance: It's a mechanism in Java where a new class (subclass) is derived from an existing class (superclass). For instance,
RecurringExpense and RegularExpense inherit properties and methods from the Expense class.

b. Polymorphism: It allows objects of different classes to be treated as objects of a common superclass (Expense), enabling
flexibility and extensibility in your code.

3. Encapsulation:
a. Private Fields and Public Methods: By making fields (date, category, amount, description, numOccurrences, frequency,
username, password, expenses, users) private and providing public methods (getters and setters), you encapsulate the internal state
of objects and control access to them.

4. Constructors and Methods:


a. Constructors: Constructors initialize objects of a class. For example, the constructors in Expense, RecurringExpense,
RegularExpense, User, ExpenseManager, Login, Register, and ExpenseService.

b. Methods: Methods represent behavior associated with objects. You've defined methods like getters, authenticate, addExpense,
deleteExpense, registerUser, authenticateUser, loginUser, etc.

5. Composition:
a. Composition: It's a design technique in OOP where a class contains an instance of another class. For example, classes like
Login, Register, and ExpenseService include instances of ExpenseManager to delegate tasks related to user management and
expense handling.

6. Control Flow:
a. Decision Making: You've used if-else statements in methods like authenticateUser in Security class to authenticate users based
on their credentials.

b. Looping Constructs: The while loop in Main class is used for continuous interaction with the user until the program is exited.
Also, for-each loop is used to iterate over expenses in methods like calculateTotalExpenses in ExpenseService.

Object-Oriented Programming (OOP):


1. Abstraction:
a. Abstraction: It hides complex implementation details and shows only essential features of an object. Your classes provide clear
interfaces for interaction, abstracting away the internal implementation details.

2. Inheritance:
a. Inheritance: It allows you to create new classes that are based on existing classes, inheriting their attributes and behaviors.
Subclasses like RecurringExpense and RegularExpense inherit properties and methods from the Expense class.
3. Polymorphism:
a. Polymorphism: It allows objects of different types to be treated as objects of a common superclass. For example, you can treat
both RecurringExpense and RegularExpense objects as Expense objects, enabling flexibility in method calls and code design.

4. Encapsulation:
a. Data Hiding and Encapsulation: Encapsulation restricts access to certain components of objects, preventing direct
modification. Private fields (username, password, etc.) are encapsulated within classes, and access is provided through public
methods.

Data Structures and Algorithms (DSA):


1. Collections Framework:
a. List Interface: It's a part of the Java Collections Framework that represents an ordered collection of elements. You've used
List<Expense> in ExpenseManager and ExpenseService to store and manipulate expenses.

2. HashMap:

a. HashMap: It's a part of the Java Collections Framework that stores key-value pairs. You've used HashMap<String, User> in
ExpenseManager to store user information with usernames as keys.

3. Iteration:
a. Iteration: It's the process of traversing through elements of a collection. You've used iteration to loop through expenses in
methods like calculateTotalExpenses to calculate the total expenses.

4. Basic Arithmetic Operations:


a. Arithmetic Operations: You've performed basic arithmetic operations (addition) to calculate the total expenses by iterating
through the list of expenses.

Additional Concepts:
1. Exception Handling:
a. Exception Handling: It's a mechanism to handle errors and exceptional situations gracefully. You may consider adding
exception handling mechanisms like try-catch blocks to deal with potential errors in user input or file operations.

2. User Input Handling:


a. Scanner Class: It's used for obtaining user input. You've used Scanner class to handle user input from the console in Main
class.
Project Codes
package com.example.project11.Model;

import java.util.Date;

public class Expense {


private Date date;
private String category;
private double amount;
private String description;

public Expense(String category, double amount, String description) {


this.date = new Date();
this.category = category;
this.amount = amount;
this.description = description;
}

public Date getDate() {


return date;
}

public String getCategory() {


return category;
}

public double getAmount() {


return amount;
}

public String getDescription() {


return description;
}
}
above is expense.java file code

package com.example.project11.Model;

public class RecurringExpense extends Expense {


private int numOccurrences;

public RecurringExpense(String category, double amount, String description, int numOccurrences) {


super(category, amount, description);
this.numOccurrences = numOccurrences;
}

public int getNumOccurrences() {


return numOccurrences;
}
}
above is recurring expense.java file code
package com.example.project11.Model;

public class RegularExpense extends Expense {


private String frequency;

public RegularExpense(String category, double amount, String description, String frequency) {


super(category, amount, description);
this.frequency = frequency;
}

public String getFrequency() {


return frequency;
}
}
above is regularexpense.java file code

package com.example.project11.Security;

import com.example.project11.Service.ExpenseManager;

public class Login {


private ExpenseManager expenseManager;

public Login(ExpenseManager expenseManager) {


this.expenseManager = expenseManager;
}

public User loginUser(String username, String password, Security security) {


User user = expenseManager.getUser(username);
if (security.authenticateUser(user, password)) {
return user;
}
return null;
}
}
above is login.java file code

package com.example.project11.Security;

import com.example.project11.Service.ExpenseManager;

public class Register {


private ExpenseManager expenseManager;

public Register(ExpenseManager expenseManager) {


this.expenseManager = expenseManager;
}

public void registerUser(String username, String password) {


expenseManager.registerUser(username, password);
}
}
above is register.java file code
package com.example.project11.Security;

public class Security {


public boolean authenticateUser(User user, String password) {
return user != null && user.authenticate(password);
}
}
above is security.java file code

package com.example.project11.Security;

import java.util.ArrayList;
import java.util.List;

import com.example.project11.Model.Expense;

public class User {


private String username;
private String password;
private List<Expense> expenses;

public User(String username, String password) {


this.username = username;
this.password = password;
this.expenses = new ArrayList<>();
}

public String getUsername() {


return username;
}

public boolean authenticate(String password) {


return this.password.equals(password);
}

public void addExpense(Expense expense) {


expenses.add(expense);
}

public void deleteExpense(Expense expense) {


expenses.remove(expense);
}

public List<Expense> getExpenses() {


return expenses;
}
}
above is user.java file code

package com.example.project11.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.project11.Model.Expense;
import com.example.project11.Security.User;

public class ExpenseManager {


private final Map<String, User> users;

public ExpenseManager() {
this.users = new HashMap<>();
}

public void registerUser(String username, String password) {


users.put(username, new User(username, password));
}

public boolean authenticateUser(String username, String password) {


User user = users.get(username);
return user != null && user.authenticate(password);
}

public void addExpense(String username, String category, double amount, String description) {
User user = users.get(username);
if (user != null) {
Expense expense = new Expense(category, amount, description);
user.addExpense(expense);
}
}

public void deleteExpense(String username, Expense expense) {


User user = users.get(username);
if (user != null) {
user.deleteExpense(expense);
}
}

public List<Expense> getExpenses(String username) {


User user = users.get(username);
return user != null ? user.getExpenses() : null;
}

public User getUser(String username) {


return users.get(username);
}
}
above is expensemanager.java file code

package com.example.project11.Service;

import com.example.project11.Model.Expense;
import com.example.project11.Security.User;

import java.util.List;

public class ExpenseService {


private final ExpenseManager expenseManager;

public ExpenseService(ExpenseManager expenseManager) {


this.expenseManager = expenseManager;
}

public void addExpense(User user, Expense expense) {


expenseManager.addExpense(user.getUsername(), expense.getCategory(), expense.getAmount(),
expense.getDescription());
}

public List<Expense> getExpenses(User user) {


return expenseManager.getExpenses(user.getUsername());
}

public double calculateTotalExpenses(List<Expense> expenses) {


double total = 0;
for (Expense expense : expenses) {
total += expense.getAmount();
}
return total;
}
}
above is expenseservice.java file code

package com.example.project11.Util;

import com.example.project11.Model.Expense;
import com.example.project11.Model.RegularExpense;
import com.example.project11.Model.RecurringExpense;
import com.example.project11.Security.User;
import com.example.project11.Security.Login;
import com.example.project11.Security.Register;
import com.example.project11.Security.Security;
import com.example.project11.Service.ExpenseManager;
import com.example.project11.Service.ExpenseService;

import java.util.List;
import java.util.Scanner;

public class Main {


private static User currentUser;
private static Security security;
private static Register register;
private static Login login;
private static ExpenseService expenseService;

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
ExpenseManager expenseManager = new ExpenseManager();
security = new Security();
register = new Register(expenseManager);
login = new Login(expenseManager);
expenseService = new ExpenseService(expenseManager);

System.out.println("Welcome to Expense Management System!");

while (true) {
System.out.println("\n1. Register\n2. Login\n3. Exit");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
scanner.nextLine();

switch (option) {
case 1:
registerUser(scanner);
break;
case 2:
loginUser(scanner);
break;
case 3:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid option!");
}
}
}

private static void registerUser(Scanner scanner) {


System.out.print("Enter username: ");
String regUsername = scanner.nextLine();
System.out.print("Enter password: ");
String regPassword = scanner.nextLine();
register.registerUser(regUsername, regPassword);
System.out.println("User registered successfully!");
}

private static void loginUser(Scanner scanner) {


System.out.print("Enter username: ");
String loginUsername = scanner.nextLine();
System.out.print("Enter password: ");
String loginPassword = scanner.nextLine();

currentUser = login.loginUser(loginUsername, loginPassword, security);

if (currentUser != null) {
System.out.println("Login successful!");
manageSession(scanner);
} else {
System.out.println("Invalid username or password!");
}
}

private static void manageSession(Scanner scanner) {


while (true) {
System.out.println("\n1. Add Expense\n2. View Expenses\n3. View Total Expenses\n4. Logout");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
scanner.nextLine();

switch (option) {
case 1:
addExpense(scanner);
break;
case 2:
viewExpenses();
break;
case 3:
viewTotalExpenses();
break;
case 4:
currentUser = null;
return;
default:
System.out.println("Invalid option!");
}
}
}

private static void addExpense(Scanner scanner) {


System.out.println("\n1. Add Regular Expense\n2. Add Recurring Expense\n3. Back");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
scanner.nextLine();

switch (option) {
case 1:
addRegularExpense(scanner);
break;
case 2:
addRecurringExpense(scanner);
break;
case 3:
return;
default:
System.out.println("Invalid option!");
}
}

private static void addRegularExpense(Scanner scanner) {


System.out.print("Enter category: ");
String category = scanner.nextLine();
System.out.print("Enter amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter description: ");
String description = scanner.nextLine();
System.out.print("Enter frequency: ");
String frequency = scanner.nextLine();

RegularExpense expense = new RegularExpense(category, amount, description, frequency);


expenseService.addExpense(currentUser, expense);
System.out.println("Regular Expense added successfully!");
}

private static void addRecurringExpense(Scanner scanner) {


System.out.print("Enter category: ");
String category = scanner.nextLine();
System.out.print("Enter amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter description: ");
String description = scanner.nextLine();
System.out.print("Enter number of occurrences: ");
int numOccurrences = scanner.nextInt();

RecurringExpense expense = new RecurringExpense(category, amount, description, numOccurrences);


expenseService.addExpense(currentUser, expense);
System.out.println("Recurring Expense added successfully!");
}

private static void viewExpenses() {


List<Expense> expenses = expenseService.getExpenses(currentUser);
if (expenses != null && !expenses.isEmpty()) {
System.out.println("Expenses:");
for (Expense expense : expenses) {
System.out.println("\n Date: " + expense.getDate() + " \n Category: " + expense.getCategory() +
" \n Amount: " + expense.getAmount() + " \n Description: " + expense.getDescription());
}
} else {
System.out.println("No expenses found.");
}
}

private static void viewTotalExpenses() {


List<Expense> expenses = expenseService.getExpenses(currentUser);
if (expenses != null && !expenses.isEmpty()) {
double total = expenseService.calculateTotalExpenses(expenses);
System.out.println("Total expenses: " + total);
} else {
System.out.println("No expenses found.");
}
}
}
above is main.java file code

You might also like