Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PasswordKeeper{
- private ArrayList<SuperUser> users;
- private static Scanner keyboard = new Scanner(System.in);
- public PasswordKeeper(){
- users = new ArrayList();
- }
- public void login(){
- try{
- //readObject
- get();
- }catch(EOFException a){
- System.out.println("You are the First User!");
- }catch(IOException b){
- System.out.println(b);
- }
- catch(ClassNotFoundException c){
- System.out.println(c);
- }
- boolean loopDisplay = true;
- while(loopDisplay){
- System.out.println("1. Loginn2. Create Accountn3. Exit");
- int answer = keyboard.nextInt();
- if(answer == 1){
- String existingUser = "";
- String existingPass = "";
- System.out.println("Enter a Username followed by password:");
- existingUser = keyboard.next();
- existingPass = keyboard.next();
- SuperUser temp = new SuperUser(existingUser,existingPass);
- System.out.println();
- if(users.contains(temp)){
- //viewing superUser method
- temp.display();
- try{
- System.out.println("Saving.");
- save(users);
- }catch(IOException e){
- System.out.println(e);
- }
- }
- }
- public void save(ArrayList<SuperUser> obj)throws IOException{
- File file = new File("userInformation.dat");
- FileOutputStream fileOut = new FileOutputStream(file, false);
- BufferedOutputStream buffedOutput = new BufferedOutputStream(fileOut);
- ObjectOutputStream out = new ObjectOutputStream(buffedOutput);
- out.writeObject(obj);
- out.close();
- fileOut.close();
- }
- public ArrayList<SuperUser> get() throws IOException, ClassNotFoundException{
- FileInputStream fileIn = new FileInputStream("userInformation.dat");
- BufferedInputStream buffedInput = new BufferedInputStream(fileIn);
- ObjectInputStream in = new ObjectInputStream(buffedInput);
- users = (ArrayList<SuperUser>) in.readObject();
- in.close();
- fileIn.close();
- return users;
- }
- public class SuperUser implements Serializable{
- private String userName;
- private String password;
- private static Scanner keyboard = new Scanner(System.in);
- private ArrayList<LoginInfo> info = new ArrayList();
- public SuperUser(String name, String pass){
- userName = name;
- password = pass;
- }
- public String getUser(){
- return userName;
- }
- public void display(){
- int count = 1;
- if(info.size() == 0){
- System.out.println("You don't have any accounts saved yet!" +
- "nGo and add some!");
- }else{
- for(LoginInfo i: info){
- System.out.println(count + " " + i.getSite());
- count ++;
- }
- }
- System.out.println();
- System.out.println("Add,Delete or Logout " + info.size());
- String next = keyboard.next();
- if(next.equalsIgnoreCase("add")){
- add();
- }else if(next.equalsIgnoreCase("delete")){
- delete();
- }else if(numberCheck(next)){
- int choice = (int) Integer.parseInt(next) - 1;
- edit(choice);
- }else if(next.equalsIgnoreCase("logout")){
- System.out.println(info.size());
- }
- }
- public boolean numberCheck(String in){
- try{
- Integer.parseInt(in);
- }catch(NumberFormatException e){
- return false;
- }
- return true;
- }
- public void add(){
- System.out.println("What is the website name?:");
- String trash = keyboard.nextLine();
- String webName = keyboard.nextLine();
- System.out.println("The Username?:");
- String webUsername = keyboard.nextLine();
- System.out.println("The Password?:");
- String webPass = keyboard.nextLine();
- info.add(new LoginInfo(webUsername, webPass, webName));
- System.out.println(info.size());
- System.out.println("Application added!");
- display();
- }
- public void delete(){
- System.out.println("Which account # would you like to delete?");
- int userDelete = keyboard.nextInt();
- info.remove(userDelete - 1);
- System.out.println("Account removed.");
- display();
- }
- public void edit(int index){
- System.out.println(info.get(index));
- System.out.println("Would you like to change the information? y/n");
- String trash = keyboard.nextLine();
- String yes = keyboard.nextLine();
- if(String.valueOf(yes.charAt(0)).equalsIgnoreCase("y")){
- System.out.println("Username?");
- String username = keyboard.nextLine();
- System.out.println("Password?");
- String password = keyboard.nextLine();
- info.set(index, new LoginInfo(username, password,info.get(index).getSite()));
- System.out.println("Information Updated!n");
- display();
- }else{
- display();
- }
- }
- @Override
- public int hashCode(){
- return userName.hashCode();
- }
- @Override
- public boolean equals(Object other){
- if(other instanceof SuperUser){
- SuperUser toCompare = (SuperUser) other;
- return this.userName.equals(toCompare.getUser());
- }
- return false;
- }
Add Comment
Please, Sign In to add comment