What's new
Status
Not open for further replies.

sidji

Eternal Poster
Established
create a flowchart for this program
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 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();

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
Here is the flowchart for the given program:

Code:
start
|
while (true)
|
|-- if (not loggedIn)
|   |-- display welcome message
|   |-- display options
|   |-- take user input
|   |-- switch (input)
|       |-- if (1)
|           |-- ask for username and password
|           |-- try to login
|           |-- if (login success)
|               |-- set loggedIn to true
|               |-- set currentUser to username
|           |-- else
|               |-- display error message
|       |-- if (2)
|           |-- ask for new username and password
|           |-- try to create account
|           |-- if (create account success)
|               |-- display success message
|           |-- else
|               |-- display error message
|...
Here is the flowchart for the given program:

Code:
start
|
while (true)
|
|-- if (not loggedIn)
|   |-- display welcome message
|   |-- display options
|   |-- take user input
|   |-- switch (input)
|       |-- if (1)
|           |-- ask for username and password
|           |-- try to login
|           |-- if (login success)
|               |-- set loggedIn to true
|               |-- set currentUser to username
|           |-- else
|               |-- display error message
|       |-- if (2)
|           |-- ask for new username and password
|           |-- try to create account
|           |-- if (create account success)
|               |-- display success message
|           |-- else
|               |-- display error message
|       |-- if (3)
|           |-- display exit message
|           |-- close scanner
|           |-- exit program
|       |-- else
|           |-- display error message
|
|-- else
|   |-- display welcome message for logged in user
|   |-- display options
|   |-- take user input
|   |-- switch (input)
|       |-- if (1)
|           |-- take product name, previous year, current year, current price, and previous price input
|           |-- calculate inflation rate
|           |-- display inflation rate
|       |-- if (2)
|           |-- display logout message
|           |-- set loggedIn to false
|           |-- set currentUser to empty
|       |-- else
|           |-- display error message
|
end
 
Solution
explain me this code, in a very accurate and detailed way.


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 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();

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;
}
}
 
This Java program is a command line tool that helps users calculate inflation rate for a given product and two different years. It has a simple login system that allows users to create their accounts and then log in to access the inflation rate calculator. Here's a detailed breakdown of the code:

1. The code starts with the import statements that import the required Java classes. They are InputMismatchException and Scanner.

2. Next, a class InflationRateCalculator is defined, which contains the main method of the program.

3. Four static variables are declared at the class level: usernames, passwords, userCount, and loggedIn. These variables are used to store user data, such as login credentials and account creation data.

4. Another static variable product is declared to store the name of the product for which the inflation rate needs to be calculated.

5. A static variable currentUser is declared to store the username of the currently logged-in user.

6. The main method starts with a Scanner object that reads user input from the command line.

7. Two integer variables previousYear and currentYear are declared, which will store the two years for which the inflation rate needs to be calculated.

8. The program uses an infinite loop to keep prompting the user for input until the user chooses to exit.

9. Inside the loop, if the user is not logged in, the program displays a menu with three options: to login, to create an account, or to exit. The user's choice is read using the Scanner object's nextInt method.

10. If the user chooses to login, the program prompts the user to enter their username and password. If the credentials are correct, the program logs the user in and sets the loggedIn flag to true.

11. If the user chooses to create an account, the program prompts the user to enter a username and password. If the username is not already taken, the program creates a new account and sets the loggedIn flag to true.

12. If the user chooses to exit, the program exits by calling the System.exit method.

13. If the user is already logged in, the program displays a menu with two options: to calculate inflation rate or to log out.

14. If the user chooses to calculate inflation rate, the program prompts the user to enter the product name, previous year, current year, current price, and previous price. The program then calculates the inflation rate using the input and displays it to the user.

15. If the user enters a non-integer value while entering any of the required values, the program catches the InputMismatchException and prompts the user to enter an integer.

16. If the user chooses to log out, the program logs the user out by setting the loggedIn flag to false.

17. The login method takes a username and password as input and checks if there exists an account with the given username and password.

18. The createAccount method takes a username and password as input and checks if the username is already taken. If it is not taken, it creates a new account and returns true.

That's the end of the code breakdown. I hope it helps you understand the program better!
 
Status
Not open for further replies.

Similar threads

Back
Top