Koiaanbsh123
Leecher
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;
}
}
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;
}
}