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 }};