Please write code simply (if else,for, while, do-while and array) according to t
ID: 3827420 • Letter: P
Question
Please write code simply (if else,for, while, do-while and array) according to the following information.
Write a Java program that gets a list of inventory items and their counts. The user first enters the number of items she wants to enter. Then, she enters names of the items. Next, the user enters a list of counts of these items in the inventory. The order of item counts must be the same as the order of item names. For instance, if the user enters box, screwdriver, and wrench as the list of item names and 12, 0, and 4 as the list of item counts (see sample runs), it represents that in the inventory there are 12 box items, 0 screwdriver items, and 4 wrench items. Later, the program asks the user which item to search for in the inventory and finally prints the count of that item in the inventory.
Sample Runs. Enter the number of items: Enter names of these 3 items: box screwdriver wrench Enter counts of these 3 items: Which item do you want to search for wrench There are 4 wrench items in the inventory Enter the number of items: Enter names of these 5 items: milk cheese sucuk Cream chocolate Enter counts of these 5 items: 12 Which item do you want to search for Cream There are 12 cream items in the inventoryExplanation / Answer
import java.io.*;
import java.util.Scanner;
import java.lang.*;
class item {
String name;
int count;
}
public class Inventory
{
public static void main(String[] args) {
int n,i;
String name;
item ptr = null;
ArrayList<item> list = new ArrayList<item>();
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of items:");
n = scan.nextInt();
System.out.println("Enter names of these " + n + " items");
for (i=0; i<n; i++){
ptr = new item();
ptr.name = scan.next();
list.add(ptr);
}
System.out.println("Enter counts of these " + n + " items");
for (i=0; i<n; i++){
ptr = list.get(i);
ptr.count = scan.nextInt();
}
System.out.println("Which item do you want to search for:");
name = scan.next();
for (i=0; i<n; i++){
ptr = list.get(i);
if (ptr.name == name){
System.out.println("There are " + ptr.count + " " + name +" items in the inventory");
}
}
}
}