I don’t understand what the mistake is. Task:
You are given two methods, requestProduct and getNumberOfProducts:
getNumberOfProducts should return the total number of requested products; requestProduct should keep track of requested products, and format the product argument in the format: No. Requested Detail.
For example:
ManufacturingController.requestProduct(“detail 1”); should return:
- Requested detail 1
and
ManufacturingController.requestProduct(“Wrench”); should return:
- Requested Wrench
After execution of these two commands,
ManufacturingController.getNumberOfProducts(); should return:
2
public class DetailManufacturing {
public static void main(String[] args) {
System.out.println(requestProduct("Wrench"));
System.out.println(getNumberOfProducts());
}
public static String requestProduct(String product) {
// write your code here
return getNumberOfProducts() + ". Requested " + product;
}
public static int getNumberOfProducts() {
// write your code here
int count=0;
count++;
return count;
}
}
Advertisement
Answer
What you need is a variable that will keep the count of how many times request has been made.
One way to do this is using static variable.
public class DetailManufacturing {
static count = 0; // This will get memory only once and shared with others while retaining it's value.
public static void main(String[] args) {
...
}
public static String requestProduct(String product) {
count++; // each time request is made, increment it's value
return getNumberOfProducts() + ". Requested " + product;
}
public static int getNumberOfProducts() {
return count;
}
}