473,473 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple Sign in Application

3 New Member
This is a basic sign in application I created. Currently I'm studying Java Programming and doing this to practice for a future assignment. Please comment whether if my code is okay or if it needs improvement; and if it needs to be improved how do I do it.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class LogIn {
  4.     Scanner key = new Scanner (System.in);
  5.     private String username = "Kate", password = "abc789";
  6.     private String user, pass;
  7.  
  8.     public void SignIn(){
  9.         System.out.println("Enter your username");
  10.         user = key.next();
  11.         System.out.println("Enter your password");
  12.         pass = key.next();
  13.  
  14.     if ((user .equals(username)) && (pass .equals(password)))
  15.         System.out.println("You've successfuly signed in");
  16.     else
  17.         System.out.println("Sign in details incorrect");
  18.     }
  19. }

--MAIN CLASS--
Expand|Select|Wrap|Line Numbers
  1. public class BankApp {
  2.  
  3.     public static void main(String[] args) {
  4.         LogIn log = new LogIn();
  5.         log.SignIn();
  6.     }
  7. }
Mar 11 '17 #1

✓ answered by chaarmann

1.) As of java coding standard, use upper case only for class names, but lower case for method names.
So change your method name "SignIn" to "signIn".
2.) Use complete english sentences, ending with dot or whatever. So change "Sign in details incorrect" to "Sign in details are incorrect."
(By the way, don't you want the user to retry in this case?)
Also all other sentences are missing the end. "Enter your password" should be "Enter your password:". Look for spelling mistakes. Write "successfully" instead of "successfuly".
3.) Use a code formatter. It will also remove unneeded spaces and brackets according to java coding standard. So it would change
Expand|Select|Wrap|Line Numbers
  1.  if ((user .equals(username)) && (pass .equals(password)))
  2.  
to
Expand|Select|Wrap|Line Numbers
  1.  if (user.equals(username) && pass.equals(password))
  2.  
4.) use meaningful names and no (homebrew) abbreviations. "pass" means something like "do not pass". So others prefer maybe "pwd" or "passwd" or "pword". So do not use abbreviations! Just name it how it sounds: "password".
If you already have used this variable name, then think how both differ from each other, for example use currentPassword and defaultPassword.
5.) Always use brackets in if-else-statements,; even if there currently is only one statement. (This is part of many coding standards). So other programmers can easily extend the code.
6.) username and password are constants. Coding standard tells you to write them all in uppercase. Correct is:
Expand|Select|Wrap|Line Numbers
  1. private static String USERNAME= "Kate", PASSWORD = "abc789";
  2.  
7.) follow DRY (Don't repeat yourself) principle:
Just think if you don't want to print the message to console but to show a popup or whatever, you have to change it on 2 places instead of one and have to retest 2 cases instead of one.
Also what if input strings are null? Just revert the comparison so it cannot crash!
Therefore I suggest following:
Expand|Select|Wrap|Line Numbers
  1. boolean credentialsAreCorrect = (USERNAME.equals(user) && PASSWORD.equals(pass));
  2. String message = (credentialsAreCorrect) ? "You've successfully ..." : "Sign in details ...";
  3. System.out.println(message);

1 1274
chaarmann
785 Recognized Expert Contributor
1.) As of java coding standard, use upper case only for class names, but lower case for method names.
So change your method name "SignIn" to "signIn".
2.) Use complete english sentences, ending with dot or whatever. So change "Sign in details incorrect" to "Sign in details are incorrect."
(By the way, don't you want the user to retry in this case?)
Also all other sentences are missing the end. "Enter your password" should be "Enter your password:". Look for spelling mistakes. Write "successfully" instead of "successfuly".
3.) Use a code formatter. It will also remove unneeded spaces and brackets according to java coding standard. So it would change
Expand|Select|Wrap|Line Numbers
  1.  if ((user .equals(username)) && (pass .equals(password)))
  2.  
to
Expand|Select|Wrap|Line Numbers
  1.  if (user.equals(username) && pass.equals(password))
  2.  
4.) use meaningful names and no (homebrew) abbreviations. "pass" means something like "do not pass". So others prefer maybe "pwd" or "passwd" or "pword". So do not use abbreviations! Just name it how it sounds: "password".
If you already have used this variable name, then think how both differ from each other, for example use currentPassword and defaultPassword.
5.) Always use brackets in if-else-statements,; even if there currently is only one statement. (This is part of many coding standards). So other programmers can easily extend the code.
6.) username and password are constants. Coding standard tells you to write them all in uppercase. Correct is:
Expand|Select|Wrap|Line Numbers
  1. private static String USERNAME= "Kate", PASSWORD = "abc789";
  2.  
7.) follow DRY (Don't repeat yourself) principle:
Just think if you don't want to print the message to console but to show a popup or whatever, you have to change it on 2 places instead of one and have to retest 2 cases instead of one.
Also what if input strings are null? Just revert the comparison so it cannot crash!
Therefore I suggest following:
Expand|Select|Wrap|Line Numbers
  1. boolean credentialsAreCorrect = (USERNAME.equals(user) && PASSWORD.equals(pass));
  2. String message = (credentialsAreCorrect) ? "You've successfully ..." : "Sign in details ...";
  3. System.out.println(message);
Mar 13 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nicolae Fieraru | last post by:
Hi All, I just start using MS Visual C++ 6.0 and I try to make a very simple application I created a dialog application and I added two controls (Edit boxes). Using ClassWizard I added a...
2
by: Raquel | last post by:
FORCE APPLICATION command is valid only at the 'instance' level. Why is this so? An instance may contain many databases. So, what command do I give if I want to force applications from a particular...
9
by: savvy | last post by:
i'm trying to compile a simple console application for sending a mail, my main idea is to schedule it to a particular time for sending mails using the windows schedular task lateron. Therefore i...
1
by: S.Hoa | last post by:
Hello I have simple application with an Access database using VB.NET. I do not know how to export them to create execute files to run to other computers. I looked in Tool and Help but can not find...
11
by: Sehboo | last post by:
Hi, I want to write a very very simple mail application which my father can use (maybe then I will be able to send and receive emails from him). This application will have a text box, where he...
1
by: Web learner | last post by:
I am trying to convert Simpson integration algorithm given in the second section of http://csharpcomputing.com/Tutorials/Lesson16.htm from Console App to an ASP.NET 2.0 page. Here is the code...
2
by: rkrane99 | last post by:
Hi, I am developing a .NET app in C# and I am using the VS 2005 built in installer. Everything works fine except that when I build the installation project, it produces 2 files, a .msi and a...
1
by: Bullitt | last post by:
Hello, When i connect i beamer to my laptop, i don't want my complete screen the whole time, that's why I want to create a small .exe which I can control with a hotkey. The program must switch...
2
by: xinnie | last post by:
Hi everyone, I need to write a simple chat program (form application) using TCP/IP methods - socket programming. So simple that all I need in my forms is a textbox for each in which I'll be able...
0
by: Tom Schoffelen | last post by:
Hi, I'm using VB Express 2010. I'm creating an application for digital smartboards to write on it and send the data directly to a web service. I'm using an Panel with inkOverlay (Microsoft.ink)...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.