Write a Java program to accept a given number of item names
Write a Java program to accept a given number of item names and prices and then output them in the reverse order in which they were input. In addition, output the average price if one of the items is named Peas (not case sensitive) otherwise output: \"no average output\". (The first user input will be the number of items to process.)
Add a static method to the program that outputs \"Welcome to St. Joseph\'s College\" before the items and prices are output. The method main should invoke this method.
Solution
Please follow the code and comments for description :
CODE :
import java.util.Scanner;
public class userData { // class to run the code
public static void message() { // static message to print the data to console
System.out.println(\"\ Welcome to St. Joseph\'s College\"); // message to print
}
public static void main(String[] args) { // driver method
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user
boolean findAvg = false; // local varaibles
System.out.println(\"Please Enter the Total Number of Items to be Processed : \"); // prompt for the user to enter the data
int count = sc.nextInt(); // get the count
String itemName[] = new String[count]; // initialise the array
double prices[] = new double[count]; // initialise the array
for (int i = 0; i < count; i++) { // iterate over the count
System.out.println(\"Please Enter the Item Name \" + (i + 1) + \" : \"); // message to the user
sc.nextLine();
String name = sc.nextLine(); // get the data
itemName[i] = name; // save to array
System.out.println(\"Please Enter the Price of the Item \" + (i + 1) + \" : \"); // message to user
prices[i] = sc.nextDouble(); // get the data
}
message(); // print the message by calling the function
for (int m = count - 1; m >= 0; m--) { // iterate to check for the condition
if (itemName[m].equalsIgnoreCase(\"peas\")) {
findAvg = true; // assign true if found
break; // break the loop
}
}
for (int m = count - 1; m >= 0; m--) { // iterate to print the data
System.out.print(itemName[m]); // print the item name
System.out.print(\" \" + prices[m]); // print the price
System.out.println(\"\"); // new line character
}
if (findAvg == true) { // check for the flag value
double sum = 0, avg = 0; // local variables
for (double j : prices) { // iterate to read the data
sum = sum + j; // add to sum
}
avg = sum / count; // get the average
System.out.println(\"The Average of the Prices is : \" + avg); // print to console
}
}
}
OUTPUT :
Case 1 :
Please Enter the Total Number of Items to be Processed :
5
Please Enter the Item Name 1 :
Orange
Please Enter the Price of the Item 1 :
45.3
Please Enter the Item Name 2 :
cloves
Please Enter the Price of the Item 2 :
64.1
Please Enter the Item Name 3 :
peas
Please Enter the Price of the Item 3 :
24.6
Please Enter the Item Name 4 :
apples
Please Enter the Price of the Item 4 :
46.7
Please Enter the Item Name 5 :
cucumber
Please Enter the Price of the Item 5 :
12.5
Welcome to St. Joseph\'s College
cucumber 12.5
apples 46.7
peas 24.6
cloves 64.1
Orange 45.3
The Average of the Prices is : 38.64
Case 2 :
Please Enter the Total Number of Items to be Processed :
2
Please Enter the Item Name 1 :
grapes
Please Enter the Price of the Item 1 :
12.5
Please Enter the Item Name 2 :
beetroot
Please Enter the Price of the Item 2 :
10.6
Welcome to St. Joseph\'s College
beetroot 10.6
grapes 12.5
Hope this is helpful.

