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 = "";
usernames
: an array of strings that stores usernames for registered users.passwords
: an array of strings that stores passwords for registered users.userCount
: an integer that keeps track of how many users have been registered.loggedIn
: a boolean that indicates whether or not a user is currently logged in.product
: a string that stores the name of the product for which the inflation rate is being calculated.currentUser
: a string that stores the username of the user who is currently logged in.public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int previousYear = 0;
int currentYear = 0;
// code continues...
Scanner
object, and two integer variables previousYear
and currentYear
to store the years for which the inflation rate is being calculated. It then enters an infinite loop using while (true)
.while (true) {
if (!loggedIn) {
System.out.println("Welcome to 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();
// code continues...
scanner.nextInt()
. The different options are: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;
login
method to check if the username and password are valid. If they are, the user is logged in and the program sets loggedIn
to true and currentUser
to the current user's username.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;
createAccount
method to create a new account. If the account is created successfully, the program prints a success message. If the username already exists, the program prints an error message.case 3:
System.out.println("\n\nExiting program...\n");
scanner.close();
System.exit(0);
System.exit(0)
.} 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();
// code continues...
scanner.nextInt()
. The different options are: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;
(current price - previous price) / previous price * 100
, and prints the result to the console.InputMismatchException
thrown by scanner.nextDouble()
, prints an error message, and prompts the user to enter integers only.case 2:
System.out.println("\n\nLogging out...\n\n");
loggedIn = false;
currentUser = "";
break;
loggedIn
to false and currentUser
to an empty string.public static boolean login(String username, String password) {
for (int i = 0; i < userCount; i++) {
if (usernames[i].equals(username) && passwords[i].equals(password)) {
return true;
}
}
return false;
}
public static boolean createAccount(String username, String password) {
for (int i = 0; i < userCount; i++) {
if (usernames[i].equals(username)) {
return false;
}
}
usernames[userCount] = username;
passwords[userCount] = password;
userCount++;
return true;
}
login
), or to create a new account (createAccount
). login
checks if the username and password are stored in the usernames
and passwords
arrays. If they are, it returns true. Otherwise, it returns false.createAccount
checks if the username already exists in the usernames
array. If it does, it returns false. Otherwise, it stores the new username and password in the usernames
and passwords
arrays, and increments userCount
. It then returns true.We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?