fahad005

java

Sep 17th, 2021
670
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Main use of this class:
  3. 1. to show welcome message
  4. 2. to verify id and pass for already existing user
  5. 3. to create new id and pass for new user
  6.  
  7. Extra use:
  8. 1. to create directory and file for user login
  9. */
  10.  
  11. package com.company;
  12.  
  13. import java.io.File;
  14. import java.util.Formatter;
  15. import java.util.Scanner;
  16.  
  17. public class Welcome {
  18.     // to take user input
  19.     Scanner sc = new Scanner(System.in);
  20.  
  21.     // constructor
  22.     Welcome() {
  23.         createDir();
  24.         welcomeMessage();
  25.     }
  26.  
  27.     // used to clear screen
  28.     public static void cls() {
  29.         try {
  30.             new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
  31.         } catch (Exception e) {
  32.             System.out.println(e);
  33.         }
  34.     }
  35.  
  36.     // to show welcome message
  37.     void welcomeMessage() {
  38.         // showing welcome message
  39.         System.out.println("Welcome to Fahad's Super Shop");
  40.         System.out.println("1. Login");
  41.         System.out.println("2. Registration");
  42.         System.out.print("Please Enter your choice: ");
  43.  
  44.         String lr = sc.next();
  45.         System.out.println();
  46.  
  47.         // matching user input and doing action
  48.         if (lr.equals("1")) {
  49.             loginForm();
  50.         } else if (lr.equals("2")) {
  51.             regForm();
  52.         } else {
  53.             System.out.println("Invalid input. try again.\n");
  54.             welcomeMessage();
  55.         }
  56.     }
  57.  
  58.     // creating directory and login file
  59.     void createDir() {
  60.         File dir = new File("E:/Login");
  61.         dir.mkdir();
  62.  
  63.         try {
  64.             File login = new File("E:/Login/login.txt");
  65.             login.createNewFile();
  66.         } catch (Exception e) {
  67.             System.out.println("File creation error!");
  68.         }
  69.     }
  70.  
  71.     // checking for valid login information
  72.     void loginForm() {
  73.         boolean found = false; // to check if the id and pass match
  74.         String id, pass;
  75.  
  76.         // taking user id and pass.
  77.         System.out.print("Please enter your id: ");
  78.         id = sc.next();
  79.         System.out.print("Please enter your pass: ");
  80.         pass = sc.next();
  81.  
  82.         try {
  83.             // opening file and reading previously saved information
  84.             File read = new File("E:/Login/login.txt");
  85.  
  86.             sc = new Scanner(read);
  87.             while (sc.hasNext()) {
  88.                 String sTemp = sc.next();
  89.                 String[] sTemp1 = sTemp.split(">");
  90.  
  91.                 if (id.equals(sTemp1[0]) && pass.equals(sTemp1[1])) {
  92.                     System.out.println("Login Successful\n");
  93.                     found = true;
  94.                     break;
  95.                 }
  96.             }
  97.             //sc.close();
  98.         } catch (Exception e) {
  99.             System.out.println("Please enter valid information!\n");
  100.         }
  101.  
  102.         // if the id and pass do not match
  103.         if (!found) {
  104.             System.out.println("Please enter valid information!\n");
  105.             loginForm(); // recalling loginForm to enter valid information again (this doesn't work. don't know why)
  106.         }
  107.     }
  108.  
  109.     // doing registration for new user
  110.     void regForm() {
  111.         String id, pass;
  112.  
  113.         // taking new id and pass
  114.         System.out.print("Please enter your new id: ");
  115.         id = sc.next();
  116.         System.out.print("Please enter your new pass: ");
  117.         pass = sc.next();
  118.  
  119.         // writing on file for further use of that id and pass
  120.         try {
  121.             Formatter write = new Formatter("E:/Login/login.txt");
  122.             write.format("%s\n", id + ">" + pass);
  123.  
  124.             System.out.println("Registration Successful.\n");
  125.             write.close();
  126.         } catch (Exception e) {
  127.             System.out.println("Something went wrong. Registration failed!\n");
  128.             //regForm(); // this doesn't work
  129.         }
  130.     }
  131. }
  132.  
RAW Paste Data