-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.java
More file actions
62 lines (53 loc) · 2 KB
/
Demo.java
File metadata and controls
62 lines (53 loc) · 2 KB
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
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of products: ");
int numberofproducts = sc.nextInt();
String products[] = new String[numberofproducts];
int quantity[] = new int[numberofproducts];
for(int i=0; i<products.length; i++){
System.out.println("\nEnter name of product " + (i+1)+ ": ");
products[i] = sc.next();
System.out.println("\nEnter quantity of "+ products[i]+ ": ");
quantity[i] = sc.nextInt();
}
displayallproducts(products, quantity);
int totalQuantity = countTotalQuantity(quantity);
System.out.println("\nTotal quantity of all products: " + totalQuantity);
int outOfStock = countOutofStock(quantity);
System.out.println("\nNumber of out of stock products: " + outOfStock);
System.out.println("\nNumber of in stock products: " + countInStock(quantity));
}
public static void displayallproducts(String[] products, int[] quantity){
System.out.println("Products and Quantities:");
for(int i=0; i<products.length; i++){
System.out.println(products[i] + " - " + quantity[i]);
}
}
public static int countTotalQuantity(int[] quantity){
int count = 0;
for (int i=0; i<quantity.length; i++){
count += quantity[i];
}
return count;
}
public static int countOutofStock(int[] quantity){
int outOfStockCount = 0;
for (int i=0; i<quantity.length; i++){
if (quantity[i] == 0){
outOfStockCount++;
}
}
return outOfStockCount;
}
public static int countInStock(int[] quantity){
int InStockCount = 0;
for (int i=0; i<quantity.length; i++){
if (quantity[i] > 0){
InStockCount++;
}
}
return InStockCount;
}
}