Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment;
- import java.util.Scanner;
- public class core{
- static Scanner input;
- int account;
- String username[];
- String password[];
- public static void main(String[] args) {
- int x = 0;
- boolean bool = true;
- core run = new core();
- run.readDat();
- do{
- switch (x) {
- case 0:
- input = new Scanner(System.in);
- x = mainMenu();
- break;
- case 1:
- x = run.admin();
- break;
- case 2:
- x = run.user();
- break;
- case 3:
- x = run.userLogin();
- break;
- case 4:
- x = run.userReg();
- break;
- default:
- bool = false;
- System.out.println(x);
- break;
- }
- } while (bool);
- }
- private static int mainMenu(){
- System.out.println("Welcome to FreeStyler Online Apparels");
- System.out.println("Login As:\n(1)Admin\n(2)User");
- boolean bool = true;
- String n;
- do {
- try{
- n = input.nextLine();
- } catch (Exception e) {
- System.out.println("Invalid input, " + e);
- System.out.println("Login As:\n(1)Admin\n(2)User");
- continue;
- }
- if (n.equals("1")){
- bool = false;
- return 1;
- }
- else if (n.equals("2")){
- bool = false;
- return 2;
- }
- else {
- System.out.println("Invalid input, please try again");
- System.out.println("Login As:\n(1)Admin\n(2)User");
- continue;
- }
- }while(bool);
- return 0;
- }
- private int admin(){
- boolean bool = true;
- String user, pass = null;
- do {
- System.out.println("-1 to back");
- System.out.print("Username: ");
- try {
- user = input.nextLine();
- } catch (Exception e){
- System.out.println("Error: " + e);
- continue;
- }
- if (backtrack(user)){
- bool = false;
- return 0;
- }
- System.out.print("Password: ");
- try{
- pass = input.nextLine();
- } catch (Exception e){
- System.out.println("Error: " + e);
- }
- if (backtrack(pass)){
- bool = false;
- return 0;
- }
- else if (user.equals("admin") && pass.equals("admin")){
- System.out.println("Login Successfully");
- bool = false;
- return 5;
- }
- else {
- System.out.println("Invalid username or password");
- }
- }while (bool);
- return 0;
- }
- private int user(){
- String x;
- System.out.println("-1 to back");
- System.out.println("(1)Login\n(2)Register");
- try{
- x = input.nextLine();
- }
- catch (Exception e){
- System.out.println("Invalid input, " + e);
- return 2;
- }
- if (x.equals("1")){
- return 3;
- }
- else if (x.equals("2")){
- return 4;
- }
- else if (x.equals("-1")){
- return 0;
- }
- else {
- System.out.println("Invalid input, please try again");
- return 0;
- }
- }
- private int userLogin(){
- boolean bool = true;
- String user, pass = null;
- do{
- System.out.println("-1 to back");
- System.out.print("Username: ");
- try {
- user = input.nextLine();
- } catch (Exception e){
- System.out.println("Error: " + e);
- continue;
- }
- if (user.equals("-1")){
- bool = false;
- return 2;
- }
- System.out.print("Password: ");
- try{
- pass = input.nextLine();
- } catch (Exception e){
- System.out.println("Error: " + e);
- }
- if (pass.equals("-1")){
- bool = false;
- return 2;
- }
- else {
- for (int i = 0; i < this.username.length; i++) {
- if (user.equals(this.username[i]) && pass.equals(this.password[i])){
- System.out.println("Login Successfully");
- bool = false;
- return 5;
- }
- }
- System.out.println("Invalid username or password");
- }
- }while(bool);
- return 0;
- }
- private int userReg(){
- String user, pass;
- boolean bool = true;
- boolean taken;
- System.out.println("Register as");
- do{
- taken = false;
- System.out.print("Username: ");
- user = input.nextLine();
- for (int i = 0; i < this.username.length; i++) {
- if (user.equals(this.username[i])){
- System.out.println("Username taken, please take different username");
- taken = true;
- continue;
- }
- }
- if (taken){
- continue;
- }
- else {
- System.out.println("Password: ");
- pass = input.nextLine();
- System.out.println("Account created successfully");
- bool = false;
- return 2;
- }
- }while(bool);
- return 0;
- }
- private void readDat(){
- readWrite io = new readWrite();
- io.openFile("login.txt"); //open and read file
- io.readFile();
- this.username = new String[io.user.size()];
- this.password = new String[io.pass.size()];
- for (int i = 0; i < this.password.length; i++) {
- this.username[i] = io.user.get(i);
- this.password[i] = io.pass.get(i);
- }
- }
- private boolean backtrack(String back){
- if (back.equals("-1")){
- return true;
- }
- else {
- return false;
- }
- }
- }
- package assignment;
- import java.io.File;
- import java.util.Formatter;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class readWrite {
- private Formatter x;
- private Scanner y;
- public ArrayList<String> user = new ArrayList<String>(1);
- public ArrayList<String> pass = new ArrayList<String>(1);
- public void createFile(String fileName){
- try {
- x = new Formatter(fileName);
- }
- catch (Exception e) {
- System.out.println(e);
- }
- }
- public void addRecord(String user, String pass){
- x.format("%s%s\n", user + " ", pass);
- }
- public void closeFile(){
- x.close();
- }
- public void openFile(String fileName){
- try {
- y = new Scanner(new File(fileName));
- }
- catch (Exception e) {
- System.out.println(e);
- }
- }
- public void readFile(){
- while(y.hasNext()){
- user.add(y.next());
- pass.add(y.next());
- }
- }
- public void closeRead(){
- y.close();
- }
- }
Add Comment
Please, Sign In to add comment