Expense Tracker App

Expense Tracker App

To build a Single Page Application (SPA) for the Expense Tracker App.

?? This Expense Tracker is a simple yet effective web application designed to help users keep track of their daily expenses.

?? Built using React JS Hooks , this tool allows users to add, view, and manage their costs by specifying the expense name, amount, date, and category (Food, Transport, Entertainment, or Utilities).

?? It supports functionalities like adding new expenses, removing individual entries, clearing all costs, and dynamically updating the total expense value. ?? It is an excellent tool for personal finance management, offering a clean and user-friendly interface.

import React, { useState } from 'react';

import './App.css';

const App = () => {

const [expenses, setExpenses] = useState([]);

const [name, setName] = useState('');

const [amount, setAmount] = useState('');

const [date, setDate] = useState('');

const [category, setCategory] = useState('Food');

const addExpense = (e) => {

e.preventDefault();

if (!name || !amount || !date) return;

setExpenses([

...expenses,

{

id: Date.now(),

name,

amount: parseFloat(amount),

date,

category,

},

]);

setName('');

setAmount('');

setDate('');

};

const removeExpense = (id) => {

setExpenses(expenses.filter((expense) => expense.id !== id));

};

const clearExpenses = () => {

setExpenses([]);

};

const totalExpense = expenses.reduce((total, expense) => total + expense.amount, 0);

return (

<div className="app-container">

<h1>Expense Tracker</h1>

<form className="expense-form" onSubmit={addExpense}>

<input

type="text"

placeholder="Expense Name"

value={name}

onChange={(e) => setName(e.target.value)}

/>

<input

type="number"

placeholder="Amount"

value={amount}

onChange={(e) => setAmount(e.target.value)}

/>

<input

type="date"

value={date}

onChange={(e) => setDate(e.target.value)}

/>

<select value={category} onChange={(e) => setCategory(e.target.value)}>

<option value="Food">Food</option>

<option value="Transport">Transport</option>

<option value="Entertainment">Entertainment</option>

<option value="Utilities">Utilities</option>

</select>

<button type="submit">Add Expense</button>

</form>

<div className="expense-list">

{expenses.length === 0 ? (

<p>No expenses added yet.</p>

) : (

expenses.map((expense) => (

<div key={expense.id} className="expense-item">

<div>

<h4>{expense.name}</h4>

<p>

{expense.category} | {expense.date}

</p>

</div>

<div>

<p>RS - {expense.amount.toFixed(2)}</p>

<button onClick={() => removeExpense(expense.id)}>Remove</button>

</div>

</div>

))

)}

</div>

<div className="summary">

<h3>Total Expense: RS - {totalExpense.toFixed(2)}</h3>

<button className="clear-button" onClick={clearExpenses}>

Clear All

</button>

</div>

</div>

);

};

export default App;


App.css


body {

font-family: Arial, sans-serif;

margin: 0;

padding: 0;

box-sizing: border-box;

}

.app-container {

text-align: center;

padding: 20px;

max-width: 600px;

margin: auto;

}

h1 {

color: #333;

}

/* Expense Form */

.expense-form {

display: flex;

flex-wrap: wrap;

gap: 10px;

margin-bottom: 20px;

}

.expense-form input,

.expense-form select,

.expense-form button {

padding: 10px;

border: 1px solid #ccc;

border-radius: 4px;

flex: 1 1 calc(33.33% - 20px);

}

.expense-form button {

flex: 1 1 100%;

background-color: #007bff;

color: white;

cursor: pointer;

}

.expense-form button:hover {

background-color: #0056b3;

}

/* Expense List */

.expense-list {

text-align: left;

margin-bottom: 20px;

}

.expense-item {

display: flex;

justify-content: space-between;

align-items: center;

margin-bottom: 10px;

padding: 10px;

border: 1px solid #ddd;

border-radius: 4px;

background-color: #f9f9f9;

}

.expense-item button {

background-color: #dc3545;

color: white;

border: none;

padding: 5px 10px;

cursor: pointer;

}

.expense-item button:hover {

background-color: #b02a37;

}

/* Summary Section */

.summary {

margin-top: 20px;

}

.clear-button {

background-color: red;

color: white;

border: none;

padding: 10px 15px;

cursor: pointer;

}

.clear-button:hover {

background-color: darkred;

}



Output



Giridhar B

HTML | CSS | Bootstrap | Bulma | Tailwind CSS | Javascript | React | Redux | Java | Springboot | MySQL | DSA | C++ | Power BI | Power Platform

2 个月

Good work Sridhar Raj P

Dr. Saravanan Rajaram

???? Passionate Assistant Professor ?? | Electronics & Communication Engineering | ?? Winning Team Mentor ?? SIH 2024 Grand Finale ?? ?? Certified Adobe Creative Educator ??? PSNA College Of Engg & Tech, Dindigul ???

2 个月

Good Work Congrats ?? Sridhar Raj P

要查看或添加评论,请登录

Sridhar Raj P的更多文章

  • Lists and Keys in React JS

    Lists and Keys in React JS

    In React, we use lists to render multiple components dynamically, and keys help React identify which items have…

  • Object State Management

    Object State Management

    Object State Management Managing object state with in React is common when handling complex data structures like user…

  • useState Example

    useState Example

    Here are examples of using the hook in React functional components. Each example demonstrates a different use case.

  • Examples of using the useState hook in React Functional Components

    Examples of using the useState hook in React Functional Components

    Examples of using the useState hook in React Functional Components 1. Counter Example - ? Basic counter with increment…

  • Array, Array of Objects Values using Functional Component

    Array, Array of Objects Values using Functional Component

    Example 1: Displaying an Array of Strings import React from react; const FruitsList = () = { const fruits = [Apple…

    1 条评论
  • Hooks

    Hooks

    What are Hooks in React JS? Hooks in React allow functional components to manage state and side effects, which were…

  • State in Functional Components (useState Hook)

    State in Functional Components (useState Hook)

    State in Functional Components (useState Hook) In functional components, state is managed using the hook instead of and…

  • React State in Class Component

    React State in Class Component

    React State In React, state is used to store and manage component-specific data that can change over time. Unlike…

  • React JS Props

    React JS Props

    React JS Props (Properties) Props (short for "properties") in React are used to pass data from one component to…

  • Employee Management System - JSON Server

    Employee Management System - JSON Server

    A simple Employee Management System using React JS Hooks. It includes CRUD operations (Create, Read, Update, Delete)…

社区洞察

其他会员也浏览了