Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.io.*;
- import java.util.*;
- public class Project2
- {
- public static void main(String [] args) throws IOException
- {
- String userName,password;
- String account = "";
- String optionA = "y";
- String optionB = "n";
- File accountInfo = new File ("Users.txt");
- FileWriter usersInfo = new FileWriter(accountInfo,true);
- PrintWriter edit = new PrintWriter(usersInfo);
- String filePath = ("Users.txt");
- String errorMessage1 = usersInfo + " not found";
- String errorMessage2 = "No users registered";
- if (!(accountInfo.exists()))
- JOptionPane.showMessageDialog(null,errorMessage1);
- else if (accountInfo.length() == 0)
- JOptionPane.showMessageDialog(null,errorMessage2);
- else
- {
- createUser();
- }
- edit.close();
- }
- public static void createUser() throws IOException
- {
- String userName,password;
- String account = "";
- String optionA = "y";
- String optionB = "n";
- File accountInfo = new File ("Users.txt");
- FileWriter usersInfo = new FileWriter(accountInfo,true);
- PrintWriter edit = new PrintWriter(usersInfo);
- String filePath = ("Users.txt");
- while(!account.equals(optionA)&& !account.equals(optionB))
- {
- account = JOptionPane.showInputDialog(null,"Do you have an existing account? Y/N");
- account = account.toLowerCase();
- if (account.equals(optionA))
- {
- userName = JOptionPane.showInputDialog(null,"Enter username");
- password = JOptionPane.showInputDialog(null,"Enter password");
- verifyLogin(userName,password,filePath);
- }
- else if (account.equals(optionB))
- {
- userName = JOptionPane.showInputDialog(null,"Please enter your desired username");
- password = JOptionPane.showInputDialog(null,"Please enter your desired password");
- String freshUser = userName + "," + password;
- edit.println(freshUser);
- }
- else
- JOptionPane.showMessageDialog(null,"Please enter either Y or N");
- }
- edit.close();
- }
- public static void verifyLogin(String username, String password, String usersInfo)
- {
- String line;
- String [] parts;
- try(BufferedReader br = new BufferedReader(new FileReader(usersInfo)))
- {
- line = br.readLine();
- parts = line.split(",");
- if(username.equals(parts[0]) && password.equals(parts[1])) //first line in the Users.txt is reserved for admin only
- {
- JOptionPane.showMessageDialog(null,"Welcome administrator " + username);
- administrator();
- }
- else
- {
- while((line = br.readLine()) != null)
- {
- parts = line.split(",");
- if(username.equals(parts[0]) && password.equals(parts[1]))
- {
- JOptionPane.showMessageDialog(null,"Welcome user " + username);
- user();
- }
- }
- JOptionPane.showMessageDialog(null,"User not found");
- createUser();
- }
- }
- catch (Exception a)
- {
- JOptionPane.showMessageDialog(null,"Unknown error occured");
- }
- }
- public static void administrator() throws IOException
- {
- String A = "Register a new user";
- String B = "Add a new facility";
- String C = "View availability of a facility";
- String D = "View bookinngs";
- String E = "Remove an existing facility";
- String F = "Make bookings";
- String G = "Record payments";
- String H = "View account statements";
- String[] choices = { A, B, C, D, E, F, G, H };
- String input = (String) JOptionPane.showInputDialog(null, "Choose action",
- "Actions available", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
- if (input.equals(A))
- registerANewUser();
- else if (input.equals(B))
- System.out.print("it works"); //addFacility();
- else if (input.equals(C))
- System.out.print("it works"); //viewFacility();
- else if (input.equals(D))
- System.out.print("it works"); //viewBookings();
- else if (input.equals(E))
- System.out.print("it works"); //remove();
- else if (input.equals(F))
- System.out.print("it works"); //makeBookings();
- else if (input.equals(G))
- System.out.print("it works"); //recordPayments();
- else if (input.equals(H))
- System.out.print("it works"); //viewAccountStatements();
- }
- public static void user()
- {
- String A = "View bookinngs";
- String B = "View account statements";
- String[] choices = { A, B };
- String input = (String) JOptionPane.showInputDialog(null, "Choose action",
- "Actions available", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
- if (input.equals(A))
- System.out.print("it works"); //viewBookings();
- else if (input.equals(B))
- System.out.print("it works"); //viewAccountStatements();
- }
- public static void registerANewUser() throws IOException
- {
- String userName,password;
- File accountInfo = new File ("Users.txt");
- FileWriter usersInfo = new FileWriter(accountInfo,true);
- PrintWriter edit = new PrintWriter(usersInfo);
- userName = JOptionPane.showInputDialog(null,"Please enter your desired username");
- password = JOptionPane.showInputDialog(null,"Please enter your desired password");
- String freshUser = userName + "," + password;
- edit.println(freshUser);
- edit.close();
- administrator();
- }
- }
Add Comment
Please, Sign In to add comment