Skip to content
Open

Task_1 #1303

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.kotlin

### IntelliJ IDEA ###
.idea/
*.iws
*.iml
*.ipr
out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
.code-workspace
*.code-workspace

### Mac OS ###
.DS_Store
.AppleDouble
.LSOverride
._*
.Spotlight-V100
.Trashes
Thumbs.db
ehthumbs.db
Desktop.ini
85 changes: 83 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,93 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>praktikum</artifactId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.9.2</junit.version>
<mockito.version>5.17.0</mockito.version>
</properties>

</project>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.platform</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M9</version>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<configuration>
<excludes>
<exclude>**/Database.class</exclude>
<exclude>**/Praktikum.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<phase>initialize</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
25 changes: 25 additions & 0 deletions src/main/java/ru/yandex/praktikum/Bun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.yandex.praktikum;

/**
* Модель булочки для бургера.
* Булочке можно дать название и назначить цену.
*/
public class Bun {

public String name;
public float price;

public Bun(String name, float price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public float getPrice() {
return price;
}

}
57 changes: 57 additions & 0 deletions src/main/java/ru/yandex/praktikum/Burger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ru.yandex.praktikum;

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

/**
* Модель бургера.
* Бургер состоит из булочек и ингредиентов (начинка или соус).
* Ингредиенты можно перемещать и удалять.
* Можно распечать чек с информацией о бургере.
*/
public class Burger {

public Bun bun;
public List<Ingredient> ingredients = new ArrayList<>();

public void setBuns(Bun bun) {
this.bun = bun;
}

public void addIngredient(Ingredient ingredient) {
ingredients.add(ingredient);
}

public void removeIngredient(int index) {
ingredients.remove(index);
}

public void moveIngredient(int index, int newIndex) {
ingredients.add(newIndex, ingredients.remove(index));
}

public float getPrice() {
float price = bun.getPrice() * 2;

for (Ingredient ingredient : ingredients) {
price += ingredient.getPrice();
}

return price;
}

public String getReceipt() {
StringBuilder receipt = new StringBuilder(String.format("(==== %s ====)%n", bun.getName()));

for (Ingredient ingredient : ingredients) {
receipt.append(String.format("= %s %s =%n", ingredient.getType().toString().toLowerCase(),
ingredient.getName()));
}

receipt.append(String.format("(==== %s ====)%n", bun.getName()));
receipt.append(String.format("%nPrice: %f%n", getPrice()));

return receipt.toString();
}

}
36 changes: 36 additions & 0 deletions src/main/java/ru/yandex/praktikum/Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.yandex.praktikum;

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

/**
* Класс с методами по работе с базой данных.
*/
public class Database {

private final List<Bun> buns = new ArrayList<>();
private final List<Ingredient> ingredients = new ArrayList<>();

public Database() {
buns.add(new Bun("black bun", 100));
buns.add(new Bun("white bun", 200));
buns.add(new Bun("red bun", 300));

ingredients.add(new Ingredient(IngredientType.SAUCE, "hot sauce", 100));
ingredients.add(new Ingredient(IngredientType.SAUCE, "sour cream", 200));
ingredients.add(new Ingredient(IngredientType.SAUCE, "chili sauce", 300));

ingredients.add(new Ingredient(IngredientType.FILLING, "cutlet", 100));
ingredients.add(new Ingredient(IngredientType.FILLING, "dinosaur", 200));
ingredients.add(new Ingredient(IngredientType.FILLING, "sausage", 300));
}

public List<Bun> availableBuns() {
return buns;
}

public List<Ingredient> availableIngredients() {
return ingredients;
}

}
32 changes: 32 additions & 0 deletions src/main/java/ru/yandex/praktikum/Ingredient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.yandex.praktikum;

/**
* Модель ингредиента.
* Ингредиент: начинка или соус.
* У ингредиента есть тип (начинка или соус), название и цена.
*/
public class Ingredient {

public IngredientType type;
public String name;
public float price;

public Ingredient(IngredientType type, String name, float price) {
this.type = type;
this.name = name;
this.price = price;
}

public float getPrice() {
return price;
}

public String getName() {
return name;
}

public IngredientType getType() {
return type;
}

}
11 changes: 11 additions & 0 deletions src/main/java/ru/yandex/praktikum/IngredientType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.yandex.praktikum;

/**
* Перечисление с типами ингредиентов.
* SAUCE – соус
* FILLING – начинка
*/
public enum IngredientType {
SAUCE,
FILLING
}
38 changes: 38 additions & 0 deletions src/main/java/ru/yandex/praktikum/Praktikum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.yandex.praktikum;

import java.util.List;

public class Praktikum {

public static void main(String[] args) {
// Инициализируем базу данных
Database database = new Database();

// Создадим новый бургер
Burger burger = new Burger();

// Считаем список доступных булок из базы данных
List<Bun> buns = database.availableBuns();

// Считаем список доступных ингредиентов из базы данных
List<Ingredient> ingredients = database.availableIngredients();

// Соберём бургер
burger.setBuns(buns.get(0));

burger.addIngredient(ingredients.get(1));
burger.addIngredient(ingredients.get(4));
burger.addIngredient(ingredients.get(3));
burger.addIngredient(ingredients.get(5));

// Переместим слой с ингредиентом
burger.moveIngredient(2, 1);

// Удалим ингредиент
burger.removeIngredient(3);

// Распечатаем рецепт бургера
System.out.println(burger.getReceipt());
}

}
35 changes: 35 additions & 0 deletions src/test/java/ru/yandex/praktikum/BunTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.yandex.praktikum;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class BunTest {

@Test
@DisplayName("Получение имени bun")
public void getBunName() {

Bun bun = new Bun("black bun", 100);

assertEquals("black bun", bun.getName());
}

@Test
@DisplayName("Получение цены bun")
public void getBunPrice() {

Bun bun = new Bun("white bun", 1000);

assertEquals(1000, bun.getPrice(), 0.001);
}

@Test
@DisplayName("Получение дробной цены bun")
void getFloatPrice() {
Bun bun = new Bun("red bun", 99.999f);

assertEquals(99.999f, bun.getPrice(), 0.0001);
}
}
Loading