Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- String ANSI_RESET = "\u001B[0m";
- String ANSI_BLACK = "\u001B[30m";
- String ANSI_RED = "\u001B[31m";
- String ANSI_GREEN = "\u001B[32m";
- Scanner scanner = new Scanner(System.in);
- String command = scanner.nextLine();
- String[][] usersData = new String[4][2];
- // main loop
- while (!command.equals("end")) {
- String[] commandArgs = command.split(" ");
- switch (commandArgs[0]) {
- case "register":
- registrationProces(commandArgs, ANSI_RED, ANSI_RESET, usersData, ANSI_GREEN);
- break;
- case "delete": {
- // validate arguments
- if (validArguments(commandArgs, ANSI_RED, ANSI_RESET)) break;
- String username = commandArgs[1];
- String password = commandArgs[2];
- // validate username
- if (validUsername(username, ANSI_RED, ANSI_RESET)) break;
- // validate password
- if (validPassword(password, ANSI_RED, ANSI_RESET)) break;
- // find account to delete
- int accountIndex = -1;
- for (int i = 0; i < usersData.length; i++) {
- if (username.equals(usersData[i][0]) && password.equals(usersData[i][1])) {
- accountIndex = i;
- }
- }
- if (accountIndex == -1) {
- System.out.println(ANSI_RED + "Invalid account/password." + ANSI_RESET);
- break;
- }
- usersData[accountIndex][0] = null;
- usersData[accountIndex][1] = null;
- System.out.println(ANSI_GREEN + "Deleted account." + ANSI_RESET);
- break;
- }
- }
- // read next command
- command = scanner.nextLine();
- }
- }
- private static void registrationProces(String[] commandArgs, String ANSI_RED, String ANSI_RESET, String[][] usersData, String ANSI_GREEN) {
- // validate arguments
- if (validArguments(commandArgs, ANSI_RED, ANSI_RESET)) return;
- String username = commandArgs[1];
- String password = commandArgs[2];
- // validate username
- if (validUsername(username, ANSI_RED, ANSI_RESET)) return;
- // validate password
- if (validPassword(password, ANSI_RED, ANSI_RESET)) return;
- // check if username exists
- if (usernameExists(usersData, username, ANSI_RED, ANSI_RESET)) return;
- // find free slot
- int freeSlotIndex = getFreeSlotIndex(usersData);
- // no free slots
- if (freeSlotIndex == -1) {
- System.out.println(ANSI_RED + "The system supports a maximum number of 4 users." + ANSI_RESET);
- return;
- }
- // save user
- usersData[freeSlotIndex][0] = username;
- usersData[freeSlotIndex][1] = password;
- System.out.println(ANSI_GREEN + "Registered user." + ANSI_RESET);
- return;
- }
- private static int getFreeSlotIndex(String[][] usersData) {
- int freeSlotIndex = -1;
- for (int i = 0; i < usersData.length; i++) {
- if (usersData[i][0] == null) {
- freeSlotIndex = i;
- }
- }
- return freeSlotIndex;
- }
- private static boolean usernameExists(String[][] usersData, String username, String ANSI_RED, String ANSI_RESET) {
- boolean usernameExists = false;
- for (int i = 0; i < usersData.length; i++) {
- if (username.equals(usersData[i][0])) {
- usernameExists = true;
- }
- }
- if (usernameExists) {
- System.out.println(ANSI_RED + "Username already exists." + ANSI_RESET);
- return true;
- }
- return false;
- }
- private static boolean validArguments(String[] commandArgs, String ANSI_RED, String ANSI_RESET) {
- if (commandArgs.length < 3) {
- System.out.println(ANSI_RED + "Too few parameters." + ANSI_RESET);
- return true;
- }
- return false;
- }
- private static boolean validPassword(String password, String ANSI_RED, String ANSI_RESET) {
- if (password.length() < 3) {
- System.out.println(ANSI_RED + "Password must be at least 3 characters long." + ANSI_RESET);
- return true;
- }
- return false;
- }
- private static boolean validUsername(String username, String ANSI_RED, String ANSI_RESET) {
- if (username.length() < 3) {
- System.out.println(ANSI_RED + "Username must be at least 3 characters long" + ANSI_RESET);
- return true;
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment