-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitch_Loop_Arr_Meth.java
More file actions
69 lines (60 loc) · 2.25 KB
/
Switch_Loop_Arr_Meth.java
File metadata and controls
69 lines (60 loc) · 2.25 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
63
64
65
66
67
68
69
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Switch_Loop_Arr_Meth{
public static void main(String[]args) throws IOException{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of products: ");
int numproducts = sc.nextInt();
String products[] = new String[numproducts];
int quantity[] = new int[numproducts];
for(int i=0; i<products.length; i++){
System.out.println("Enter name of product " + (i+1)+ ": ");
products[i] = sc.next();
System.out.println("Enter quantity of "+ products[i]+ ": ");
quantity[i] = sc.nextInt();
}
displayAll(products, quantity);
System.out.println("\nTotal quantity of all products: " + getTotal(quantity));
System.out.println("\nNumber of products out of stock: " + outstock(quantity));
System.out.println("\nNumber of products in stock: " + instock(quantity));
FileWriter fw = new FileWriter("Inventory.txt", true);
fw.write("\nTotal quantity of all products: " + getTotal(quantity));
fw.write("\nNumber of products out of stock: " + outstock(quantity));
fw.write("\nNumber of products in stock: " + instock(quantity));
fw.close();
}
public static void displayAll(String products[], int quantity[]) throws IOException{
FileWriter fw = new FileWriter("Inventory.txt", true);
fw.write("\nProducts and Quantities:\n" );
for(int i=0; i<products.length; i++){
fw.write(products[i] + " - " + quantity[i] + "\n");
}
fw.close();
}
public static int getTotal(int quantity[]){
int count = 0;
for(int i=0; i<quantity.length; i++){
count+=quantity[i];
}
return count;
}
public static int outstock(int quantity[]){
int count =0;
for(int i=0; i<quantity.length; i++){
if(quantity[i]==0){
count++;
}
}
return count;
}
public static int instock(int quantity[]){
int count =0;
for(int i=0; i<quantity.length; i++){
if(quantity[i]>0){
count++;
}
}
return count;
}
}