IO version of User login registration case (Java)

Source: Internet
Author: User

2017-09-16

This is my first study note in the blog park.

Java Small White, strive to step in ...

What you learned today is the user login registration function.
4 Packages:
Itcast.cn.user Package User.java User class that describes user basic information, including member variables, parameterless constructors, and parametric constructs (optional). Get and Set methods

1   PackageItcast.cn.day22;2 /*3 * User Basic Description Package class4  */5 6  Public classUser {7     Private intUserName;8     Private intPassWord;9      PublicUser () {Ten         Super(); One     } A      PublicUser (intUserName,intPassWord) { -         Super(); -          This. username=UserName; the          This. password=PassWord;  -     } -      Public intGetUserName () { -         returnUserName; +     } -      Public voidSetusername (intuserName) { +          This. UserName =UserName; A     } at      Public intGetPassword () { -         returnPassWord; -     } -      Public voidSetPassword (intPassWord) { -          This. PassWord =PassWord; -     } in  -}

Tcast.cn.uerDao Package Userdao.java defines an interface that declares the login registration function

1  Public Interface  2publicabstractboolean  islogin (String userName, String passWord); 3  Public Abstract void Register (user user); 4 4}
View Code

The Itcast.cn.userDaoImpl package Userdaoimpl.java implements the Userdao interface, which describes the login registration method.
Note: In the logon method, the return value Boolean flag = True to log on successfully

One thing to note in the implementation process is that the declaration of the global variable BR,BW must be placed outside the try Brace
The IO version implementation method code is as follows:

1  Public classUserdaoimplImplementsuserdao{2 3 @Override4  Public BooleanIsLogin (String userName, String passWord) {5 BooleanFlag =false;6BufferedReader br =NULL;7 Try {8BR =NewBufferedReader (NewFileReader ("User,txt"));9String line =NULL;Ten  One  while((Line=br.readline ())! =NULL){ Astring[] Datas = line.split ("="); - if(Datas[0].equals (UserName) &&datas[1].equals (PassWord)) { -Flag =true; the  Break; - } - } - }  + Catch(FileNotFoundException e) { - //e.printstacktrace (); +SYSTEM.OUT.PRINTLN ("User information acquisition failed"); A}Catch(IOException e) { at //e.printstacktrace (); -SYSTEM.OUT.PRINTLN ("User logon Failed"); - } - if(br!=NULL){ - Try { - br.close (); in}Catch(IOException e) { - //e.printstacktrace (); toSystem.out.println ("User Freed resource failed"); + } - } the returnFlag; * } $ Panax Notoginseng @Override -  Public voidRegister (user user) { theBufferedWriter BW =NULL;  + Try { ABW =NewBufferedWriter (NewFileWriter ("User.txt")); theBw.write (User.getusername () + "=" +User.getpassword ()); + bw.newline (); -}Catch(IOException e) { $ //e.printstacktrace (); $SYSTEM.OUT.PRINTLN ("Registration failed"); -}finally{ - if(bw!=NULL){ the Try { - bw.close ();Wuyi}Catch(IOException e) { the //e.printstacktrace (); -SYSTEM.OUT.PRINTLN ("Registration Frees resource failed"); Wu } - } About } $}58}


Itcast.cn.usertest Package Usertest Class test class

In this class, there is an unresolved problem:
The scanner function cannot have one input, and the constructor method (true) needs to be seen more.

1  PackageItcast.cn.day22;2 3 ImportJava.util.Scanner;4 5  Public classUsertest {6  Public Static voidMain (string[] args) {7 //Welcome Interface8  while(true){9Userdao ud =NewUserdaoimpl ();TenSystem.out.println ("Welcome to the * * * * * * * * * *); OneSYSTEM.OUT.PRINTLN ("Login Please enter 1"); ASYSTEM.OUT.PRINTLN ("Register Please enter 2"); -SYSTEM.OUT.PRINTLN ("Return Please enter 3"); -  theSystem.out.println ("Please enter your choice:"); -Scanner sc =NewScanner (system.in); - intChoice =sc.nextint (); - Switch(choice) { +  Case1: -SYSTEM.OUT.PRINTLN ("* * * * Login Interface *****************"); + //Scanner SC1 = new Scanner (system.in); ASystem.out.println ("Please enter user name:"); atString UserName =sc.nextline (); -System.out.println ("Please Enter password:"); -String PassWord =sc.nextline (); -  - BooleanFlag =Ud.islogin (UserName, PassWord); - if(flag) { inSYSTEM.OUT.PRINTLN ("Login Successful"); -  Break; to}Else{ +System.out.println ("User name or password error"); - } the  *  Case2: $SYSTEM.OUT.PRINTLN ("* * * * Registration Interface ****************");Panax NotoginsengSystem.out.println ("Please enter user name:"); -String userName1 =sc.nextline (); theSystem.out.println ("Please Enter password:"); +String PassWord1 =sc.nextline (); A  theUser User =NewUser (); + user.getusername (); - User.getpassword (); $ ud.register (user); $SYSTEM.OUT.PRINTLN ("Registered successfully"); -  Break; -  Case3: the default: -System.out.println ("Thanks for using, welcome to come again next time");WuyiSystem.exit (0); the  Break; - } Wu } - } About}

IO version of User login registration case (Java)

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.