import [Link].
Scanner;
public class MainImproved {// Main class to run the inventory management application
// Method to clear the console screen (not implemented in this example)
public static void clearScreen() {
try {
String os = [Link]("[Link]").toLowerCase();// Get the name of
the operating system
if ([Link]("windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();// Clear the
console screen for Windows
} else {
[Link]("\033[H\033[2J");// Clear the console screen for
Unix/Linux/Mac
[Link]();
} catch (Exception e) {
for (int i = 0; i < 50; i++) {
[Link]();// Print new lines to simulate clearing the console screen
if the above method fails
// Method to print text centered in the console
public static void printCentered(String text) {
int width = 80;// Set the width for centering the text
int padding = (width - [Link]()) / 2;// Calculate the padding needed to center
the text
if (padding < 0) padding = 0;// Ensure padding is not negative
String spaces = " ".repeat(padding);// Create a string of spaces for padding
[Link](spaces + text);// Print the centered text with padding
// Method to read an integer input from the user with error handling
public static int readInt(Scanner sc, String msg) {
while (true) {
try {
[Link](ConsoleColors.ANSI_YELLOW + msg +
ConsoleColors.ANSI_RESET);// Print the message in yellow color
return [Link]([Link]());// Read the user input and parse it as an
integer
} catch (Exception e) {
[Link](ConsoleColors.ANSI_RED + "Invalid input. Please enter a
valid integer." + ConsoleColors.ANSI_RESET);// Print an error message for invalid input
// Method to read a double input from the user with error handling
public static double readDouble(Scanner sc, String msg) {
while (true) {
try {
[Link](ConsoleColors.ANSI_YELLOW + msg +
ConsoleColors.ANSI_RESET);// Print the message in yellow color
return [Link]([Link]());// Read the user input and parse it
as a double
} catch (Exception e) {
[Link](ConsoleColors.ANSI_RED + "Invalid input. Please enter a
valid number." + ConsoleColors.ANSI_RESET);// Print an error message for invalid input
// Method to read a string input from the user
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);// Create a Scanner object to read user input
InventoryImproved inventory = new InventoryImproved();// Create an
InventoryImproved object to manage the products
[Link]("[Link]");// Load existing products from a CSV file
into the inventory
int option;//
do {
[Link](ConsoleColors.ANSI_BOLD_RED +
ConsoleColors.ANSI_BACKGROUND_WHITE +
"______________________________________________________________________
_____________________");// Print a separator line for better readability
[Link](" J Krlos Supermarket Inventory Management
System ");// Print the title of the inventory management system
[Link](" WELCOME TO JK´RLOS APP FOR INVENTORY
MANAGEMENT ");// Print a welcome message for the user
[Link](" This application allows you to manage the inventory of a
supermarket by adding products ");// Print a brief description of the application
[Link](" saving them to a file, and loading them from a
file. ");// Print a brief description of the application
[Link]("_________________________________________________
__________________________________________" + ConsoleColors.ANSI_RESET);//
Print a separator line for better readability
[Link](ConsoleColors.ANSI_BOLD_CYAN + "\n1. Add a new
product");// Print the option to add a new product
[Link]("2. Search for a product by name");// Print the option to
search for a product by name
[Link]("3. Search for a product by ID");// Print the option to search
for a product by ID
[Link]("4. Save inventory to file");// Print the option to save the
inventory to a file
[Link]("5. Load inventory from file");// Print the option to load the
inventory from a file
[Link]("6. Exit" + ConsoleColors.ANSI_RESET);// Print the option to
exit the application
[Link](ConsoleColors.ANSI_BOLD_CYAN + "\nPlease select an
option:" + ConsoleColors.ANSI_RESET);// Prompt the user to select an option
option = [Link]();// Read the user's selected option
[Link]();// Consume the newline character left by nextInt()
switch (option) {
case 1:
[Link](ConsoleColors.ANSI_BOLD_YELLOW + "\nPlease select
the type: 1 >> Food, | 2 >> Utensils | 3 >> Cleaning |\n" +
ConsoleColors.ANSI_RESET);// Prompt the user to select the type of product they want
to add
int type = [Link]();// Read the user's selected product type
[Link]();// Consume the newline character left by nextInt()
[Link](ConsoleColors.ANSI_YELLOW + "Enter product ID:" +
ConsoleColors.ANSI_RESET);// Prompt the user to enter the product ID
int id = [Link]();// Read the product ID
[Link]();// Consume the newline character left by nextInt()
[Link](ConsoleColors.ANSI_YELLOW + "Enter product name:" +
ConsoleColors.ANSI_RESET);// Prompt the user to enter the product name
String name = [Link]();// Read the product name
[Link](ConsoleColors.ANSI_YELLOW + "Enter product price:" +
ConsoleColors.ANSI_RESET);// Prompt the user to enter the product price
double price = [Link]();// Read the product price
[Link](ConsoleColors.ANSI_YELLOW + "Enter product quantity:"
+ ConsoleColors.ANSI_RESET);// Prompt the user to enter the product quantity
int quantity = [Link]();// Read the product quantity
[Link]();// Consume the newline character left by nextInt()
if(type == 1) {//
[Link](ConsoleColors.ANSI_BLUE + "Enter expiration date
(YYYY-MM-DD): " + ConsoleColors.ANSI_RESET);// Prompt the user to enter the
expiration date for a food product
String date = [Link]();// Read the expiration date
[Link](new FoodImproved(id, name, price, quantity,
date));// Create a new Food product and add it to the inventory
} else if(type == 2) {
[Link](ConsoleColors.ANSI_BLUE + "Enter warranty period (in
months): " + ConsoleColors.ANSI_RESET);// Prompt the user to enter the warranty
period for a utensil product
int warranty = [Link]();// Read the warranty period
[Link](new UtensilImproved(id, name, price, quantity,
warranty));// Create a new Utensil product and add it to the inventory
} else if(type == 3) {
[Link](ConsoleColors.ANSI_BLUE + "Enter danger level (Low,
Medium, High): " + ConsoleColors.ANSI_RESET);// Prompt the user to enter the danger
level for a cleaning product
String dangerLevel = [Link]();// Read the danger level
[Link](new CleaningImproved(id, name, price, quantity,
dangerLevel));// Create a new Cleaning product and add it to the inventory
} else {
[Link](ConsoleColors.ANSI_BRIGHT_RED_BOLD + "\nInvalid
product type selected. Please try again.\n" + ConsoleColors.ANSI_RESET);// Print an
error message if the user selects an invalid product type
break;
case 2:
[Link](ConsoleColors.ANSI_BLUE + "Enter product name to
search: " + ConsoleColors.ANSI_RESET);// Prompt the user to enter the product
String searchName = [Link]();// Read the product name to search for
[Link](searchName);// Search for the product by name in
the inventory
break;
case 3:
[Link](ConsoleColors.ANSI_BLUE + "Enter product ID to search:
" + ConsoleColors.ANSI_RESET);// Prompt the user to enter the product
int searchId = [Link]();// Read the product ID to search for
[Link](searchId);// Search for the product by ID in the
inventory
break;
case 4:
[Link](ConsoleColors.ANSI_BLUE + "[Link]" +
ConsoleColors.ANSI_RESET);// Save the current inventory to a CSV file
break;
} while (option != 6);// Continue to show the menu until the user selects the option
to exit
[Link]();// Close the Scanner object to free up resources
[Link](ConsoleColors.ANSI_BRIGHT_YELLOW_BOLD + "\nThank you
for using J Krlos Supermarket Inventory Management System. Goodbye!\n" +
ConsoleColors.ANSI_RESET);// Print a goodbye message when the user exits the
application