0% found this document useful (0 votes)
91 views7 pages

Online Food Order Java SP

Uploaded by

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

Online Food Order Java SP

Uploaded by

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

Online Food Ordering Application using Java

Spring Boot
1. Project Overview

The app will allow users to browse through menus, select items, and place
orders online. There will be two key roles:

● Customer: Browses the menu, places orders, and makes payments.


● Admin/Restaurant Owner: Manages food items, menu categories, and
order processing.

2. Tech Stack

● Backend: Java Spring Boot


● Frontend: ReactJS
● Database: MySQL or PostgreSQL
● Build Tools: Maven/Gradle
● Security: Spring Security + JWT
● Payment Gateway: Stripe or Razorpay
● Server: Tomcat (embedded in Spring Boot)

3. Backend: Java Spring Boot Setup

a. Initialize Spring Boot Project

● Use Spring Initializr to generate a Spring Boot project:


○ Dependencies:
■ Spring Web
■ Spring Data JPA
■ Spring Security
■ MySQL Driver
■ Spring Boot DevTools
■ Lombok (for boilerplate code)
■ JWT Authentication (Optional)
■ Stripe (for payment)
spring init
--dependencies=web,data-jpa,security,mysql,lombok,devtools,jwt
stripe online-food-ordering

b. Database Configuration

● In application.properties or application.yml, configure the MySQL


database:

spring.datasource.url=jdbc:mysql://localhost:3306/food_ordering_
db
spring.datasource.username=root
spring.datasource.password=rootpassword
spring.jpa.hibernate.ddl-auto=update

c. Entities & Repositories

Create entity classes for User, Food Item, Order, and Menu. Use Spring Data
JPA for repository management.

@Entity
public class FoodItem {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
private String category;
private String description;
// Getters, setters, and constructors
}

d. Service Layer

Create services for handling business logic.

@Service
public class FoodItemService {
@Autowired
private FoodItemRepository foodItemRepository;

public List<FoodItem> getAllFoodItems() {


return foodItemRepository.findAll();
}

public FoodItem saveFoodItem(FoodItem foodItem) {


return foodItemRepository.save(foodItem);
}
}

e. Controller Layer

Expose REST APIs to handle requests for food items, users, and orders.

@RestController
@RequestMapping("/api/food")
public class FoodItemController {

@Autowired
private FoodItemService foodItemService;

@GetMapping("/items")
public ResponseEntity<List<FoodItem>> getAllFoodItems() {
return
ResponseEntity.ok(foodItemService.getAllFoodItems());
}

@PostMapping("/add")
public ResponseEntity<FoodItem> addFoodItem(@RequestBody
FoodItem foodItem) {
return
ResponseEntity.ok(foodItemService.saveFoodItem(foodItem));
}
}
f. Authentication and Authorization

● Use Spring Security with JWT for securing APIs. Create roles for Admin
and Customer.
● Define custom filters for JWT authentication.

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{

@Override
protected void configure(HttpSecurity http) throws Exception
{
http.csrf().disable()
.authorizeRequests()
.antMatchers("/api/admin/**").hasRole("ADMIN")
.antMatchers("/api/customer/**").hasRole("CUSTOMER")

.and().sessionManagement().sessionCreationPolicy(SessionCreation
Policy.STATELESS);
}
}

g. Payment Gateway Integration

Integrate with Stripe for handling payments using their Java SDK.

4. Frontend: React Setup

a. Initialize React Project


Use Create React App to set up the frontend.

npx create-react-app online-food-ordering-frontend

b. Routing with React Router

Set up routing for different pages like Menu, Cart, and Order History.

import { BrowserRouter as Router, Route, Switch } from


'react-router-dom';
import Menu from './components/Menu';
import Cart from './components/Cart';

function App() {
return (
<Router>
<Switch>
<Route path="/menu" component={Menu} />
<Route path="/cart" component={Cart} />
</Switch>
</Router>
);
}

export default App;

c. Fetching Data from Backend

Use Axios or Fetch API to make API calls to the Spring Boot backend.

useEffect(() => {
axios.get('/api/food/items')
.then(response => setFoodItems(response.data))
.catch(error => console.error(error));
}, []);

d. Cart Functionality

Implement cart functionality using React useState and Context API.

e. Payment Integration

Add Stripe or Razorpay components to handle customer payments. Use React


hooks to manage payment flows.

5. Database Design

a. User Table

Fields: id, name, email, password, role

b. FoodItem Table

Fields: id, name, category, price, description

c. Order Table

Fields: id, user_id, total_amount, status

d. OrderDetails Table

Fields: order_id, food_item_id, quantity, price

6. Testing and Deployment

a. Unit and Integration Testing

● Write unit tests for services using JUnit and Mockito.


● Test controllers with Spring MVC Test.

@RunWith(SpringRunner.class)
@WebMvcTest(FoodItemController.class)
public class FoodItemControllerTest {
@Autowired
private MockMvc mockMvc;

@Test
public void shouldReturnAllFoodItems() throws Exception {
mockMvc.perform(get("/api/food/items"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(5)); //
Expecting 5 food items
}
}

b. Dockerize the Application

Create a Dockerfile to containerize the backend.

FROM openjdk:17-jdk-slim
COPY target/online-food-ordering.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

For the frontend, use nginx or serve to host the React app.

npx serve -s build

c. Deploy to Cloud

Deploy the backend using Heroku, AWS Elastic Beanstalk, or Google Cloud
Run. For the frontend, deploy using Vercel or Netlify.

You might also like