Answer to Question #245456 in Java | JSP | JSF for Jeff

Question #245456

v>

Write a Java programme named transactions.java that takes input from you in the

form of: positive integers representing the number of items brought in each time,

and negative integers representing the number of items issued out each time.

The programme will then calculate and print out the total number of items

received and issued out, as well as their corresponding total costs. Your

programme ends when the input is zero (0).


1
Expert's answer
2021-10-01T16:52:31-0400
import java.util.Scanner;


public class Main {


	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);


		boolean flag = true;
		int itemsReceived = 0;
		int itemsIssuedOut = 0;
		
		while (flag) {
			System.out.print("Enter the number of items brought in each time (0 - to end): ");
			int numberItemsBrought = input.nextInt();
			if (numberItemsBrought > 0) {
				itemsReceived+=numberItemsBrought;
				System.out.print("Enter the number of items issued out in each time (0 - to end): ");
				int numberItemsIssuedOut = input.nextInt();
				if (numberItemsIssuedOut < 0) {
					itemsIssuedOut+=(-1)*numberItemsIssuedOut;
				} else {
					flag = false;
				}
			} else {
				flag = false;
			}
		}
		float costItemsReceived = itemsReceived*0.5f;
		float costItemsIssuedOut = itemsIssuedOut*0.5f;
		System.out.printf("The total number of items received: %d\n", itemsReceived);
		System.out.printf("The total cost of items received: %.2f\n", costItemsReceived);
		System.out.printf("The total number of items issued out: %d\n", itemsIssuedOut);
		System.out.printf("The total cost of items issued out: %.2f\n", costItemsIssuedOut);


		input.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS