After ten cycles registered account to ensure the program restart, use these ten account can log on successfully

 

 

 

 

User Templates

import java.io.Serializable;

public class User implements Serializable{

    
    //账号
    private String id;
    
    
    //密码
    private String password;


    public User(String id, String password) {
        super();
        this.id = id;
        this.password = password;
    }


    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }
    
    
    public static void main(String[] args) {
    
    }
    
}

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Zuoye02 {

    
    static Scanner sc = new Scanner(System.in);
    public static Map<String,String> map = new HashMap();
    static String choose = null;
     Public  static  void logo () { 
        
        
        // Register 
        System.out.println ( "Registration:" ); 
        System.out.println ( "Please enter the account number you want to register:" ); 
        String the above mentioned id = sc.next (); 
        
        System.out.println ( "Please enter the password for the account you want to register:" ); 
        String password = sc.next (); 
        
        
        map.put (the above mentioned id, password); 
        
        
        
        
    } 
    
    public  static  void main (String [] args) { 
        
        
        / / write a set of 
        
        // requirements:
         // after ten cycles registered account to ensure the program restart, use these ten successful login account can 
        
    
        // call registration methods: 
        
        
            logo ();
            System.out.println ( "successfully registered!" ); 
            Start: the while ( to true ) { 
            System.out.println ( "whether to continue registration (the y-/ the n-)" ); 
            String the Choose = sc.next (); 
            
            IF (the Choose .equalsIgnoreCase ( "the y-" )) { 
                
                logo (); 
                
            } the else { 
                
                BREAK Start; 
            } 
            } 
    

        // make sure the system is restarted before using the account can log in successfully, then it is imperative program (registered good account) => file clip
         // and this is the subject to the object may be a fluidized serialization and deserialization 
        
        
        
        // 1 2 output address: calling FileOutStra .. ObjectOutStr .. write two classes
    
            FileOutputStream fos = null;
            ObjectOutputStream ops = null;
        
        //再输入 读取  read  FileIn.....   ObjectRead
                FileInputStream fis = null;
                ObjectInputStream ois = null;
                
        
        try {
            fos = new FileOutputStream("C:/User/User.txt");
            fis = new FileInputStream("C:/User/User.txt");
            
            try {
                ops = new ObjectOutputStream(fos);
                ois = new ObjectInputStream(fis);
                
                ops.writeObject(map);
                
                System.out.println("注册成功!");
                ops.flush();
                ops.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Map<String, String> userIn = null;
        System.out.println ( "User login:" ); 
        System.out.println ( "Please enter your account number:" );
         the try {
              the try { 
                userIn = (the Map <String, String> ) ois.readObject (); 
            } the catch (a ClassNotFoundException E) {
                 // the TODO Auto-Generated Block the catch 
                e.printStackTrace (); 
            } the catch (IOException E) {
                 // the TODO Auto-Generated Block the catch 
                e.printStackTrace (); 
            } 
                // log
            The above mentioned id = String sc.next (); 
            System.out.println ( "Please enter your password:" ); 
            String password = sc.next (); 
            
            the User in = new new the User (the above mentioned id, password);
             IF (userIn.containsKey (in .getId ()) && userIn.containsValue (in.getPassword ())) { 
                
                
                System.out.println ( "Login successful!" ); 
                
                
                
            } the else  IF (userIn.containsKey (in.getId (!)) ||! userIn .containsValue (in.getPassword ())) { 
                System.out.println ( "wrong password" ); 
                
                
            } the else {
                
                 
                System.out.println ("登录失败!");
                
                
            }
            
//        } catch (ClassNotFoundException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//    } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
        }finally {
            
            try {
                fis.close();
                ois.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace (); 
            } 
            
            
            
        } 
    
        
        
    } 

    
    
}
Import java.io.BufferedReader;
 Import a java.io.FileInputStream;
 Import java.io.FileNotFoundException;
 Import java.io.FileReader;
 Import java.io.IOException;
 Import java.io.ObjectInputStream;
 Import a java.io.Reader;
 Import java.util.HashMap;
 Import java.util.Map; 

// test whether the class to read text saved us a good account registered 
public  class Demo { 
    
    public  static  void main (String [] args) { 
        
        // read using reverse sequence input of 
         the Map <String, String> MAP1 = new new the HashMap ();
        
        FileInputStream fis = null;
        
        ObjectInputStream ois = null;
        
        
        try {
            fis = new FileInputStream("C:/User/User.txt");
            try {
                ois = new ObjectInputStream(fis);
                try {
                    Map<String,String>  user = (Map<String,String>)ois.readObject();
                    
                    for (Map.Entry<String, String> demo : user.entrySet()) {
                        
                        System.out.println("用户账号是:"+demo.getKey()+"用户密码是:"+demo.getValue());
                }
                
                    
                    
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            
            try {
                ois.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
        }

        
        
    }
    

}

The test results first test whether the login is successful (no reboot) After successful registration in the program

 

 To see if the text is passed into the program something

 

 Later found to have been stored content, are not we just test registered a good account:

 

 Here I found a BUG clearly registered three but only display two at the back but this did not know this is true (because the Map is to ensure the uniqueness of the collection account) is registered in the condition where the small increase   

After some thought of my own, only to find that at the time of registration plus a few conditions

Originally, I use Map collection is to ensure the uniqueness of the account but has finished doing his mind behind the

 

So I was over there were registered code modification (opening and closing principle)

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Zuoye02 {

    static Scanner sc = new Scanner(System.in);
    public static Map<String, String> map = new HashMap();
    static String choose = null;

    public static void logo() {

        // 注册
        System.out.println("注册:");
        System.out.println("请输入您要注册的账号:");
        String id = sc.next();
        System.out.println("请输入您要注册账号的密码:");
        String password = sc.next();

//以下为修改内容:
for (Map.Entry<String, String> entry : map.entrySet()) { if (id.equalsIgnoreCase(entry.getKey())) { System.out.println("用户已存在!"); return; } } //这里结束 map.put(id, password); }

我加入判断后再输出的结果:

 

 

 

 原来输入可以正确的,因为用户已存在 则就没有将其存入文本这样再输出:

 

就是只能输出用户账号唯一的用户,确保了用户账号的唯一性  但有时也要看需求而定

 

本答案仅供参考

 

Guess you like

Origin www.cnblogs.com/bichen-01/p/11295006.html