18.5k views
0 votes
Counter Controlled Loops Create a Java program that asks for a number of orders to process and use a loop to handle the number the user gives. For each order the program needs to ask the user for a quantity and price. Calculate the total for the order and show this to the user as well as adding it to a grand total. At the end of the program show the grand total to the user.

User Myoungjin
by
3.4k points

2 Answers

1 vote

Answer:

//import the scanner class to allow for user's input

import java.util.Scanner;

//Create a class to house the application

public class OrderProcessor {

//Create the main method where execution starts from

public static void main(String[] args) {

//Create an object of the Scanner class to read user's inputs

Scanner input = new Scanner(System.in);

//Prompt the user to enter number of orders

System.out.println("Enter the number of orders to process");

//Read user input and store in a variable

int noOfOrders = input.nextInt();

//Declare and initialize the variables to hold the

//quantity, price, total and grand total of the order

int quantity = 0;

double price = 0.0;

double total = 0.0;

double grandtotal = 0.0;

//Write a for loop that cycles as many times as the number of orders

for (int i=1; i<=noOfOrders; i++){

//At each of the loop, prompt the user to enter the quantity of each

// order

System.out.println("Enter the quantity of order " + i);

//Read the user input and store in the quantity variable

quantity = input.nextInt();

//Also, prompt the user to enter the price of a quantity of each order

System.out.println("Enter the price of a quantity of order " + i);

//Read the user input and store in the price variable

price = input.nextDouble();

//Calculate the total of each order by multiplying the quantity and

// price

total = quantity * price;

//Print out the total

System.out.println("Total price for order " + i + " is " + total);

//Add the total to the grandtotal

grandtotal += total;

} // End of for loop

//At the end of the loop, print out the grandtotal

System.out.println("The grand total for the order is " + grandtotal);

} //End of main method

} //End of class definition

===========================================================

Sample Output:

>> Enter the number of orders to process

3

>> Enter the quantity of order 1

7

>> Enter the price of a quantity of order 1

30

>> Total price for order 1 is 210.0

>> Enter the quantity of order 2

5

>> Enter the price of a quantity of order 2

30

>> Total price for order 2 is 150.0

>> Enter the quantity of order 3

6

>> Enter the price of a quantity of order 3

30

>> Total price for order 3 is 180.0

>> The grand total for the order is 540.0

===========================================================

Step-by-step explanation:

The program contains comments explaining every line of the code. Please go through the comments.

The actual lines of code have been written in bold face to enhance readability and distinguish them from comments.

A sample output of a run of the program has been given too.

User EDi
by
3.5k points
2 votes

Answer:

// Scanner class is imported to allow program

// receive input

import java.util.Scanner;

// Solution class is defined

public class Solution {

// main method that signify beginning of program execution

public static void main(String args[]) {

// Scanner object scan is created

// it receive input via keyboard

Scanner scan = new Scanner(System.in);

// Prompt is display asking the user to enter number

System.out.println("Enter your number of order: ");

// the user input is stored at numberOfOrder

int numberOfOrder = scan.nextInt();

// the grandTotal is initialized to 0

double grandTotal = 0;

// for-loop that asked the user for quantity and price

// based on the number of order

for(int i = 0; i < numberOfOrder; i++){

// A prompt is display to the user to enter quantity

System.out.println("Enter the quantity you want: ");

// user input is assigned to quantity

int quantity = scan.nextInt();

// Prompt is display to the user to enter price

System.out.println("Enter the price: ");

// user input is assigned to price

double price = scan.nextDouble();

// total is calculated

double total = quantity * price;

// total for each quantity/price is displayed

System.out.println("Total = " + total);

// total is added to the grandTotal

grandTotal += total;

}

// The grandTotal is displayed to the user.

System.out.println("The grand total for your order is: " + grandTotal);

}

}

Step-by-step explanation:

The program is well commented. A sample image of the output when the program is executed is attached.

Counter Controlled Loops Create a Java program that asks for a number of orders to-example-1
User Daniel Gelling
by
3.8k points