Class design and method for analyzing problems ----- User Logon example

Source: Internet
Author: User

1. The first thing to do in the User Login example is to implement the function first:

Public class LoginDemo01 {public static void main (String args []) {if (args. length! = 2) {// check whether the number of input parameters is 2 System. out. println ("the input parameters are incorrect. The System exits! "); // Give a correct format of System. out. println ("Format: java LoginDemo01 username and password"); System. exit (1); // system exit} String name = args [0]; // retrieve the username String password = args [1]; // retrieve the password if (name. equals ("xiaowu") & password. equals ("qiang") {// verify System. out. println ("welcome" + name +! ");} Else {System. out. println (" Incorrect username and password! ");}}};

 

2. Further analysis of the problem requires a dedicated verification class to encapsulate the verification and access the verification class through a class.
Class Check {// complete the specific verification Operation public boolean validate (String name, String password) {if (name. equals ("xiaowu") & password. equals ("qiang") {// verify return true ;}else {return false ;}}}; class Operate {// This class only calls the specific verification operation private String info []; // defines an array attribute for receiving all input parameters public Operate (String info []) {this.info = info; // obtain all input parameters through the constructor} public String login () {Check check = new Check (); // instantiate the Check object for checking Query Information this. isExit (); // judge whether the input parameter is correct String str = null; // return message String name = this.info [0]; // retrieve the name String password = this.info [1]; // retrieve the password if (check. validate (name, password) {// login verification str = "welcome" + name +! ";} Else {str =" Incorrect username and password! ";}Return str;} public void isExit () {// determine the number of parameters to determine whether to exit the program if (this.info. length! = 2) {System. out. println ("the input parameter is incorrect. The System exits! "); // Give a correct format of System. out. println ("Format: java LoginDemo02 username and password"); System. exit (1); // system exit }}; public class LoginDemo02 {public static void main (String args []) {Operate login = new Operate (args ); // instantiate the operation class Object System. out. println (bytes. login (); // information obtained after verification }};

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.