Skip to content
Open
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
2 changes: 2 additions & 0 deletions .idea/Product-Inventory-Lab.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions chocolate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ {
"id" : 1,
"brand" : "cornetto",
"type" : "ice cream",
"quantity" : 5,
"price" : 10.0
} ]
14 changes: 7 additions & 7 deletions instructions/section-03.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Now that we have production models and services to manage them we can pull every
* Create a user interface to create, read, update and delete products

## Part 1 - Input Output
We will need to allow user input in order for the application to function. In order to do this we will use the Scanner class to accept input and methods from the System.out package to print to the console. In order to keep our code a bit cleaner as well as add a layer of abstraction we will use a Console class.
We will need to allow user input in order for the application to function. In order to do this we will use the Scanner class to accept input and methods from the System.out package to print to the console. In order to keep our code a bit cleaner as well as add a layer of abstraction we will use a io class.

```
package io;

public class Console {
public class io {

}
```
Expand All @@ -24,7 +24,7 @@ Notice that I've added this to an _io_ package.
Now we can keep repetitious System.out and Scanner calls out of the main application code. We can also use this class to print large output strings that would bloat our code.

```
public class Console {
public class io {
public static void printWelcome(){
System.out.println("" +
"**************************************************" +
Expand All @@ -40,10 +40,10 @@ public class Console {
Having this print line string in the code is large and litters up the main code. Having it abstracted away alows us to keep the main code clean and easy to read. We will simply call the _printWelcome()_ behaviour wherever we want to call this code.

```
Console.printWelcome()
io.printWelcome()
```

Whenever we want to capture user input or display output we will create methods in the Console class to handle these behaviours.
Whenever we want to capture user input or display output we will create methods in the io class to handle these behaviours.

## Part 2 - Application class
Now it is time to put all of these classes we have created to work. We will begin by creating a App class to initialize the application logic and initialize the services. This is the top most class and will start the program
Expand Down Expand Up @@ -76,7 +76,7 @@ public class App {
// (4)
// application logic here
// call methods to take user input and interface with services
Console.printWelcome();
io.printWelcome();
}
}
```
Expand All @@ -97,7 +97,7 @@ Now that we have a way to start the application, create the rest of the code to

## Conclusion

Creating a Console class allowed us to abstract away user interface components of the code. Now the main code isn't concerned with how UI is done, it is only concerned with what it does. We created an application class to house the elements of the program. Finally, creating a user interface to allow a user to interact (Create, Read, Update, and Delete) with items in the inventory.
Creating a io class allowed us to abstract away user interface components of the code. Now the main code isn't concerned with how UI is done, it is only concerned with what it does. We created an application class to house the elements of the program. Finally, creating a user interface to allow a user to interact (Create, Read, Update, and Delete) with items in the inventory.



21 changes: 20 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,24 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>

</project>
</project>
59 changes: 59 additions & 0 deletions src/main/java/Models/Chocolate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package Models;

public class Chocolate {
private int id;
private String brand;
private String type;
private int quantity;
private float price;

public Chocolate(){
}

public Chocolate(int id,String brand,String type,int quantity,float price){
this.brand=brand;
this.id = id;
this.type=type;
this.quantity=quantity;
this.price=price;
}
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public float getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}
}
75 changes: 75 additions & 0 deletions src/main/java/Models/Clothing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package Models;

public class Clothing {
private int id;
private String name;
private String brand;
private int size;
private int quantity;
private float price;

public Clothing(int id,String name,String brand,int size,int quantity,float price){
this.id=id;
this.name=name;
this.brand=brand;
this.size=size;
this.quantity=quantity;
this.price=price;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public Clothing()
{}




public String getName(){
return name;
}

}

123 changes: 123 additions & 0 deletions src/main/java/Services/ChocolateService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package Services;

import Models.Chocolate;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectWriter;
import utils.CSVUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ChocolateService {
private static int nextId=1;
private ArrayList<Chocolate> inventory = new ArrayList<>();



public Chocolate create(String brand,String type,int quantity,float price){

Chocolate createdChocolate = new Chocolate(nextId++,brand,type,quantity,price);

inventory.add(createdChocolate);

return createdChocolate;
}

public Chocolate findChocolate(String brand) {
for (Chocolate findObject : inventory) {
if (findObject.getBrand().equals(brand)) {
return findObject;
}
} return null;
}

public Chocolate[] findAll(){
// ArrayList<Chocolate> inventoryClone =(ArrayList<Chocolate>) inventory.clone();
// return (Chocolate[]) inventoryClone.toArray();
return inventory.toArray(new Chocolate[0]);
}

public Boolean update(String brand,Integer quantity){
for(Chocolate updateQuantity:inventory){
if(updateQuantity.getBrand().equals(brand)){
updateQuantity.setQuantity(quantity);
return true;
}
}
return false;
}
public boolean delete(String brand){
for(Chocolate deleteObject : inventory){
if(deleteObject.getBrand().equals(brand)){
return true;
}
}
return false;
}

public void writeCSVUtils() throws IOException {
String csvFile = "/Users/batman/Desktop/Chocolate.csv";
FileWriter writer = new FileWriter(csvFile); //(1)

CSVUtils.writeLine(writer, new ArrayList<String>(Arrays.asList(String.valueOf(nextId)))); // (2)

for (Chocolate s : inventory) {
List<String> list = new ArrayList<>(); // (3)
// list.add(String.valueOf(s.getId()));
list.add(s.getBrand());
list.add(s.getType());
// list.add(s.getSport());
list.add(String.valueOf(s.getQuantity()));
list.add(String.valueOf(s.getPrice()));

CSVUtils.writeLine(writer, list); // (4)
}
writer.flush();
writer.close();
}

private void loadData(){
// (1)
String csvFile = "/Users/batman/Desktop/Chocolate.csv";
String line = "";
String csvSplitBy = ",";

// (2)
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
nextId = Integer.parseInt(br.readLine()); // (3)

while ((line = br.readLine()) != null) {
// split line with comma
String[] candy = line.split(csvSplitBy);

// (4)
int id = Integer.parseInt(candy[0]);
String type = candy[1];
String brand = candy[2];
// int quantity = candy[5];
int quantity = Integer.parseInt(candy[3]);
float price = Float.parseFloat(candy[4]);

// (5)
inventory.add(new Chocolate(id, type,brand, quantity, price));
}
} catch (IOException e) {
e.printStackTrace();
}
}

public ArrayList<Chocolate> readJson() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
this.inventory = objectMapper.readValue(new File("chocolate.json"), new TypeReference<ArrayList<Chocolate>>(){});
return this.inventory;
}
public void writeJson() throws IOException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File("chocolate.json"), inventory);
}
}
Loading