Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Accounts
- {
- public static void main(String args[])throws IOException
- {
- BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
- Methods met=new Methods();
- met.GMes();
- String op=null;
- try{
- op=in.readLine();
- op=op.toLowerCase();
- if (op.equals("login") || op.equals("register"))
- {
- if (op.equals("login"))
- {
- met.login();
- }else{
- met.register();
- }
- }else{
- System.out.println("Wrong command entered.");
- }
- }catch (Exception e){
- System.out.println("Error");
- }
- }
- }
- class Methods
- {
- private File dir=new File("acc/");
- private String[] acc=dir.list();
- void GMes()
- {
- System.out.println("Welcome to the login/register section\n\nWould you like to Login or Register?\n(login/register)");
- }
- void login()throws IOException
- {
- BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
- System.out.println("\t-=LOGIN=-");
- System.out.println("Enter your username:");
- String name=in.readLine();
- acc=dir.list();
- if(checkName(name))
- {
- System.out.println("Enter your password:");
- String pass=in.readLine();
- if (checkPass(pass,name))
- {
- Login l=new Login(name,pass);
- l.wel();
- }else{
- System.out.println("Wrong Password");
- }}else{
- System.out.println("No such login name");
- }
- }
- boolean checkName(String name)
- {
- boolean exist=false;
- int i=0;
- while(!exist && i<acc.length)
- {
- if (acc[i].equals(name))
- {
- exist=true;
- }
- i++;
- }
- return exist;
- }
- boolean checkPass(String pass,String name)throws IOException
- {
- BufferedReader in=new BufferedReader(new FileReader("acc/"+name));
- String password=in.readLine();
- if (password.equals(pass))
- {
- return true;
- }else{
- return false;
- }
- }
- void register()throws IOException
- {
- BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
- boolean used=false;
- String name=null;
- while (!used)
- {
- System.out.println("\t-=REGISTER=-");
- System.out.println("Please enter your desired username:");
- name=in.readLine();
- if (usedName(name))
- {
- used=false;
- System.out.println("Username already taken, please choose another one.");
- }else{
- used=true;
- }
- }
- System.out.println("Enter password for you account:");
- String pass=in.readLine();
- addPass(pass,name);
- System.out.println("Account created.\n");
- login();
- }
- boolean usedName(String name)throws IOException
- {
- File n=new File("acc/"+name);
- if(n.exists())
- {
- return true;
- }else{
- n.createNewFile();
- return false;
- }
- }
- void addPass(String pass, String name)throws IOException
- {
- BufferedWriter out=new BufferedWriter(new FileWriter("acc/"+name));
- out.write(pass);
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement