A Java Program for Store Inventory

How can a Java program help in performing end-of-the-year store inventory for printers?

To solve the problem of end-of-the-year store inventory for printers, a Java program can be written. The program will ask the user to enter the type (D for Deskjet, L for Laser) and price for 20 printers. It will then display the number of Deskjet printers, the number of Laser printers, and the number of other printers. The program will iterate through the user input using loops and conditionals to calculate the counts for each type of printer.

Implementing a Java Program for Store Inventory

Step 1: Create variables to store the counts of Deskjet printers, Laser printers, and other printers. Initialize them to 0.

Step 2: Use a loop to iterate 20 times to get the type and price of each printer from the user.

Step 3: Inside the loop, prompt the user to enter the type of printer (D or L) and read it from the user using the Scanner class.

Step 4: Based on the entered type, increment the count of Deskjet printers if the type is 'D', increment the count of Laser printers if the type is 'L', and increment the count of other printers otherwise.

Step 5: After the loop ends, display the counts of Deskjet printers, Laser printers, and other printers on the screen.

Step 6: Run the program and test it by entering the type and price for each printer.

Here's an example code snippet:

import java.util.Scanner;
public class PrinterInventory {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int deskjetCount = 0;
int laserCount = 0;
int otherCount = 0;
for (int i = 1; i <= 20; i++) {
System.out.println("Enter the type (D for Deskjet, L for Laser) and price for printer " + i + ":");
String type = scanner.nextLine().toUpperCase();
int price = scanner.nextInt();
scanner.nextLine(); // Consume the newline character after reading the price
if (type.equals("D")) {
deskjetCount++;
} else if (type.equals("L")) {
laserCount++;
} else {
otherCount++;
}
}
System.out.println("Number of Deskjet printers: " + deskjetCount);
System.out.println("Number of Laser printers: " + laserCount);
System.out.println("Number of other printers: " + otherCount);
scanner.close();
}
}

← Type of construction with expansion joint Velocity unleashing the power of flow dynamics →