weiqian1020

FreeStyler Online Aparrels

Jun 12th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1. package assignment;
  2. import java.util.Scanner;
  3. public class core{
  4.     static Scanner input;
  5.     int account;
  6.     String username[];
  7.     String password[];
  8.     public static void main(String[] args) {
  9.         int x = 0;
  10.         boolean bool = true;
  11.         core run = new core();
  12.         run.readDat();
  13.         do{
  14.             switch (x) {
  15.            
  16.             case 0:
  17.                 input = new Scanner(System.in);
  18.                 x = mainMenu();
  19.                 break;
  20.             case 1:
  21.                 x = run.admin();
  22.                 break;
  23.             case 2:
  24.                 x = run.user();
  25.                 break;
  26.             case 3:
  27.                 x = run.userLogin();
  28.                 break;
  29.             case 4:
  30.                 x = run.userReg();
  31.                 break;
  32.             default:
  33.                 bool = false;
  34.                 System.out.println(x);
  35.                 break;
  36.             }
  37.         } while (bool);
  38.     }
  39.    
  40.     private static int mainMenu(){
  41.         System.out.println("Welcome to FreeStyler Online Apparels");
  42.         System.out.println("Login As:\n(1)Admin\n(2)User");
  43.         boolean bool = true;
  44.         String n;
  45.         do {
  46.             try{
  47.                 n = input.nextLine();
  48.             } catch (Exception e) {
  49.                 System.out.println("Invalid input, " + e);
  50.                 System.out.println("Login As:\n(1)Admin\n(2)User");
  51.                 continue;
  52.             }
  53.             if (n.equals("1")){
  54.                 bool = false;
  55.                 return 1;
  56.             }
  57.             else if (n.equals("2")){
  58.                 bool = false;
  59.                 return 2;
  60.             }
  61.             else {
  62.                 System.out.println("Invalid input, please try again");
  63.                 System.out.println("Login As:\n(1)Admin\n(2)User");
  64.                 continue;
  65.             }
  66.         }while(bool);
  67.         return 0;
  68.     }
  69.     private int admin(){
  70.         boolean bool = true;
  71.         String user, pass = null;
  72.         do {
  73.             System.out.println("-1 to back");
  74.             System.out.print("Username: ");
  75.             try {
  76.                 user = input.nextLine();
  77.             } catch (Exception e){
  78.                 System.out.println("Error: " + e);
  79.                 continue;
  80.             }
  81.             if (backtrack(user)){
  82.                 bool = false;
  83.                 return 0;
  84.             }
  85.             System.out.print("Password: ");
  86.             try{
  87.                 pass = input.nextLine();
  88.             } catch (Exception e){
  89.                 System.out.println("Error: " + e);
  90.             }
  91.            
  92.             if (backtrack(pass)){
  93.                 bool = false;
  94.                 return 0;
  95.             }
  96.             else if (user.equals("admin") && pass.equals("admin")){
  97.                 System.out.println("Login Successfully");
  98.                 bool = false;
  99.                 return 5;
  100.             }
  101.             else {
  102.                 System.out.println("Invalid username or password");
  103.             }
  104.         }while (bool);
  105.         return 0;
  106.     }
  107.    
  108.     private int user(){
  109.         String x;
  110.         System.out.println("-1 to back");
  111.         System.out.println("(1)Login\n(2)Register");
  112.         try{
  113.         x = input.nextLine();
  114.         }
  115.         catch (Exception e){
  116.             System.out.println("Invalid input, " + e);
  117.             return 2;
  118.         }
  119.        
  120.         if (x.equals("1")){
  121.             return 3;
  122.         }
  123.         else if (x.equals("2")){
  124.             return 4;
  125.         }
  126.         else if (x.equals("-1")){
  127.             return 0;
  128.         }
  129.         else {
  130.             System.out.println("Invalid input, please try again");
  131.             return 0;
  132.         }
  133.     }
  134.    
  135.     private int userLogin(){
  136.         boolean bool = true;
  137.         String user, pass = null;
  138.        
  139.         do{
  140.             System.out.println("-1 to back");
  141.             System.out.print("Username: ");
  142.             try {
  143.                 user = input.nextLine();
  144.             } catch (Exception e){
  145.                 System.out.println("Error: " + e);
  146.                 continue;
  147.             }
  148.             if (user.equals("-1")){
  149.                 bool = false;
  150.                 return 2;
  151.             }
  152.             System.out.print("Password: ");
  153.             try{
  154.                 pass = input.nextLine();
  155.             } catch (Exception e){
  156.                 System.out.println("Error: " + e);
  157.             }
  158.             if (pass.equals("-1")){
  159.                 bool = false;
  160.                 return 2;
  161.             }
  162.             else {
  163.                 for (int i = 0; i < this.username.length; i++) {
  164.                     if (user.equals(this.username[i]) && pass.equals(this.password[i])){
  165.                         System.out.println("Login Successfully");
  166.                         bool = false;
  167.                         return 5;
  168.                     }
  169.                 }
  170.                 System.out.println("Invalid username or password");
  171.             }  
  172.         }while(bool);
  173.         return 0;
  174.     }
  175.    
  176.     private int userReg(){
  177.         String user, pass;
  178.         boolean bool = true;
  179.         boolean taken;
  180.         System.out.println("Register as");
  181.        
  182.         do{
  183.             taken = false;
  184.             System.out.print("Username: ");
  185.             user = input.nextLine();
  186.            
  187.             for (int i = 0; i < this.username.length; i++) {
  188.                 if (user.equals(this.username[i])){
  189.                     System.out.println("Username taken, please take different username");
  190.                     taken = true;
  191.                     continue;
  192.                 }
  193.             }
  194.            
  195.             if (taken){
  196.                 continue;
  197.             }
  198.            
  199.             else {
  200.                 System.out.println("Password: ");
  201.                 pass = input.nextLine();
  202.                 System.out.println("Account created successfully");
  203.                 bool = false;
  204.                 return 2;
  205.             }
  206.         }while(bool);
  207.         return 0;
  208.     }
  209.    
  210.     private void readDat(){
  211.         readWrite io = new readWrite();
  212.         io.openFile("login.txt"); //open and read file
  213.         io.readFile();
  214.         this.username = new String[io.user.size()];
  215.         this.password = new String[io.pass.size()];
  216.         for (int i = 0; i < this.password.length; i++) {
  217.             this.username[i] = io.user.get(i);
  218.             this.password[i] = io.pass.get(i);
  219.         }
  220.     }
  221.    
  222.     private boolean backtrack(String back){
  223.         if (back.equals("-1")){
  224.             return true;
  225.         }
  226.         else {
  227.             return false;
  228.         }
  229.     }
  230. }
  231.  
  232.  
  233.  
  234. package assignment;
  235.  
  236. import java.io.File;
  237. import java.util.Formatter;
  238. import java.util.Scanner;
  239. import java.util.ArrayList;
  240.  
  241. public class readWrite {
  242.     private Formatter x;
  243.     private Scanner y;
  244.     public ArrayList<String> user = new ArrayList<String>(1);
  245.     public ArrayList<String> pass = new ArrayList<String>(1);
  246.    
  247.     public void createFile(String fileName){
  248.         try {
  249.             x = new Formatter(fileName);
  250.         }
  251.         catch (Exception e) {
  252.             System.out.println(e);
  253.         }
  254.     }
  255.    
  256.     public void addRecord(String user, String pass){
  257.         x.format("%s%s\n", user + " ", pass);
  258.     }
  259.    
  260.     public void closeFile(){
  261.         x.close();
  262.     }
  263.  
  264.     public void openFile(String fileName){
  265.         try {
  266.             y = new Scanner(new File(fileName));
  267.         }
  268.         catch (Exception e) {
  269.             System.out.println(e);
  270.         }
  271.     }
  272.    
  273.     public void readFile(){
  274.         while(y.hasNext()){
  275.             user.add(y.next());
  276.             pass.add(y.next());
  277.         }
  278.     }
  279.    
  280.     public void closeRead(){
  281.         y.close();
  282.     }
  283. }
Add Comment
Please, Sign In to add comment