Advertisement
Guest User

Untitled

a guest
May 18th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Accounts
  4. {
  5.     public static void main(String args[])throws IOException
  6.     {
  7.         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  8.         Methods met=new Methods();
  9.         met.GMes();
  10.         String op=null;
  11.         try{
  12.             op=in.readLine();
  13.             op=op.toLowerCase();
  14.             if (op.equals("login") || op.equals("register"))
  15.             {
  16.                 if (op.equals("login"))
  17.                 {
  18.                     met.login();
  19.                 }else{
  20.                     met.register();
  21.                 }
  22.             }else{
  23.                 System.out.println("Wrong command entered.");
  24.             }
  25.         }catch (Exception e){
  26.         System.out.println("Error");
  27.         }
  28.  
  29.     }
  30. }
  31.  
  32. class Methods
  33. {
  34.     private     File dir=new File("acc/");
  35.     private     String[] acc=dir.list();
  36.    
  37.     void GMes()
  38.     {
  39.         System.out.println("Welcome to the login/register section\n\nWould you like to Login or Register?\n(login/register)");
  40.     }
  41.    
  42.     void login()throws IOException
  43.     {
  44.         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  45.         System.out.println("\t-=LOGIN=-");
  46.         System.out.println("Enter your username:");
  47.         String name=in.readLine();
  48.         acc=dir.list();
  49.         if(checkName(name))
  50.         {
  51.             System.out.println("Enter your password:");
  52.             String pass=in.readLine();
  53.             if (checkPass(pass,name))
  54.             {
  55.                 Login l=new Login(name,pass);
  56.                 l.wel();
  57.             }else{
  58.                 System.out.println("Wrong Password");
  59.         }}else{
  60.             System.out.println("No such login name");  
  61.         }
  62.     }
  63.    
  64.     boolean checkName(String name)
  65.     {
  66.         boolean exist=false;
  67.         int i=0;
  68.         while(!exist && i<acc.length)
  69.         {
  70.             if (acc[i].equals(name))
  71.             {
  72.                 exist=true;
  73.             }
  74.             i++;
  75.         }
  76.         return exist;
  77.     }
  78.    
  79.     boolean checkPass(String pass,String name)throws IOException
  80.     {
  81.         BufferedReader in=new BufferedReader(new FileReader("acc/"+name));
  82.         String password=in.readLine();
  83.         if (password.equals(pass))
  84.         {
  85.             return true;
  86.         }else{
  87.             return false;
  88.         }
  89.     }
  90.        
  91.    
  92.     void register()throws IOException
  93.     {
  94.         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  95.         boolean used=false;
  96.         String name=null;
  97.         while (!used)
  98.         {
  99.             System.out.println("\t-=REGISTER=-");
  100.             System.out.println("Please enter your desired username:");
  101.             name=in.readLine();
  102.             if (usedName(name))
  103.             {
  104.                 used=false;
  105.                 System.out.println("Username already taken, please choose another one.");
  106.             }else{
  107.                 used=true;
  108.             }
  109.         }  
  110.         System.out.println("Enter password for you account:");
  111.         String pass=in.readLine();
  112.         addPass(pass,name);
  113.         System.out.println("Account created.\n");
  114.         login();
  115.     }
  116.    
  117.     boolean usedName(String name)throws IOException
  118.     {
  119.         File n=new File("acc/"+name);
  120.         if(n.exists())
  121.         {
  122.             return true;
  123.         }else{
  124.             n.createNewFile();
  125.             return false;
  126.         }
  127.     }
  128.    
  129.     void addPass(String pass, String name)throws IOException
  130.     {
  131.         BufferedWriter out=new BufferedWriter(new FileWriter("acc/"+name));
  132.         out.write(pass);
  133.         out.close();
  134.     }
  135.        
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement