Supposing you are operating a warehouse that receives and issues out items daily, and at the end of each day, you take stock of the transactions. Answer the following based on your knowledge in Java Programming.
a) 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).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean isEntering = true;
int totalNumberItemsReceived = 0;
int totalNumberItemsIssuedOut = 0;
while (isEntering) {
System.out.print("Enter positive integer representing the number of items brought in each time (0 - exit): ");
int numberItemsBrought = keyboard.nextInt();
if (numberItemsBrought > 0) {
totalNumberItemsReceived+=numberItemsBrought;
System.out.print("Enter negative integer representing the number of items issued out in each time (0 - exit): ");
int numberItemsIssuedOut = keyboard.nextInt();
if (numberItemsIssuedOut < 0) {
totalNumberItemsIssuedOut+=(-1)*numberItemsIssuedOut;
} else {
isEntering = false;
}
} else {
isEntering = false;
}
}
float totalCostItemsReceived = totalNumberItemsReceived*0.5f;
float totalCostItemsIssuedOut = totalNumberItemsIssuedOut*0.5f;
System.out.printf("The total number of items received: %d\n", totalNumberItemsReceived);
System.out.printf("The total cost of items received: %.2f\n", totalCostItemsReceived);
System.out.printf("The total number of items issued out: %d\n", totalNumberItemsIssuedOut);
System.out.printf("The total cost of items issued out: %.2f\n", totalCostItemsIssuedOut);
keyboard.close();
}
}
Comments
Leave a comment