Skip to content

Commit

Permalink
added 'remove by id' option
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodnf committed Feb 13, 2020
1 parent ab63d13 commit 8c54ea6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<version>1.0.1</version>
<version>1.1.0</version>
<artifactId>grocery</artifactId>
<groupId>org.iselab.grocery</groupId>
<name>Grocery</name>
Expand Down
46 changes: 35 additions & 11 deletions src/main/java/org/iselab/grocery/controller/ProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ public void listAll() {
SystemUtils.pressEnterKeyToContinue();
}

public void removeById() {

SystemUtils.clearScreen();

SystemUtils.printHeader("Products", "Remove by Id");

System.out.print("Id: ");

int id = SystemUtils.getIntFromKeyboard();

System.out.println("----------------");

Product removed = productRepository.removeById(id);

if (removed == null) {
System.out.println("Product not found");
} else {
System.out.println("Product " + removed.getName() + " was successfully removed");
}

SystemUtils.pressEnterKeyToContinue();
}

public void searchByName() {

SystemUtils.clearScreen();
Expand Down Expand Up @@ -95,17 +118,18 @@ public void start() {
while (option != 9) {

switch (option) {
case 1:
addProduct();
break;
case 2:
searchByName();
break;
case 4:
listAll();
break;
default:
break;
case 1:
addProduct();
break;
case 2:
searchByName();
break;
case 3:
removeById();
break;
case 4:
listAll();
break;
}

option = showMenu();
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/iselab/grocery/repository/ProductRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public void save(Product product) {
public List<Product> findAll() {
return products;
}

public Product findById(int id) {

for (Product product : products) {

if (product.getId() == id) {
return product;
}
}

return null;
}

public List<Product> findByName(String name) {

Expand All @@ -30,4 +42,17 @@ public List<Product> findByName(String name) {

return found;
}

public Product removeById(int id) {

Product found = findById(id);

if (found == null) {
return null;
}

products.remove(found);

return found;
}
}

0 comments on commit 8c54ea6

Please sign in to comment.