You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.7 KiB

## MapContains
You have been assigned the task of implementing a product inventory using Java maps. Each product has a unique ID as the key and the corresponding product details as the value.
### Instructions
Create a file `MapInventory.java`.
Write two functions:
CON-1881-Review-java-piscine-subjects-Fix-grammar-and-semantic-issues (#2102) * docs(java piscine): Review and update AdventureAbstract exercise * docs(java piscine): Review AdventureCharacter subject * docs(java piscine): Review AdventureInterface subject * docs(java piscine): Review AdventureMonster subject * docs(java piscine): review AdventureSorcerer subject * docs(java piscine): Review AdventureTemplar subject * docs(java piscine): Review AdventureUtils subject * docs(java piscine): Review AdventureWeapon subject * docs(java piscine): Review ComputeArray subject * docs(java piscine): Review FileSearch subject * docs(java piscine): Review FormatDate subject * docs(java piscine): Review KeepTheChange subject * docs(java piscine): Review ListEquals subject * docs(java piscine): Review MapInventory subject * docs(java piscine): Review Observer subject * docs(java piscine): Review Palindrome subject * docs(java piscine): Review RegexReplace subject * docs(java piscine): Review Singleton subject * docs(java piscine): Review StarConstructors subject * docs(java piscine): Review StarGalaxy subject * docs(java piscine): Review StarMass subject * docs(java piscine): Review StarOverride subject * docs(java piscine): Review StarPlanet subject * docs(java piscine): Review StarProperties subject * docs(java piscine): Review StarStatic subject * docs(java piscine): Review StarUtils subject * docs(java piscine): Review StreamCollect subject * docs(java piscine): Review StreamMap subject * docs(java piscine): Review StreamReduce subject * docs(java piscine): Review StringConcat subject * docs(java piscine): Review StringContains subject * docs(java piscine): Review Wedding subject * docs(java piscine): Review WeddingComplex subject * docs(java piscine): Change a part of RegexReplace subject * docs(java piscine): Update AdventureAbstract subject * docs(java piscine): Fix wrong naming parameter -> attribute * docs(java piscine): Remove empty lines * docs(java piscine): Remove ## from line
1 year ago
Write a function `getProductPrice` that takes in parameters a map of product IDs and their corresponding prices, along with a product ID, and returns the price of the specified product. If the product ID is not present in the map, the function should return `-1`.
CON-1881-Review-java-piscine-subjects-Fix-grammar-and-semantic-issues (#2102) * docs(java piscine): Review and update AdventureAbstract exercise * docs(java piscine): Review AdventureCharacter subject * docs(java piscine): Review AdventureInterface subject * docs(java piscine): Review AdventureMonster subject * docs(java piscine): review AdventureSorcerer subject * docs(java piscine): Review AdventureTemplar subject * docs(java piscine): Review AdventureUtils subject * docs(java piscine): Review AdventureWeapon subject * docs(java piscine): Review ComputeArray subject * docs(java piscine): Review FileSearch subject * docs(java piscine): Review FormatDate subject * docs(java piscine): Review KeepTheChange subject * docs(java piscine): Review ListEquals subject * docs(java piscine): Review MapInventory subject * docs(java piscine): Review Observer subject * docs(java piscine): Review Palindrome subject * docs(java piscine): Review RegexReplace subject * docs(java piscine): Review Singleton subject * docs(java piscine): Review StarConstructors subject * docs(java piscine): Review StarGalaxy subject * docs(java piscine): Review StarMass subject * docs(java piscine): Review StarOverride subject * docs(java piscine): Review StarPlanet subject * docs(java piscine): Review StarProperties subject * docs(java piscine): Review StarStatic subject * docs(java piscine): Review StarUtils subject * docs(java piscine): Review StreamCollect subject * docs(java piscine): Review StreamMap subject * docs(java piscine): Review StreamReduce subject * docs(java piscine): Review StringConcat subject * docs(java piscine): Review StringContains subject * docs(java piscine): Review Wedding subject * docs(java piscine): Review WeddingComplex subject * docs(java piscine): Change a part of RegexReplace subject * docs(java piscine): Update AdventureAbstract subject * docs(java piscine): Fix wrong naming parameter -> attribute * docs(java piscine): Remove empty lines * docs(java piscine): Remove ## from line
1 year ago
Write a function `getProductIdsByPrice` that takes a map of product IDs and their corresponding prices, along with a price as parameter, and returns a list of product IDs that have the given price. If no products are found for the specified price, the function should return an empty list.
### Expected Functions
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MapInventory {
public static int getProductPrice(Map<String, Integer> inventory, String productId) {
// your code here
}
public static List<String> getProductIdsByPrice(Map<String, Integer> inventory, int price) {
// your code here
}
}
```
### Usage
Here is a possible ExerciseRunner.java to test your functions:
```java
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExerciseRunner {
public static void main(String[] args) {
Map<String, Integer> inventory = new HashMap<>();
inventory.put("P001", 100);
inventory.put("P002", 50);
inventory.put("P003", 75);
inventory.put("P004", 50);
inventory.put("P005", 75);
System.out.println(MapInventory.getProductPrice(inventory, "P002")); // Output: 50
System.out.println(MapInventory.getProductPrice(inventory, "P004")); // Output: 50
System.out.println(MapInventory.getProductPrice(inventory, "P006")); // Output: -1
List<String> productsWithPrice50 = MapInventory.getProductIdsByPrice(inventory, 50);
System.out.println(productsWithPrice50); // Output: [P002, P004]
List<String> productsWithPrice75 = MapInventory.getProductIdsByPrice(inventory, 75);
System.out.println(productsWithPrice75); // Output: [P003, P005]
List<String> productsWithPrice80 = MapInventory.getProductIdsByPrice(inventory, 80);
System.out.println(productsWithPrice80); // Output: []
}
}
```
and its output :
```shell
$ javac *.java -d build
$ java -cp build ExerciseRunner
50
50
-1
[P002, P004]
[P003, P005]
[]
$
```
### Notions
[Map](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html)
[List](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html)