英文:
Why does my sign in function only work for one user in my CSV file with usernames and passwords?
问题
我有一个包含我汽车经销商所有客户和管理员信息的CSV文件,其中包括用户名和密码。我有一个登录函数,用户输入他们的用户名和密码,然后我使用一个for循环来检查用户是否输入了正确的凭据。我尝试登录我的一个帐户,成功了。但是,我尝试登录其他任何帐户都不起作用。以下是登录函数。
private static void signIn(CarDealership carDealership, Scanner scnr, ArrayList<Customer> customerList, Car carInUse) //method used to sign in
{
String userNameAttempt;
String passWordAttempt;
try {
System.out.println("Enter username:");
userNameAttempt = scnr.next();
for (Customer customerU : carDealership.getCustomerList()) {
if (customerU.getUserName().equals(userNameAttempt)) {
System.out.println("Enter password:");
passWordAttempt = scnr.next();
if (customerU.getPassWord().equals(passWordAttempt)) {
System.out.println("Welcome " + customerU.getFullName());
if (customerU instanceof Admin) {
Customer customerInUse = customerU;
adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
Customer customerInUse = customerU;
customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
throw new IllegalArgumentException("password is incorrect.");
}
throw new IllegalArgumentException("username does not exist.");
}
} catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage() + "Would you like to retry?(1) register yourself?(2) or go back?(3) ");
int input = scnr.nextInt();
switch (input) {
case 1 -> {
System.out.println("-----------------------------------");
signIn(carDealership, scnr, customerList, carInUse);
break;
}
case 2 -> {
System.out.println("-----------------------------------");
registerCustomer(carDealership, scnr, customerList, carInUse);
break;
}
case 3 -> {
System.out.println("-----------------------------------");
beginningSite(carDealership, scnr, customerList, carInUse);
break;
}
default -> {
}
}
}
}
我会说,一个客户可以登录而其他客户不能登录的原因可能是因为它是CSV文件中的第一个用户。如何让它在抛出异常之前检查每个用户?
我尝试将异常抛出移到更高的位置,但这只会导致在用户输入密码之前就抛出密码错误异常。
英文:
I have a csv file that holds all the customers and admins of my car dealership and one of the pieces of information is a username and password. I have a sign in function where the user inputs their username and password and I use a for loop to see if the user is typing the correct credentials. I tried signing into one of my accounts which worked. However, I tried to sign into any other account and it doesnt work the same way. Below is the sign in function.
private static void signIn(CarDealership carDealership, Scanner scnr, ArrayList<Customer> customerList, Car carInUse) //method used to sign in
{
String userNameAttempt;
String passWordAttempt;
try {
System.out.println("Enter username:");
userNameAttempt = scnr.next();
for (Customer customerU : carDealership.getCustomerList()) {
if (customerU.getUserName().equals(userNameAttempt)) {
System.out.println("Enter password:");
passWordAttempt = scnr.next();
if (customerU.getPassWord().equals(passWordAttempt)) {
System.out.println("Welcome " + customerU.getFullName());
if (customerU instanceof Admin) {
Customer customerInUse = customerU;
adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
Customer customerInUse = customerU;
customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
throw new IllegalArgumentException("password is incorrect. ");
}
throw new IllegalArgumentException("username does not exist. ");
}
} catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage() + "Would you like to retry?(1) register yourself?(2) or go back?(3) ");
int input = scnr.nextInt();
switch (input) {
case 1 -> {
System.out.println("-----------------------------------");
signIn(carDealership, scnr, customerList, carInUse);
break;
}
case 2 -> {
System.out.println("-----------------------------------");
registerCustomer(carDealership, scnr, customerList, carInUse);
break;
}
case 3 -> {
System.out.println("-----------------------------------");
beginningSite(carDealership, scnr, customerList, carInUse);
break;
}
default -> {
}
}
}
}
I will say that maybe the reason one of the customers works while the others doesnt is because its the first user in the csv file. How do I make it do the for loop checks every user then throws the exceptions?
I did try moving the throws higher but it just made the password throw work before even asking the user for their password.
答案1
得分: 0
如何使它执行for循环检查每个用户然后抛出异常?
也许类似这样:
// 查找客户
Customer customerInUse = null;
for (Customer customerU : carDealership.getCustomerList()) {
if (customerU.getUserName().equals(userNameAttempt)) {
customerInUse = customerU;
break;
}
}
if (customerInUse == null)
// 抛出异常
// 提示并验证密码
System.out.println("输入密码:");
passWordAttempt = scnr.next();
if (!customerInUse.getPassWord().equals(passWordAttempt))
// 抛出异常
// 有效的登录,显示适当的菜单
if (customerInUse instanceof Admin)
adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
else
customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
英文:
> How do I make it do the for loop checks every user then throws the exceptions?
Maybe something like:
// Find the Customer
Customer customerInUse = null;
for (Customer customerU : carDealership.getCustomerList()) {
if (customerU.getUserName().equals(userNameAttempt)) {
customerInUse = customerU;
break;
}
}
if (customerInUse == null)
// throw the Exception
// prompt and validate the password
System.out.println("Enter password:");
passWordAttempt = scnr.next();
if (!customerInUse.getPassWord().equals(passWordAttempt))
// throw the Exception
// valid sign in, display the appropriate menu
if (customerInUse instanceof Admin)
adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
else
customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
答案2
得分: 0
以下是代码的翻译部分:
有一些需要考虑的事情与您的代码相关。
这是_customerU_的_for循环_。
```java
for (Customer customerU : carDealership.getCustomerList()) {
if (customerU.getUserName().equals(userNameAttempt)) {
System.out.println("请输入密码:");
passWordAttempt = scnr.next();
if (customerU.getPassWord().equals(passWordAttempt)) {
System.out.println("欢迎 " + customerU.getFullName());
if (customerU instanceof Admin) {
Customer customerInUse = customerU;
adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
Customer customerInUse = customerU;
customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
throw new IllegalArgumentException("密码不正确。");
}
throw new IllegalArgumentException("用户名不存在。");
}
您的第一个_if条件语句如下。
if (customerU.getUserName().equals(userNameAttempt))
如果不满足此条件,将直接跳到IllegalArgumentException的抛出。
而且,即使if条件满足,代码块退出时也会抛出异常。
throw new IllegalArgumentException("用户名不存在。");
因此,如果carDealership.getCustomerList()中的第一个Customer不匹配userNameAttempt,则会抛出异常。
我建议按照以下方式重新排列代码。
我们需要捕获是否找到了userNameAttempt。
因此,我添加了一个名为“found”的布尔值,当if条件满足时,我们将其赋值为true。
然后,将其伴随的IllegalArgumentException放在了for循环之外,条件是found == false,也可以写为!found。
此外,我反转了passWordAttempt评估的if条件。
因此,如果密码不匹配,它会抛出异常。
因此,之后的任何代码都是在密码匹配的条件下。
然后,我在条件范围内放置了一个break关键字,以退出for循环,因为后续迭代将是错误的。
最后,您需要区分customerU是否是Admin的实例。
因此,这里的if条件现在包括一个else块。
请随时提供反馈,因为我无法运行代码。
boolean found = false;
for (Customer customerU : carDealership.getCustomerList()) {
if (customerU.getUserName().equals(userNameAttempt)) {
found = true;
System.out.println("请输入密码:");
passWordAttempt = scnr.next();
if (!customerU.getPassWord().equals(passWordAttempt))
throw new IllegalArgumentException("密码不正确。");
System.out.println("欢迎 " + customerU.getFullName());
Customer customerInUse = customerU;
if (customerU instanceof Admin) {
adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
} else {
customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
break;
}
}
if (!found) throw new IllegalArgumentException("用户名不存在。");
英文:
There are a few thing to consider with your code.
Here is the customerU for-loop.
for (Customer customerU : carDealership.getCustomerList()) {
if (customerU.getUserName().equals(userNameAttempt)) {
System.out.println("Enter password:");
passWordAttempt = scnr.next();
if (customerU.getPassWord().equals(passWordAttempt)) {
System.out.println("Welcome " + customerU.getFullName());
if (customerU instanceof Admin) {
Customer customerInUse = customerU;
adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
Customer customerInUse = customerU;
customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
throw new IllegalArgumentException("password is incorrect. ");
}
throw new IllegalArgumentException("username does not exist. ");
}
Your first if-conitional statement is the following.
if (customerU.getUserName().equals(userNameAttempt))
Which, if not met, will skip directly to your throw for IllegalArgumentException.
And, at that rate, even if the if-conditional was met, upon exit of the code block, the exception would be thrown.
throw new IllegalArgumentException("username does not exist. ");
Thus, if the first Customer within carDealership.getCustomerList() does not match userNameAttempt, the exception will be thrown.
I recommend re-ordering your code, as follows.
We'll need to capture whether the userNameAttempt was found or not.
So, I've added a boolean value, named "found", that we'll assign as true when the if-conditional is met.
I then placed it's accompanying IllegalArgumentException outside of the for-loop, under the condition that found == false— which can also be written as, !found.
Furthermore, I inverted the if-conditional for the passWordAttempt evaluation.
Thus, having it throw the exception if the password is not a match.
Therefore, any code after would be under the condition that the password was a match.
I then placed a break keyword within the conditional scope, to exit the for-loop—since subsequent iterations would be erroneous.
And, finally, you need to create a distinction between whether customerU is an instance of Admin or not.
So, the if-conditional here now includes an else block.
Feel free to provide feedback, as I cannot run the code.
boolean found = false;
for (Customer customerU : carDealership.getCustomerList()) {
if (customerU.getUserName().equals(userNameAttempt)) {
found = true;
System.out.println("Enter password:");
passWordAttempt = scnr.next();
if (!customerU.getPassWord().equals(passWordAttempt))
throw new IllegalArgumentException("password is incorrect. ");
System.out.println("Welcome " + customerU.getFullName());
Customer customerInUse = customerU;
if (customerU instanceof Admin) {
adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
} else {
customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
}
break;
}
}
if (!found) throw new IllegalArgumentException("username does not exist. ");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论