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).
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();
}
}
Comments
Leave a comment