├── .idea ├── .gitignore ├── ShoppingList.iml ├── compiler.xml ├── jarRepositories.xml ├── misc.xml └── uiDesigner.xml ├── README.md ├── ShoppingList.iml ├── pom.xml └── src └── main └── java └── shoppinglist ├── EntryPoint.java ├── Report.java └── model └── Product.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /../../../../../../:\Users\gabri\Desktop\MCASupermarket\.idea/dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.idea/ShoppingList.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Make representation of a shopping list for a homemade products taken from a given API. 2 | Java console application. 3 | 4 | -------------------------------------------------------------------------------- /ShoppingList.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | MCASupermarket 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-compiler-plugin 15 | 16 | 11 17 | 11 18 | 19 | 20 | 21 | 22 | 23 | 24 | com.fasterxml.jackson.core 25 | jackson-databind 26 | 2.11.4 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/shoppinglist/EntryPoint.java: -------------------------------------------------------------------------------- 1 | package shoppinglist; 2 | 3 | import com.fasterxml.jackson.core.type.TypeReference; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import shoppinglist.model.Product; 6 | 7 | import java.io.IOException; 8 | import java.net.URI; 9 | import java.net.URISyntaxException; 10 | import java.net.http.HttpClient; 11 | import java.net.http.HttpRequest; 12 | import java.net.http.HttpResponse; 13 | import java.util.List; 14 | 15 | public class EntryPoint { 16 | 17 | public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { 18 | 19 | HttpRequest request = HttpRequest.newBuilder() 20 | .uri(new URI("")) 21 | .GET() 22 | .build(); 23 | 24 | HttpResponse response = HttpClient.newHttpClient() 25 | .send(request, HttpResponse.BodyHandlers.ofString()); 26 | 27 | ObjectMapper objectMapper = new ObjectMapper(); 28 | 29 | List products = objectMapper.readValue(response.body(), new TypeReference>(){}); 30 | 31 | String homeMadeProductsReport = Report.getReport(products); 32 | 33 | System.out.println(homeMadeProductsReport); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/shoppinglist/Report.java: -------------------------------------------------------------------------------- 1 | package shoppinglist; 2 | 3 | import shoppinglist.model.Product; 4 | 5 | import java.util.Comparator; 6 | import java.util.List; 7 | 8 | public class Report { 9 | 10 | /** 11 | * Creates a report for homemade products list received as a parameter. 12 | * 13 | * @param productList the product list. 14 | * @return {@link String} the report of home made products in string format. 15 | */ 16 | public static String getReport(List productList){ 17 | 18 | StringBuilder sb = new StringBuilder(); 19 | sb.append("Show all home made products that the customer wants to buy:"); 20 | productList.stream() 21 | .filter(product -> product 22 | .getHomeMade() 23 | .equals(true)) 24 | .sorted(Comparator.comparing(Product::getPrice)) 25 | .forEach(product -> { 26 | sb.append("\n... " + product.getName()); 27 | sb.append("\n " + splitDescription(product.getDescription())); 28 | if(product.getPrice() > 700){ 29 | sb.append("\nToo expensive, probably not worth it." ); 30 | }else{ 31 | sb.append("\nPrice: " + product.getPrice() + "eur"); 32 | } 33 | }); 34 | 35 | Double costHomeMadeProduct = 0.0; 36 | Long countHomeMadeProduct = 0L; 37 | 38 | costHomeMadeProduct = productList 39 | .stream() 40 | .filter(product -> product.getHomeMade() 41 | .equals(true)) 42 | .mapToDouble(Product::getPrice) 43 | .sum(); 44 | 45 | countHomeMadeProduct = productList 46 | .stream() 47 | .filter(product -> product.getHomeMade() 48 | .equals(true)) 49 | .count(); 50 | 51 | sb.append("\nHomeMade cost: " + costHomeMadeProduct + "eur"); 52 | sb.append("\nHomeMade count: " + countHomeMadeProduct); 53 | 54 | 55 | return sb.toString(); 56 | } 57 | 58 | /** 59 | * Splits the description if it's longer than 50 characters, else returns the description without changes. 60 | * 61 | * @param description 62 | * @return {@link String} 63 | */ 64 | public static String splitDescription(String description){ 65 | String splitDescription; 66 | 67 | if(description.length() < 50){ 68 | return description; 69 | } 70 | splitDescription = description.substring(0, 50) + " etc."; 71 | return splitDescription; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/shoppinglist/model/Product.java: -------------------------------------------------------------------------------- 1 | package shoppinglist.model; 2 | 3 | /** 4 | * Represents a product. 5 | */ 6 | public class Product { 7 | 8 | private String name; 9 | 10 | private Boolean homeMade; 11 | 12 | private Double price; 13 | 14 | private String description; 15 | 16 | public Product() { 17 | } 18 | 19 | public Product(String name, Boolean homeMade, Double price, Integer weight, String description) { 20 | this.name = name; 21 | this.homeMade = homeMade; 22 | this.price = price; 23 | this.description = description; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public Boolean getHomeMade() { 35 | return homeMade; 36 | } 37 | 38 | public void setHomeMade(Boolean homeMade) { 39 | this.homeMade = homeMade; 40 | } 41 | 42 | public Double getPrice() { 43 | return price; 44 | } 45 | 46 | public void setPrice(Double price) { 47 | this.price = price; 48 | } 49 | 50 | 51 | public String getDescription() { 52 | return description; 53 | } 54 | 55 | public void setDescription(String description) { 56 | this.description = description; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "Product{" + 62 | "name='" + name + '\'' + 63 | ", homeMade=" + homeMade + 64 | ", price=" + price + 65 | ", description='" + description + '\'' + 66 | '}'; 67 | } 68 | } 69 | --------------------------------------------------------------------------------