In: Computer Science
Use the code supplied to display the Customers, Products, and Prices . Also show the total and average prices package
arraylist3;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Arraylist3 {
public static void main(String[] args) {
private static ArrayList customers = new ArrayList<>()
; private static ArrayList products = new ArrayList<>();
private static ArrayList price = new ArrayList<>();
private static int x = 0;
public static void main(String[] args) {
x = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the number of customer")); input_customers(); display_sales_report(); } private static void input_customers(){ for(int y = 0;y < x; y++){ customers.add(JOptionPane.showInputDialog(null,"Please enter the customer :"+(y+1))); products.add(JOptionPane.showInputDialog(null,"Please enter the product :" + customers.get(y))); price.add(Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter the price :R" + products.get(y)))); } } private static void display_sales_report(){ } } } }
The java code after modifying the given code is as follows:
//importing the necessary components
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Arraylist3
{
public static void main(String[] args)
{
//declaring the variable arraylists
private static ArrayList customers = new ArrayList<>();
private static ArrayList products = new ArrayList<>();
private static ArrayList price = new ArrayList<>();
private static int x = 0;
//main method
public static void main(String[] args)
{
x = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the number of customers"));
input_customers();
display_sales_report();
}
// method to take inputs
private static void input_customers()
{
for(int y = 0;y < x; y++)
{
customers.add(JOptionPane.showInputDialog(null,"Please enter the customer :"+(y+1)));
products.add(JOptionPane.showInputDialog(null,"Please enter the product :" + customers.get(y)));
price.add(Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter the price :R" + products.get(y))));
}
}
//method to display the output
private static void display_sales_report()
{
float sum=0;
for(int i=0;i<price.size();i++)
{
sum=sum+price.get(i);
}
JOptionPane.showMessageDialog(null,"Total prices:"+ sum);
float average=0;
average=sum/price.size();
JOptionPane.showMessageDialog(null,"Average of prices: "+ average);
}
}