-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
java pizza project #2 file repository & test
- Loading branch information
Showing
6 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...IT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaFileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package pizza.repository; | ||
|
||
import pizza.domain.Pizza; | ||
|
||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.NoSuchFileException; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class PizzaFileRepository implements CrudRepository<Integer, Pizza> { | ||
private String fileName; | ||
private final String SPLIT_CHAR = ";"; | ||
|
||
public PizzaFileRepository(String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
@Override | ||
public Collection<Pizza> findAll() { | ||
Collection<Pizza> pizzas = new ArrayList<>(); | ||
try { | ||
List<String> lines = Files.readAllLines(Paths.get(fileName)); | ||
lines.forEach(str -> { | ||
String[] fields = str.split(SPLIT_CHAR); | ||
pizzas.add(new Pizza(Integer.valueOf(fields[0]), fields[1], fields[2], Integer.valueOf(fields[3]))); | ||
}); | ||
} catch (NoSuchFileException e) { | ||
// if file not found - return empty list | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return pizzas; | ||
} | ||
|
||
@Override | ||
public Pizza findById(Integer id) { | ||
Collection<Pizza> pizzas = findAll(); | ||
for (Pizza pizza : pizzas) { | ||
if (pizza.getId() == id) { | ||
return pizza; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void save(Pizza pizza) { | ||
// read all records from file | ||
Collection<Pizza> pizzas = findAll(); | ||
if (pizza.getId() == null) { | ||
// add record and generate id | ||
int id = 0; | ||
for (Pizza p : pizzas) { // TODO optimization | ||
if (id < p.getId()) { | ||
id = p.getId(); | ||
} | ||
} | ||
pizza.setId(id + 1); | ||
pizzas.add(pizza); | ||
} else { | ||
// update record | ||
for (Pizza p : pizzas) { | ||
if (p.getId() == pizza.getId()) { | ||
p.update(pizza.getName(), pizza.getComposition(), pizza.getPrice()); | ||
break; | ||
} | ||
} | ||
} | ||
// rewrite all records to file | ||
rewiteFile(pizzas); | ||
} | ||
|
||
@Override | ||
public void remove(Integer id) { | ||
Collection<Pizza> pizzas = findAll(); | ||
Pizza deletedPizza = null; | ||
for (Pizza pizza : pizzas) { | ||
if (pizza.getId() == id) { | ||
deletedPizza = pizza; | ||
break; | ||
} | ||
} | ||
// if pizza found by id | ||
if (deletedPizza != null) { | ||
pizzas.remove(deletedPizza); | ||
// rewrite all records to file | ||
rewiteFile(pizzas); | ||
} | ||
} | ||
|
||
private void rewiteFile(Collection<Pizza> pizzas) { | ||
try (FileWriter writer = new FileWriter(fileName)) { // TODO Buffered | ||
for (Pizza p : pizzas) { | ||
String str = p.getId() + SPLIT_CHAR + p.getName() + SPLIT_CHAR + p.getComposition() + SPLIT_CHAR + p.getPrice(); | ||
writer.write(str + "\n"); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/test/java/pizza/PizzaServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pizza; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import pizza.domain.Pizza; | ||
import pizza.repository.CrudRepository; | ||
import pizza.repository.PizzaFileRepository; | ||
import pizza.service.PizzaService; | ||
|
||
import java.util.Collection; | ||
|
||
public class PizzaServiceTest { | ||
final String PIZZA_FILE = "C:\\temp\\java_pizza_pizza_test.txt"; | ||
CrudRepository<Integer, Pizza> pizzaRepository; | ||
private PizzaService pizzaService; | ||
|
||
@BeforeEach | ||
public void init() { | ||
pizzaRepository = new PizzaFileRepository(PIZZA_FILE); | ||
pizzaService = new PizzaService(pizzaRepository); | ||
} | ||
|
||
@Test | ||
public void testAdd() { | ||
Collection<Pizza> pizzas = pizzaRepository.findAll(); | ||
Assertions.assertEquals(0, pizzas.size()); | ||
|
||
pizzaService.add("Pizza", "Components...", 100); | ||
pizzas = pizzaRepository.findAll(); | ||
Assertions.assertEquals(1, pizzas.size()); | ||
|
||
Pizza pizza = pizzas.iterator().next(); | ||
Assertions.assertEquals("Pizza", pizza.getName()); | ||
} | ||
} |