• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create a login that reads strings from another class and compares them to console input?  RSS feed

 
Alisha Marsh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a login system that gets a username and password from another Class (Drivers). I'm using an if equals statement for the menu. If the user enters text that matches what's inside the other class (Drivers) then the login works. If it doesn't match in three attempts then it force logs them out. However when I enter the correct username and password its not being recognised. I don't think that the strings from the other class (Drivers) is being recognised. Any help on how to fix what I've got so far would be highly appreciated.

Here's the code that I have so far.

Here's the second class:


Any help would be greatly appreciated. Thanks
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know.But I do know that fields marked public and static are wrong. Make all your fields private and not static.
And does your code compile? It produced no end of compiler errors when I tried it.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple typo on lines 55 and 56, I'm assuming you're trying to set values there instead of get them.* Anyway you should probably have a Map or something for usernames and passwords. You don't need a separate class for just a username and password entry, even though you might need the Driver class for some reason after access has been granted.

*edit: also your set methods have parameters that aren't used.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Break out of the habit of making your main become a god method, as such, your code has a few idiosyncrasies that I am sure you are not aware of, one of which being:

Where you have "static" variables defined within the body of the class and outside of other scopes, you have class variables and not instance variables. If you could instantiate multiple instances of your class, then when you set one of those values in 1 instance, they would be changed in all instances--class variables reference the same space for all instances of the class.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Once you remove keyword static from the variables declarations as Campbell Ritchie suggested earlier - do the same in methods, remove static.
Also probably you don't need two scanner objects. Should be enough one - you can reuse it.

Few methods you declared with quite good names, I mean they are close to reveal their intension. What has happened with method main? No need to answer main method is exactly the same method as any other, well, that is not true, but it has its own purpose as every other method, and its purpose is - execute your program. Apart from that, it shouldn't be doing anything else. Remeber single responsibility principle? Method should be doing only 1 thing. You may want to go through this tutorial Main Is A Pain (<- link to click on).
Everything what is currently in main method could be divided up to few other methods. As an example one of those could be showLogonScreen().

Probably it is not a very good idea to shorten method names in order to save 5 characters. Personally for me clearer method is setDepotLocation rather than setDepotLoc, others might have different opinion. Another thing, in the same method parameteres list you got variable which is named as Manchester, why? Couldn't be location Liverpool? Think of if someone sets Liverpool as location, that means your Manchester variable will hold actually Liverpool name. That is not quite right, is it? Consider having there just depotLocation instead. Do you know about the keyword this?
That could look similar to:
Note: variable names start with lower case, in the same way as method names. Class names start with an upper case, you got that rigth, but again, class name is not that clear. Maybe DepoteSystem? If you read that tutorial you might even end up with DepotSystemLauncher

After all, I think your classes a bit chaotic. Would be easier for yourself if you were to model those classes on the piece of paper first (it is not too late for that). Think about following such structure:
After you created Driver class, you could add main method to it for isolated class testing. So you could instantiate Driver class, and try all methods to see if you got them right. After class is tested separately from the rest of your system, you can remove that main method as you won't need it anymore in it.

If you look a bit further, you may find, that setDriver method not much sense makes. Driver class shouldn't be allowed to instantiate without driver name probably. You may keep setPass method.

So I'd suggest you finish first with the Driver class and only then move on towards DepotSystem class. And if you'll think even more further, you may find that you're missing some other classes. As Route, Depot.

What the instructions says? You may have all those suggested there. If you don't mind please post them here, so we could have a look.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . . Also probably you don't need two scanner objects. Should be enough one - you can reuse it. . . .
In which case the Scanner can beneficially be final and static. Using static means there is only one object.

Maybe OP used two Scanner objects because she is calling nextLine after nextInt. As you know, that causes the second call to return the empty String. As we also know, that is poorly documented, but you can find an explanation here.

[add in edit]Beware of using one Scanner object in a multi‑threaded environment because Scanners are not thread‑safe.
 
Alisha Marsh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't know.But I do know that fields marked public and static are wrong. Make all your fields private and not static.
And does your code compile? It produced no end of compiler errors when I tried it.


If I change them to non-static I get the error "Cannot make a static reference to the non-static method getDriver() from the type Driver". Any advice?
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my previous comment.

Alisha Marsh wrote:If I change them to non-static I get the error "Cannot make a static reference to the non-static method getDriver() from the type Driver". Any advice?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, my fault. I should have told you to remove the keyword static everywhere (except the main method and as stated above).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!