Why does my sign in function only work for one user in my CSV file with usernames and passwords?

huangapple go评论95阅读模式
英文:

Why does my sign in function only work for one user in my CSV file with usernames and passwords?

问题

我有一个包含我汽车经销商所有客户和管理员信息的CSV文件,其中包括用户名和密码。我有一个登录函数,用户输入他们的用户名和密码,然后我使用一个for循环来检查用户是否输入了正确的凭据。我尝试登录我的一个帐户,成功了。但是,我尝试登录其他任何帐户都不起作用。以下是登录函数。

  1. private static void signIn(CarDealership carDealership, Scanner scnr, ArrayList<Customer> customerList, Car carInUse) //method used to sign in
  2. {
  3. String userNameAttempt;
  4. String passWordAttempt;
  5. try {
  6. System.out.println("Enter username:");
  7. userNameAttempt = scnr.next();
  8. for (Customer customerU : carDealership.getCustomerList()) {
  9. if (customerU.getUserName().equals(userNameAttempt)) {
  10. System.out.println("Enter password:");
  11. passWordAttempt = scnr.next();
  12. if (customerU.getPassWord().equals(passWordAttempt)) {
  13. System.out.println("Welcome " + customerU.getFullName());
  14. if (customerU instanceof Admin) {
  15. Customer customerInUse = customerU;
  16. adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  17. }
  18. Customer customerInUse = customerU;
  19. customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  20. }
  21. throw new IllegalArgumentException("password is incorrect.");
  22. }
  23. throw new IllegalArgumentException("username does not exist.");
  24. }
  25. } catch (IllegalArgumentException ex) {
  26. System.out.println(ex.getMessage() + "Would you like to retry?(1) register yourself?(2) or go back?(3) ");
  27. int input = scnr.nextInt();
  28. switch (input) {
  29. case 1 -> {
  30. System.out.println("-----------------------------------");
  31. signIn(carDealership, scnr, customerList, carInUse);
  32. break;
  33. }
  34. case 2 -> {
  35. System.out.println("-----------------------------------");
  36. registerCustomer(carDealership, scnr, customerList, carInUse);
  37. break;
  38. }
  39. case 3 -> {
  40. System.out.println("-----------------------------------");
  41. beginningSite(carDealership, scnr, customerList, carInUse);
  42. break;
  43. }
  44. default -> {
  45. }
  46. }
  47. }
  48. }

我会说,一个客户可以登录而其他客户不能登录的原因可能是因为它是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.

  1. private static void signIn(CarDealership carDealership, Scanner scnr, ArrayList&lt;Customer&gt; customerList, Car carInUse) //method used to sign in
  2. {
  3. String userNameAttempt;
  4. String passWordAttempt;
  5. try {
  6. System.out.println(&quot;Enter username:&quot;);
  7. userNameAttempt = scnr.next();
  8. for (Customer customerU : carDealership.getCustomerList()) {
  9. if (customerU.getUserName().equals(userNameAttempt)) {
  10. System.out.println(&quot;Enter password:&quot;);
  11. passWordAttempt = scnr.next();
  12. if (customerU.getPassWord().equals(passWordAttempt)) {
  13. System.out.println(&quot;Welcome &quot; + customerU.getFullName());
  14. if (customerU instanceof Admin) {
  15. Customer customerInUse = customerU;
  16. adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  17. }
  18. Customer customerInUse = customerU;
  19. customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  20. }
  21. throw new IllegalArgumentException(&quot;password is incorrect. &quot;);
  22. }
  23. throw new IllegalArgumentException(&quot;username does not exist. &quot;);
  24. }
  25. } catch (IllegalArgumentException ex) {
  26. System.out.println(ex.getMessage() + &quot;Would you like to retry?(1) register yourself?(2) or go back?(3) &quot;);
  27. int input = scnr.nextInt();
  28. switch (input) {
  29. case 1 -&gt; {
  30. System.out.println(&quot;-----------------------------------&quot;);
  31. signIn(carDealership, scnr, customerList, carInUse);
  32. break;
  33. }
  34. case 2 -&gt; {
  35. System.out.println(&quot;-----------------------------------&quot;);
  36. registerCustomer(carDealership, scnr, customerList, carInUse);
  37. break;
  38. }
  39. case 3 -&gt; {
  40. System.out.println(&quot;-----------------------------------&quot;);
  41. beginningSite(carDealership, scnr, customerList, carInUse);
  42. break;
  43. }
  44. default -&gt; {
  45. }
  46. }
  47. }
  48. }

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循环检查每个用户然后抛出异常?

也许类似这样:

  1. // 查找客户
  2. Customer customerInUse = null;
  3. for (Customer customerU : carDealership.getCustomerList()) {
  4. if (customerU.getUserName().equals(userNameAttempt)) {
  5. customerInUse = customerU;
  6. break;
  7. }
  8. }
  9. if (customerInUse == null)
  10. // 抛出异常
  11. // 提示并验证密码
  12. System.out.println("输入密码:");
  13. passWordAttempt = scnr.next();
  14. if (!customerInUse.getPassWord().equals(passWordAttempt))
  15. // 抛出异常
  16. // 有效的登录,显示适当的菜单
  17. if (customerInUse instanceof Admin)
  18. adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  19. else
  20. 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:

  1. // Find the Customer
  2. Customer customerInUse = null;
  3. for (Customer customerU : carDealership.getCustomerList()) {
  4. if (customerU.getUserName().equals(userNameAttempt)) {
  5. customerInUse = customerU;
  6. break;
  7. }
  8. }
  9. if (customerInUse == null)
  10. // throw the Exception
  11. // prompt and validate the password
  12. System.out.println(&quot;Enter password:&quot;);
  13. passWordAttempt = scnr.next();
  14. if (!customerInUse.getPassWord().equals(passWordAttempt))
  15. // throw the Exception
  16. // valid sign in, display the appropriate menu
  17. if (customerInUse instanceof Admin)
  18. adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  19. else
  20. customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);

答案2

得分: 0

以下是代码的翻译部分:

  1. 有一些需要考虑的事情与您的代码相关
  2. 这是_customerU__for循环_
  3. ```java
  4. for (Customer customerU : carDealership.getCustomerList()) {
  5. if (customerU.getUserName().equals(userNameAttempt)) {
  6. System.out.println("请输入密码:");
  7. passWordAttempt = scnr.next();
  8. if (customerU.getPassWord().equals(passWordAttempt)) {
  9. System.out.println("欢迎 " + customerU.getFullName());
  10. if (customerU instanceof Admin) {
  11. Customer customerInUse = customerU;
  12. adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  13. }
  14. Customer customerInUse = customerU;
  15. customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  16. }
  17. throw new IllegalArgumentException("密码不正确。");
  18. }
  19. throw new IllegalArgumentException("用户名不存在。");
  20. }

您的第一个_if条件语句如下。

  1. if (customerU.getUserName().equals(userNameAttempt))

如果不满足此条件,将直接跳到IllegalArgumentException的抛出。
而且,即使if条件满足,代码块退出时也会抛出异常。

  1. throw new IllegalArgumentException("用户名不存在。");

因此,如果carDealership.getCustomerList()中的第一个Customer不匹配userNameAttempt,则会抛出异常。

我建议按照以下方式重新排列代码。

我们需要捕获是否找到了userNameAttempt。
因此,我添加了一个名为“found”的布尔值,当if条件满足时,我们将其赋值为true。

然后,将其伴随的IllegalArgumentException放在了for循环之外,条件是found == false,也可以写为!found。

此外,我反转了passWordAttempt评估的if条件。
因此,如果密码不匹配,它会抛出异常。
因此,之后的任何代码都是在密码匹配的条件下。

然后,我在条件范围内放置了一个break关键字,以退出for循环,因为后续迭代将是错误的。

最后,您需要区分customerU是否是Admin的实例。
因此,这里的if条件现在包括一个else块。

请随时提供反馈,因为我无法运行代码。

  1. boolean found = false;
  2. for (Customer customerU : carDealership.getCustomerList()) {
  3. if (customerU.getUserName().equals(userNameAttempt)) {
  4. found = true;
  5. System.out.println("请输入密码:");
  6. passWordAttempt = scnr.next();
  7. if (!customerU.getPassWord().equals(passWordAttempt))
  8. throw new IllegalArgumentException("密码不正确。");
  9. System.out.println("欢迎 " + customerU.getFullName());
  10. Customer customerInUse = customerU;
  11. if (customerU instanceof Admin) {
  12. adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  13. } else {
  14. customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  15. }
  16. break;
  17. }
  18. }
  19. if (!found) throw new IllegalArgumentException("用户名不存在。");
英文:

There are a few thing to consider with your code.

Here is the customerU for-loop.

  1. for (Customer customerU : carDealership.getCustomerList()) {
  2. if (customerU.getUserName().equals(userNameAttempt)) {
  3. System.out.println(&quot;Enter password:&quot;);
  4. passWordAttempt = scnr.next();
  5. if (customerU.getPassWord().equals(passWordAttempt)) {
  6. System.out.println(&quot;Welcome &quot; + customerU.getFullName());
  7. if (customerU instanceof Admin) {
  8. Customer customerInUse = customerU;
  9. adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  10. }
  11. Customer customerInUse = customerU;
  12. customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  13. }
  14. throw new IllegalArgumentException(&quot;password is incorrect. &quot;);
  15. }
  16. throw new IllegalArgumentException(&quot;username does not exist. &quot;);
  17. }

Your first if-conitional statement is the following.

  1. 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.

  1. throw new IllegalArgumentException(&quot;username does not exist. &quot;);

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&mdash; 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&mdash;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.

  1. boolean found = false;
  2. for (Customer customerU : carDealership.getCustomerList()) {
  3. if (customerU.getUserName().equals(userNameAttempt)) {
  4. found = true;
  5. System.out.println(&quot;Enter password:&quot;);
  6. passWordAttempt = scnr.next();
  7. if (!customerU.getPassWord().equals(passWordAttempt))
  8. throw new IllegalArgumentException(&quot;password is incorrect. &quot;);
  9. System.out.println(&quot;Welcome &quot; + customerU.getFullName());
  10. Customer customerInUse = customerU;
  11. if (customerU instanceof Admin) {
  12. adminMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  13. } else {
  14. customerMainMenu(carDealership, scnr, customerInUse, customerList, carInUse);
  15. }
  16. break;
  17. }
  18. }
  19. if (!found) throw new IllegalArgumentException(&quot;username does not exist. &quot;);

huangapple
  • 本文由 发表于 2023年6月1日 09:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定