What's new

Koiaanbsh123

Enthusiast
can you explain this code via comments

import java.util.InputMismatchException;
import java.util.Scanner;

public class InflationRateCalculator {
static String[] usernames = new String[10];
static String[] passwords = new String[10];
static int userCount = 0;
static boolean loggedIn = false;
static String product = "";
static String currentUser = "";

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int previousYear = 0;
int currentYear = 0;

while (true) {
if (!loggedIn) {
System.out.println("Welcome Inflation Rate for Goods and Services Calculator\n");
System.out.println("Please choose an option:\n");
System.out.println("1. Log in");
System.out.println("2. Create account");
System.out.println("3. Exit\n\n");
int choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.println("\nEnter username:");
String username = scanner.next();
System.out.println("\nEnter password:");
String password = scanner.next();
if (login(username, password)) {
System.out.println("\nLogin successful.");
loggedIn = true;
currentUser = username;
} else {
System.out.println("Invalid username or password. Please try again.\n");
}
break;
case 2:
System.out.println("\nEnter username:");
String newUsername = scanner.next();
System.out.println("\nEnter password:");
String newPassword = scanner.next();
if (createAccount(newUsername, newPassword)) {
System.out.println("\n\nAccount created successfully.\n");
} else {
System.out.println("\nUsername already exists. Please choose a different username.\n");
}
break;
case 3:
System.out.println("\n\nExiting program...\n");
scanner.close();
System.exit(0);
default:
System.out.println("\nInvalid choice. Please try again.\n");
break;
}
} else {
System.out.println("\nWelcome, " + currentUser + "!");
System.out.println("\nPlease choose an option:");
System.out.println("\n1. Calculate inflation rate");
System.out.println("\n2. Logout");
int choice = scanner.nextInt();

switch (choice) {
case 1:
boolean validInput = false;
while (!validInput) {
try {

System.out.println("\nEnter product name :");
product = scanner.next();

System.out.println("\nEnter previous year:");
previousYear = scanner.nextInt();
System.out.println("\nEnter current year:");
currentYear = scanner.nextInt();
System.out.println("\nEnter current price:");
double cpi = scanner.nextDouble();
System.out.println("\nEnter previous price:");
double previousCpi = scanner.nextDouble();
double inflationRate = (int) Math.round(cpi - previousCpi) / previousCpi * 100;
System.out.println("\nInflation rate for " + previousYear + "-" + currentYear + " (" + product + ") = " + inflationRate + "%");

validInput = true;
} catch (InputMismatchException e) {
System.out.println("\nYou entered a non-integer. Please enter integers only.");
scanner.next();
}
}
break;
case 2:
System.out.println("\n\nLogging out...\n\n");
loggedIn = false;
currentUser = "";
break;
default:
System.out.println("\nInvalid choice. Please try again.");
break;


}
}
}
}

public static boolean login(String username, String password) {
for (int i = 0; i < userCount; i++) {
if (usernames.equals(username) && passwords.equals(password)) {
return true;
}
}
return false;
}

public static boolean createAccount(String username, String password) {
for (int i = 0; i < userCount; i++) {
if (usernames.equals(username)) {
return false;
}
}
usernames[userCount] = username;
passwords[userCount] = password;
userCount++;
return true;
}
}
 
Solution
This code is a simple Inflation Rate Calculator program that allows users to calculate the inflation rate of a product between two years. Here is a breakdown of the code with comments:

Code:
import java.util.InputMismatchException;
import java.util.Scanner;

public class InflationRateCalculator {
    // Arrays to store usernames and passwords of users
    static String[] usernames = new String[10];
    static String[] passwords = new String[10];
    // Counter to keep track of number of users
    static int userCount = 0;
    // boolean to check if user has logged in
    static boolean loggedIn = false;
    // String to store the product name
    static String product = "";
    // String to store the current user
    static String...
This code is a simple Inflation Rate Calculator program that allows users to calculate the inflation rate of a product between two years. Here is a breakdown of the code with comments:

Code:
import java.util.InputMismatchException;
import java.util.Scanner;

public class InflationRateCalculator {
    // Arrays to store usernames and passwords of users
    static String[] usernames = new String[10];
    static String[] passwords = new String[10];
    // Counter to keep track of number of users
    static int userCount = 0;
    // boolean to check if user has logged in
    static boolean loggedIn = false;
    // String to store the product name
    static String product = "";
    // String to store the current user
    static String currentUser = "";
    
    public static void main(String[] args) {
        // Create a new scanner object to read input from user
        Scanner scanner = new Scanner(System.in);
        // Variables to store previous and current year
        int previousYear = 0;
        int currentYear = 0;
        
        while (true) {
            if (!loggedIn) {
                // User is not logged in, display login menu
                System.out.println("Welcome Inflation Rate for Goods and Services Calculator\n");
                System.out.println("Please choose an option:\n");
                System.out.println("1. Log in");
                System.out.println("2. Create account");
                System.out.println("3. Exit\n\n");
                int choice = scanner.nextInt();
                
                switch (choice) {
                    case 1:
                        // User has chosen to log in, prompt for username and password
                        System.out.println("\nEnter username:");
                        String username = scanner.next();
                        System.out.println("\nEnter password:");
                        String password = scanner.next();
                        // Check if username and password are correct
                        if (login(username, password)) {
                            System.out.println("\nLogin successful.");
                            loggedIn = true;
                            currentUser = username;
                        } else {
                            System.out.println("Invalid username or password. Please try again.\n");
                        }
                        break;
                    case 2:
                        // User has chosen to create an account, prompt for username and password
                        System.out.println("\nEnter username:");
                        String newUsername = scanner.next();
                        System.out.println("\nEnter password:");
                        String newPassword = scanner.next();
                        // Check if username already exists
                        if (createAccount(newUsername, newPassword)) {
                            System.out.println("\n\nAccount created successfully.\n");
                        } else {
                            System.out.println("\nUsername already exists. Please choose a different username.\n");
                        }
                        break;
                    case 3:
                        // User has chosen to exit, close scanner and exit program
                        System.out.println("\n\nExiting program...\n");
                        scanner.close();
                        System.exit(0);
                    default:
                        // User has entered an invalid choice
                        System.out.println("\nInvalid choice. Please try again.\n");
                        break;
                }
            } else {
                // User is logged in, display main menu
                System.out.println("\nWelcome, " + currentUser + "!");
                System.out.println("\nPlease choose an option:");
                System.out.println("\n1. Calculate inflation rate");
                System.out.println("\n2. Logout");
                int choice = scanner.nextInt();
                
                switch (choice) {
                    case 1:
                        // User has chosen to calculate inflation rate
                        boolean validInput = false;
                        while (!validInput) {
                            try {
                                // Prompt user for product name, previous year, current year, current price and previous price
                                System.out.println("\nEnter product name :");
                                product = scanner.next();
                                                                
                                System.out.println("\nEnter previous year:");
                                previousYear = scanner.nextInt();
                                System.out.println("\nEnter current year:");
                                currentYear = scanner.nextInt();
                                System.out.println("\nEnter current price:");
                                double cpi = scanner.nextDouble();
                                System.out.println("\nEnter previous price:");
                                double previousCpi = scanner.nextDouble();
                                // Calculate inflation rate and display result
                                double inflationRate = (int) Math.round(cpi - previousCpi) / previousCpi * 100;
                                System.out.println("\nInflation rate for " + previousYear + "-" + currentYear + " (" + product + ") = " + inflationRate + "%");
                                validInput = true;
                            } catch (InputMismatchException e) {
                                // User has entered a non-integer for year or price
                                System.out.println("\nYou entered a non-integer. Please enter integers only.");
                                scanner.next();
                            }
                        }
                        break;
                    case 2:
                        // User has chosen to log out, set loggedIn to false and currentUser to empty string
                        System.out.println("\n\nLogging out...\n\n");
                        loggedIn = false;
                        currentUser = "";
                        break;
                    default:
                        // User has entered an invalid choice
                        System.out.println("\nInvalid choice. Please try again.");
                        break;   
                }
            }
        }
    }
    
    public static boolean login(String username, String password) {
        // Loop through the usernames and passwords arrays to find a match
        for (int i = 0; i < userCount; i++) {
            if (usernames[i].equals(username) && passwords[i].equals(password)) {
                return true;
            }
        }
        // If no match is found, return false
        return false;
    }
    
    public static boolean createAccount(String username, String password) {
        // Loop through the usernames array to check if username already exists
        for (int i = 0; i < userCount; i++) {
            if (usernames[i].equals(username)) {
                return false;
            }
        }
        // If username doesn't exist, add it to the array
        usernames[userCount] = username;
        passwords[userCount] = password;
        userCount++;
        return true;
    }
}

The code uses a simple console-based interface to get input from the user and perform actions based on the user's choices. The program keeps track of user accounts using two arrays, one for usernames and one for passwords. Users can log in using their username and password, or create a new account if they don't have one. Once logged in, users can calculate the inflation rate of a product between two years. The program uses try-catch blocks to handle exceptions when the user enters non-integer values for year or price.
 
Solution

Similar threads

Back
Top