Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sebastianofazzino/number-game

A Java Spring Boot application for a number guessing game where players bet and guess numbers. Includes RESTful API, data validation, and comprehensive unit and integration tests.
https://github.com/sebastianofazzino/number-game

java junit mockito restful springboot

Last synced: 4 days ago
JSON representation

A Java Spring Boot application for a number guessing game where players bet and guess numbers. Includes RESTful API, data validation, and comprehensive unit and integration tests.

Awesome Lists containing this project

README

        

# Number Game

## Overview

This project is a Java Spring Boot implementation of a number-based betting game. It provides a simple API where players can place bets and receive winnings based on a random number generated by the server. The game demonstrates various aspects of Spring Boot development, including RESTful services, data validation, and testing.

## Key Features

- **Game Mechanics**: Players submit a number between 1 and 100 along with a bet amount.
The server generates a random number within the same range. If the player's number is higher, the winnings are calculated based on the bet and the difference between the player's number and the generated number.
- **REST API**: Exposes a single endpoint to play a round of the game.
- **Data Validation**: Ensures valid input for the player's number and bet amount.
- **Testing**: Includes unit and integration tests to verify the functionality and performance of the game logic.

## Technologies Used

- **Java 11**: The programming language used for the implementation.
- **Spring Boot 2.7.18**: Framework for building the RESTful API and application services.
- **JUnit 5**: Testing framework for unit and integration tests.
- **Mockito**: Framework for mocking and testing dependencies.
- **Validation API**: For ensuring data integrity in game inputs.

## Table of Contents

- [Features](#features)
- [Project Structure](#project-structure)
- [Dependencies](#project-dependencies)
- [Installation](#installation)
- [Summary Table](#summary-table)

## Features

### Game Mechanics
- **Gameplay**:
1. Players send a number between 1 and 100 along with a bet amount.
2. The server generates a random number in the same range.
3. If the player's number is greater than the generated number, the win is calculated as: `bet * (99 / selectedNumber)`.
4. The server returns the generated number, win status, and the amount won.

### REST API
- **Endpoint**: `POST /v1/number-game/play-round`
- **Request Body**:
```json
{
"selectedNumber": 50,
"placedBet": 100.0
}
```
- **Response**:
```json
{
"generatedNumber": 30,
"isWin": true,
"wonAmount": 198.0
}
```

### Data Validation
- **GameRound Class**: Ensures that the selected number is between 1 and 100 and the bet amount is at least 1.

## Project Structure

- **`src/main/java/`**: Contains the main source code.
- **`com.numbergame.controllers/`**: Includes the REST controller for handling game requests.
- **`com.numbergame.exception/`**: Contains the classes ExceptionHandler and ExceptionResponse.
- **`com.numbergame.models/`**: Contains the domain models such as `GameRound` and `GameResult`.
- **`com.numbergame.services/`**: Contains the game logic and service implementations.



- **`src/test/java/`**: Contains test code.
- **`com.numbergame.controllers/`**: Tests for the GameController.
- **`com.numbergame.services/`**: Tests for the GameService.
- **`com.numbergame.models/`**: Tests for the class GameRound.

## Project Dependencies

The project dependencies are managed via Maven. Here are the main dependencies used in the project:

- **Spring Boot Starter**: Core starter for Spring Boot applications.
```xml

org.springframework.boot
spring-boot-starter

```

- **Spring Boot Starter Web**: Provides support for building web applications.
```xml

org.springframework.boot
spring-boot-starter-web

```

- **Spring Boot Starter WebFlux:**: Provides support for building reactive web applications.
```xml

org.springframework.boot
spring-boot-starter-webflux

```

- **Spring Boot DevTools**: Provides additional development-time features such as automatic restarts and live reload.
```xml

org.springframework.boot
spring-boot-devtools
runtime
true

```

- **Lombok**: A library that helps reduce boilerplate code in Java.
```xml

org.projectlombok
lombok
true

```

- **Jakarta Validation API**: Provides bean validation support.
```xml

jakarta.validation
jakarta.validation-api

```

- **Hibernate Validator**: The reference implementation of the Jakarta Bean Validation API.
```xml

org.hibernate.validator
hibernate-validator

```

- **Jakarta Expression Language (EL)**: Provides expression language support.
```xml

org.glassfish
jakarta.el

```

- **Spring Boot Starter Test**: Provides testing support with libraries such as JUnit, Hamcrest, and Mockito.
```xml

org.springframework.boot
spring-boot-starter-test
test

```

- **Reactor Test**: Provides testing support for Reactor-based applications.
```xml

io.projectreactor
reactor-test
test

```

## Installation

To set up and run the project, follow these steps:

### 1. Clone the Repository

First, clone the repository to your local machine:

```sh
git clone https://github.com/SebastianoFazzino/number-game.git
cd number-game
```

### 2. Install Dependencies

To set up and run the project, follow these steps:

#### Prerequisites

1. **[Maven](https://maven.apache.org/)** - The build tool used for managing dependencies and building the project.
2. **[Java 11](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html)** - The Java Development Kit required to run the project.

#### Dependency Installation

Dependencies are managed via Maven. You can install them by running:

```sh
mvn install
```

#### Application Configuration

The application uses the default settings and does not require additional configuration for data storage. If you need to adjust any application properties, you can modify the `application.yml` file located in `src/main/resources`.

### 4. Build the Project

To build the project, run:

```sh
mvn compile
```

### 5. Run the Project

To start the application, execute:

```sh
mvn spring-boot:run
```
This command will start the application and expose the API endpoint /v1/number-game/play-round.

### 6. Run Tests

To run the tests, execute:

```sh
mvn test
```

## Summary Table

| Step | Command |
|--------------------------|-------------------------------------------------------------------------------------------|
| **Clone the Repository** | `git clone https://github.com/SebastianoFazzino/number-game`
`cd your-repo` |
| **Install Java** | [Java Installation](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html) |
| **Install Maven** | [Maven Installation](https://maven.apache.org/install.html) |
| **Build the Project** | `mvn compile` |
| **Run the Project** | `mvn spring-boot:run` |
| **Run Tests** | `mvn test` |
| **Clean the Project** | `mvn clean` |
| **Package the Project** | `mvn package` |

You can find an enhanced version of this project [here](https://github.com/SebastianoFazzino/enhanced-number-game.git).