wm: java

ref: b0e7cab307f99283490c5fd1d3e7d085492c4155
dir: /Main.java/

View raw version
//package BookProj;

// XXX get rid of *s
// handle throws
// make whatever you can final


/*
import java.util.regex.Pattern; // Pattern, Matcher
import java.util.Matcher; // Scanner, ArrayList
import java.util.Scanner;
*/
import java.util.*;
import java.time.LocalDateTime;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

import java.io.Console;
import java.io.File;

enum Err
{
	Ok,

	NoBook, /* no such book has found */
	BookReserved, /* book is reserved by someone else */
	NoBookReserved, /* no book is revserved */

	NoUser, /* no such user has found */
	UserHasABook, /* User has already registered a book */
	UserHasNoBook,
	Age; /* age doesn't match */
}

enum Role
{
	Admin,
	User;
}
 
enum BookAttr
{
	ISBN,
	Status,
	Name,
	Author,
	AgeGroup,
	Abstract,
	Owner;
}

enum AdminAttr
{
	Username,
	role,
	Reserved,
	ID,
	RegTime,
	// Password,
	Active;
}

enum UserAttr
{
	Username,
	role,
	Reserved,
	ID,
	RegTime,
	Age,
	Active;
}

interface Library 
{
	/* admin */
	boolean Login(Admin a, String pass);
	void Logout(Admin a);

	Admin NewAdmin(String user, String pass);
	void DelAdmin(Admin a);

	boolean CheckAdmin(String a);
	Admin MatchAdmin(String un);
	boolean FindAdmin(AdminAttr k, Pattern p);

	/* user */
	boolean Login(User u);
	// void Logout(User u);

	// User NewUser();
	// void DelUser(User u);

	boolean CheckUser(String u);
	User MatchUser(String un);
	boolean FindUser(UserAttr k, Pattern p);

	/* books */
	Err TakeBook(String un, String isbn);
	Err TakeBook(Admin u, Book b);
	Err TakeBook(User u, Book b);

	Err ReturnBook(User u);
	Err ReturnBook(Admin u);

	Book NewBook(String isbn, String name, String author, int agegroup, String abst);
	void DelBook(Book b);

	boolean CheckBook(String ISBN);
	Book MatchBook(String isbn);
	boolean FindBook(BookAttr k, Pattern p);

	/*
	Err ReturnBook(Book b);
	boolean WriteStatus();
	boolean FindBook(boolean q);  for Status value searchs 
	*/


	interface UI
	{
		void Program();
		void LoginMenu();

		/* checks password with user, gets input from GetPassword */
		// boolean AskPass(Admin u);
		// boolean AskPass(User u);

		/* different Menus, for different Users to handle */
		void Menu(Admin u);
		// void Menu(User u);

		/* Gets a String, either console or scanner, checks if it's valid or not, and encrypt it */
		String GetUsername();
		String GetPassword();
		
		/* asks for user/book info, returns username/isbn if it exists */
		// String AskAdmin();
		// String AskUser();
		// String AskBook();

		/* search foolan menu */
		// void SearchAdmin();
		void SearchUser();
		void SearchBook();

		/* add foolan menu */
		void AddAdmin();
		// void AddUser();
		void AddBook();

		/* remove foolan menu */
		// void RemAdmin();
		// void RemUser();
		// void RemBook(); // maybe it's stolen

		/* list foolan menu */
		void ListAdmin();
		void ListUser();
		void ListBook();

		/* take and return book menus */
		void TakeMenu(Admin a);
		void TakeMenu(User u);
		// void ReturnMenu(Admin a);
		// void ReturnMenu(User u);

		/* handles result of Take/Return menu */
		void Result(Err res);
	}
}

/* i personally don't use it but here is a converter */
class Books
{
	HashMap<BookAttr, String> Book = new HashMap<BookAttr, String>();
	HashMap<String, HashMap> Booklist = new HashMap<String, HashMap>();

	HashMap<String, TreeMap> Mkbl(HashMap<String, Book> bl)
	{
		HashMap<String, TreeMap> list = new HashMap<String, TreeMap>();
		for(Book i: bl.values())
		{
			list.put(i.getISBN(), i.toMap());
		}
		return list;
	}
}

class Book
{
	/* book is untaken by default */
	private	boolean Status = false;
	private String Abstract, Author, Name, ISBN;
	private String Owner = ""; /* Username of user who reserved it */
	private int AgeGroup;
	/* should a register be a constractor? XXX
		 should it even named register? */

	Book(String isbn, String name, String author, int age, String abs)
	{
		/* joy of joys willy */
		this.ISBN = isbn;
		this.Name = name;
		this.Author = author;
		this.AgeGroup = age;
		this.Abstract = abs;
	}

	boolean AgeOk(int age)
	{
		if(this.AgeGroup <= age)
			return true;
		else
			return false;
	}

	boolean setOwner(String u)
	{
		if(!this.Owner.isEmpty())
		{
			System.err.println("A User (ID:" + this.getOwner() + ") already got the book");
			return false;
		}
		this.Owner = u;
		return true;
	}

	boolean setStatus(boolean b)
	{
		if(this.Status == b)
		{
			System.err.println("book (ISBN: " + this.getISBN() + ") status already set to: " + this.Status);
			return false;  
		}
		this.Status = b;
		return true;
	}

	boolean getStatus()
	{
		return Status;
	}

	String getAbstract()
	{
		return this.Abstract;
	}

	String getAuthor()
	{
		return this.Author;
	}

	String getName()
	{
		return this.Name;
	}

	String getISBN()
	{
		return this.ISBN;
	}

	int getAgeGroup()
	{
		return this.AgeGroup;
	}

	String getOwner()
	{
		return this.Owner;
	}

	TreeMap<BookAttr, String> toMap()
	{
		TreeMap<BookAttr, String> temp = new TreeMap<BookAttr, String>();

		temp.put(BookAttr.ISBN, this.ISBN);
		temp.put(BookAttr.Status, Boolean.toString(this.Status));
		temp.put(BookAttr.Name, this.Name);
		temp.put(BookAttr.Author, this.Author);

		temp.put(BookAttr.AgeGroup, Integer.toString(this.AgeGroup));
		temp.put(BookAttr.Abstract, this.Abstract);
		temp.put(BookAttr.Owner, this.Owner);
		return temp;
	}
}

abstract class BasicUser
{
	private Role role;
	private String Username, Reserved;
	private int ID;
	private LocalDateTime RegTime;
	public boolean Active = false;

	// abstract boolean IsReserved(String ISBN);

	BasicUser(Role r, String u, int id, LocalDateTime rt)
	{
		this.role = r;
		this.Username = u;
		this.ID = id;
		this.RegTime = rt;
	}

	Role getRole()
	{
		return this.role;
	}

	String getUsername()
	{
		return this.Username;
	}

	int getID()
	{
		return this.ID;
	}

	boolean setReserved(String ISBN)
	{
		/* ensure he has no books */
		if(this.Reserved == null)
		{
			/* ensure book is not taken by someone else 
			if(
			return true; */
			return true;
		}
		else
		{
			System.err.println("the user already have a book");
			return false;
		}	
	}

	boolean hasReserved()
	{
		if(this.Reserved.isEmpty())
			return false;
		else
			return true;
	}

	String getReserved()
	{
		return this.Reserved;
	}

	LocalDateTime getRegTime()
	{
		return this.RegTime;
	}
}

class Admin extends BasicUser
{
	private String Password;
	Admin(String user)
	{
		super(Role.Admin, user, (int)(Math.random()*100), LocalDateTime.now());

	}
	Admin(String user, String Pass)
	{
		/* maybe check if password is too easy? */
		super(Role.Admin, user, (int)(Math.random()*100), LocalDateTime.now());
		this.setPassword(Pass);
	}

	boolean setReserved(String ISBN)
	{
		return false; /* XXX */
	}

	boolean isReserved(String ISBN)
	{
		return false; /* XXX */
	}
	boolean checkPass(String pass)
	{
		if(this.Password.equals(pass))
			return true;
		else
			return false;
	}
	void setPassword(String pass)
	{
		this.Password = pass;
	}

	TreeMap<AdminAttr, String> toMap()
	{
		TreeMap<AdminAttr, String> temp = new TreeMap<AdminAttr, String>();

		temp.put(AdminAttr.Username, getUsername());
		temp.put(AdminAttr.role, getRole().toString());
		temp.put(AdminAttr.Reserved, getReserved());
		temp.put(AdminAttr.ID, Integer.toString(getID()));
		temp.put(AdminAttr.RegTime, getRegTime().toString());
		/*  temp.put(AdminAttr.Password, this.Password); secrect secrect secrect */
		temp.put(AdminAttr.Active, Boolean.toString(this.Active));

		return temp;
	}
}

class User extends BasicUser
{
	private int Age;
	User(String username, int age)
	{
		super(Role.User, username, (int)(Math.random()*1000+101), LocalDateTime.now());
		this.Age = age;
	}
	int getAge()
	{
		return this.Age;
	}

	boolean IsReserved(Book b)
	{
		if(this.getReserved().equals(b.getISBN()))
			return true;
		else
			return false;
	}

	boolean IsReserved(String ISBN)
	{
		if(this.getReserved() == ISBN)
			return true;
		else
			return false;
	}

	TreeMap<UserAttr, String> toMap()
	{
		TreeMap<UserAttr, String> temp = new TreeMap<UserAttr, String>();

		temp.put(UserAttr.Username, this.getUsername());
		temp.put(UserAttr.role, getRole().toString());
		temp.put(UserAttr.Reserved, getReserved());
		temp.put(UserAttr.ID, Integer.toString(getID()));
		temp.put(UserAttr.RegTime, getRegTime().toString());
		temp.put(UserAttr.Age, Integer.toString(this.Age));
		temp.put(UserAttr.Active, Boolean.toString(this.Active));

		return temp;
	}
}

class Lib implements Library
{
	/* Username, Admin/User, ISBN, Book */
	private HashMap<String, Admin> AdminList = new HashMap<String, Admin>();
	private HashMap<String, User> UserList = new HashMap<String, User>();
	private HashMap<String, Book> BookList = new HashMap<String, Book>();

	private ArrayList<Admin> AdminQuery = new ArrayList<Admin>();
	private ArrayList<User> UserQuery = new ArrayList<User>();
	private ArrayList<Book> BookQuery = new ArrayList<Book>();

	static Admin adm;
	static User usr;
	int ResCount = 0;


	/* is hasNextLine is of any of use? i wonder, says timmy */
	public int readint(Scanner s, String errmsg, String inputmsg)
	{
		System.out.println(inputmsg);
		while(!s.hasNextLine())
		{
			System.err.println(errmsg);
			System.out.print(inputmsg);
			s.nextLine();
		}
		return s.nextInt();
	}

	public String readstr(Scanner s, String errmsg, String inputmsg)
	{
		System.out.println(inputmsg);
		while(!s.hasNextLine())
		{
			System.err.println(errmsg);
			System.out.print(inputmsg);
			s.nextLine();
		}
		return s.nextLine();
	}

	public boolean Login(Admin u, String pass)
	{
			/* clear null */
			adm = null;
			if(u.Active)
				throw new IllegalStateException("Admin is already active?");
			/* maybe later i want to do more stuff here */
			if(u.checkPass(pass))
			{
				u.Active = true;
				adm = u;
				return true;
			}
			else
				return false;
			
	}

	public void Logout(Admin u)
	{
		boolean b = false;

		for(Admin i : this.AdminList.values())
		{
			if(i == u)
				b = true;
		}

		if(!b)
			throw new NoSuchElementException("No such Admin exists.");

		u.Active = false;
	}

	public boolean Login(User u)
	{
		if(u.Active)
			throw new IllegalStateException("Admin is already active?");
		/* maybe later i want to do more stuff here */
		u.Active = true;
		return true;
	}

	public Admin NewAdmin(String user, String pass)
	{
		Admin a;
		for(Admin i : this.AdminList.values())
		{
			if(i.getUsername().equals(user))
				throw new IllegalArgumentException("Admin already exists");
		}

		/* if user is "admin", use defaults */
		if(user.equals("admin"))
			a = new Admin("admin", "pass");
		else
			a = new Admin(user, pass);

		if(this.AdminList.size() == 0)
			adm = a;

		this.AdminList.put(a.getUsername(), a);
		return a;
	}

	/* i could've made them overload,
		however, this might led to unwanted results */
	public void DelAdmin(Admin u)
	{
		if(!u.getReserved().isEmpty())
		{
			System.err.println("Admin " + u.getUsername() + " has a reserved book.");
			return;
		}
		this.AdminList.remove(u.getUsername(), u);
	}

	public void DelUser(User u)
	{
		if(!u.getReserved().isEmpty())
		{
			System.err.println("User " + u.getUsername() + " has a reserved book.");
			return;
		}
		this.UserList.remove(u.getUsername(), u);
	}

	public Book NewBook(String isbn, String name, String author, int agegroup, String abst)
	{
		for(Book b : this.BookList.values())
		{
			if(b.getISBN().equals(isbn))
				throw new IllegalStateException("Book already exist");
		}

		Book b = new Book(isbn, name, author, agegroup,  abst);
		this.BookList.put(isbn, b);
		return b;
	}

	public void DelBook(Book b)
	{
		if(!b.getOwner().isEmpty())
		{
			// System.err.println("User " + b.getOwner() + " have reserved this book.");
			throw new IllegalStateException("Book is reserved.");
		}
		this.BookList.remove(b.getISBN());
	}

	public boolean CheckBook(String isbn)
	{
		boolean b = false;

		for(Book i : this.BookList.values())
		{
			if(i.getISBN().equals(isbn))
				b = true;
		}

		if(!b)
		{
			System.err.println("No such user found, try again");
		}
		return b;
	}

	public Book MatchBook(String isbn)
	{
		Book b = null;

		for(Book i : this.BookList.values())
		{
			if(i.getISBN().equals(isbn))
				b = i;
		}

		if(b == null)
			throw new NoSuchElementException("No such Admin exists.");

		return b;
	}
/*
	public boolean FindBook(BookAttr k, Pattern p)
	{
		//
		this.BookQuery.clear();
		Matcher m;
		switch(k)
		{
			// XXX should i use i.getFoolan.contains()?
			case AgeGroup:
				for(Book i : BookList.values())
				{
					m = p.matcher(Integer.toString(i.getAgeGroup()));
					if(m.find())
						this.BookQuery.add(i);
				}
				break;

			case Abstract:
				for(Book i : BookList.values())
				{
					m = p.matcher(i.getAbstract());
					if(m.find())
						this.BookQuery.add(i);
				}
				break;

			case Author:
				for(Book i : BookList.values())
				{
					m = p.matcher(i.getAuthor());
					if(m.find())
						this.BookQuery.add(i);
				}
				break;

			case Name:
				for(Book i : BookList.values())
				{
					m = p.matcher(i.getName());
					if(m.find())
						this.BookQuery.add(i);
				}
				break;

			case ISBN:
				for(Book i : BookList.values())
				{
					m = p.matcher(i.getISBN());
					if(m.find())
						this.BookQuery.add(i);
				}
				break;

			default:
				// XXX write stuff and nag in case of invalid stuff
				break;
		}
		if(this.BookQuery.size() == 0)
			return false;
		else 
			return true;
	}
*/

	public boolean FindBook(BookAttr k, Pattern p)
	{
		Matcher m;
		this.BookQuery.clear();
		for(Book i: this.BookList.values())
		{
			m = p.matcher(i.toMap().get(k));
			if(m.find())
				BookQuery.add(i);
		}
		if(this.BookQuery.size() == 0)
			return false;
		else
			return true;
	}

	public boolean CheckAdmin(String a)
	{
			for(Admin i : this.AdminList.values())
			{
				if(i.getUsername().equals(a))
				{
					return true;
				}
			}
			return false;
	}

	public Admin MatchAdmin(String un)
	{
		Admin a = null;

		for(Admin i : this.AdminList.values())
		{
			if(i.getUsername().equals(un))
				a = i;
		}

		if(a == null)
			throw new NoSuchElementException("No such Admin exists.");
		return a;
	}

	public boolean FindAdmin(AdminAttr k, Pattern p)
	{
		Matcher m;
		this.AdminQuery.clear();
		for(Admin i: this.AdminList.values())
		{
			m = p.matcher(i.toMap().get(k));
			if(m.find())
				AdminQuery.add(i);
		}
		if(this.AdminQuery.size() == 0)
			return false;
		else
			return true;
	}

	public boolean CheckUser(String u)
	{
			for(User i : this.UserList.values())
			{
				if(i.getUsername().equals(u))
				{
					return true;
				}
			}
			return false;
	}

	public User MatchUser(String un)
	{
		User u = null;

		for(User i : this.UserList.values())
		{
			if(i.getUsername().equals(un))
				u = i;
		}

		if(u == null)
		{
			throw new NoSuchElementException("No such User exists.");
		}
		return u;
	}

	public boolean FindUser(UserAttr k, Pattern p)
	{
		Matcher m;
		this.UserQuery.clear();
		for(User i: this.UserList.values())
		{
			m = p.matcher(i.toMap().get(k));
			if(m.find())
				UserQuery.add(i);
		}
		if(this.UserQuery.size() == 0)
			return false;
		else
			return true;
	}

	public boolean FindBook(boolean q)
	{
		/* flush last query */
		this.BookQuery.clear();
		for(Book i : BookList.values())
		{
			if(i.getStatus())
				this.BookQuery.add(i);
		}
		if(this.BookQuery.size() == 0)
			return false;
		else 
			return true;
	}

	/* user/admin's ID, ISBN */
	public Err TakeBook(String un, String isbn)
	{
		Book b = this.BookList.get(isbn);
		if(this.CheckUser(un))
			return TakeBook(MatchUser(un), b);
		else if(this.CheckAdmin(un))
			return TakeBook(MatchAdmin(un), b);
		else
			return Err.NoUser;
	}

	/* they shouldn't be able to get a book, but still */
	public Err TakeBook(Admin u, Book b)
	{
		/* check if book is taken */
		if(b.getStatus())
			return Err.BookReserved;

		/* he has some book? blasmphy! */
		if(!u.getReserved().isEmpty())
			return Err.UserHasABook;

		/* check if ages match admin has no age anyway
		if(!b.AgeOk(u.getAge()))
			return Err.Age; */

		u.setReserved(b.getISBN());
		b.setOwner(u.getUsername());
		this.ResCount++;
		return Err.Ok;
	}

	public Err TakeBook(User u, Book b)
	{
		/* check if book is taken */
		if(b.getStatus())
			return Err.BookReserved;

		/* he has some book? blasmphy! */
		if(!u.getReserved().isEmpty())
			return Err.UserHasABook;

		/* check if ages match */
		if(!b.AgeOk(u.getAge()))
			return Err.Age;

		u.setReserved(b.getISBN());
		b.setOwner(u.getUsername());
		this.ResCount++;
		return Err.Ok;
	}

	public Err ReturnBook(Admin u)
	{
		Book b;
		if(u.getReserved().isEmpty())
			return Err.NoBookReserved;

		if(!CheckBook(u.getReserved()))
			return Err.NoBook;

		b = MatchBook(u.getReserved());

		u.setReserved(null);
		b.setOwner(null);
		this.ResCount--;
		return Err.Ok;
	}

	public Err ReturnBook(User u)
	{
		Book b;
		if(u.getReserved().isEmpty())
			return Err.NoBookReserved;

		if(!CheckBook(u.getReserved()))
			return Err.NoBook;

		b = MatchBook(u.getReserved());

		u.setReserved(null);
		b.setOwner(null);
		this.ResCount--;
		return Err.Ok;
	}

class conUI implements UI
{
	public void Program()
	{
		while(true)
		{
			/* there is no admin! */
			if(AdminList.size() == 0)
			{
				AddAdmin();
				adm.Active = true;
			}
			else
				this.LoginMenu();

			/* only exits if user either logs out or smh */
			this.Menu(adm);

			/* user wants to quit */
			if(adm.Active)
				return;
		}
	}

	public void LoginMenu()
	{
		String un, pass;

		System.out.print("Login> ");
		un = GetUsername();

		if(CheckAdmin(un))
		{	
			pass = this.GetPassword();

			Admin a = MatchAdmin(un);
			if(Login(a, pass))
			{
				System.out.println("Auth as " + a.getUsername() + " sucessful");
				return;
			}
			else
			{
				System.out.println("Auth fail");
				this.LoginMenu();
			}
		}
		else if(CheckUser(un))
		{
			User u = MatchUser(un);
			if(Login(u))
			{
				System.out.println("Login sucessful");
			}
		}
		else
		{
			/* fool em! */
			this.GetPassword();
			System.out.println("Auth fail");
			this.LoginMenu();
		}
	}

	/* checks if password is valid, kinda like login */
	public boolean AskPass(Admin u)
	{
		String pass;
		System.out.println("Enter " + u.getUsername() + "'s password:");
		return u.checkPass(GetPassword());
	}

	public void Menu(Admin a)
	{
		int in;
		Scanner s = new Scanner(System.in);
		System.out.println("Welcome, " + a.getUsername() + "!");
		while(true)
		{
			System.out.print(
			"\n" +
			" 1) add admin\n" +
			" 2) add user\n" +
			" 3) add book\n\n" +

			" 4) search admin\n" +
			" 5) search user\n" +
			" 6) search book\n\n" +

			" 7) list admin\n" +
			" 8) list user\n" +
			" 9) list book\n\n" +

			"10) delete admin\n" +
			"11) delete user\n" +
			"12) delete book\n\n" +

			"13) take book\n" +
			"14) return book\n\n" +

			"15) logout\n" +
			"99) quit!\n");

			System.out.print("menu> ");
			while(!s.hasNextInt())
			{
				System.err.println("Please enter a valid input");
				System.out.print("menu> ");
				s.nextLine();
			}
			in = s.nextInt();

			switch(in)
			{
				case 1:
					if(this.AskPass(adm))
						this.AddAdmin();
					else
						System.err.println("Incorrect Password.");
					break;
/*				case 2:
					if(this.AskPass(adm))
						this.AddUser();
					else
						System.err.println("Incorrect Password.");
					break;
*/
				case 3:
					this.AddBook();
					break;
/*
				case 4:
					this.SearchAdmin();
					break;
				case 5:
					this.SearchUser();
					break;
*/
				case 6:
					this.SearchBook();
					break;
				case 7:
					this.ListAdmin();
					break;
				case 8:
					this.ListUser();
					break;
				case 9:
					this.ListBook();
					break;

				case 10:
					// System.out.println("This action requires admin password");
					// if(this.AskPass(adm)) /* current admin */
					//	this.RemAdmin();
					// else
					//	System.err.println("Incorrect Password.");
					break;

				case 11:
					System.out.println("This action requires admin password");
					// if(this.AskPass(adm)) /* current admin */
					//	this.RemUser();
					// else
					//	System.err.println("Incorrect Password.");
					break;
				case 12:
					// this.RemBook();
					break;

				case 13:
					this.TakeMenu(adm);
					break;
				case 14:
					// this.ReturnMenu();
					break;

				case 15:
					System.out.println("Logging out...");
					Logout(adm);
					return;
				/* quit */
				case 99:
						System.out.print("Are you sure? (y/N)> ");
						s.nextLine();
						if(s.nextLine().equals("y"))
							return;
						break;
				default:
					System.out.println("Invalid answer!, please try again.");
					break;
			}
		}
	}

	public String GetUsername()
	{
		Scanner s = new Scanner(System.in);
		String user;

		do
		{
			System.out.print("Username?> ");
			user = s.nextLine();
		}while(user.contains(" "));

		return user;
	}

	public String GetPassword()
	{
		String pass;

		if(System.console() != null)
		{
			Console c = System.console();
			do
			{
				pass = new String(c.readPassword("Password?> "));
			}while(pass.contains(" "));
		}
		else
		{
			/* no console? */
			Scanner s = new Scanner(System.in);
			do
			{
				System.out.print("Password?> ");
				pass = s.nextLine();
			}while(pass.contains(" "));
		}

		/* encrypt it! */
		return Integer.toString(pass.hashCode());
	}


	public void SearchUser()
	{
		String attr, patt;
		Pattern pattern;
		UserAttr ua;
		Scanner s = new Scanner(System.in);

		System.out.println("Attr: Username, Reserved, ID, RegTime, Age, Active");
		System.out.print("Find> What?> ");
		attr = s.nextLine();

		if(attr.equalsIgnoreCase("Username"))
			ua = UserAttr.Username;
		else if(attr.equalsIgnoreCase("Reserved"))
			ua = UserAttr.Reserved;
		else if(attr.equalsIgnoreCase("ID"))
			ua = UserAttr.ID;
		else if(attr.equalsIgnoreCase("RegTime"))
			ua = UserAttr.RegTime;
		else if(attr.equalsIgnoreCase("Age"))
			ua = UserAttr.Age;
		else if(attr.equalsIgnoreCase("Active"))
			ua = UserAttr.Active;
		else
		{
			System.err.println("Invalid Attr");
			return;
		}

		System.out.println("Find> Pattern?> ");
		patt = s.nextLine();
		pattern = Pattern.compile(patt, Pattern.CASE_INSENSITIVE);


		if(FindUser(ua, pattern))
		{
			for(User i : UserList.values())
			{
				for(UserAttr j : UserAttr.values())
					System.out.print(j + " | ");
				System.out.println();

				for(User j : UserQuery)
				{
					for(UserAttr a : i.toMap().keySet())
						System.out.print(j.toMap().get(a) + " | ");
					System.out.println();
				}
			}
		}
		else
			System.err.println("No match has found");
	}

	public void SearchBook()
	{
		String attr, patt;
		Pattern pattern;
		BookAttr ba;
		Scanner s = new Scanner(System.in);

		System.out.println("Attr: Status, Name, Abstract, ISBN, AgeGroup etc.");
		System.out.print("Find> What?> ");
		attr = s.nextLine();

		if(attr.equalsIgnoreCase("Status"))
			ba = BookAttr.Status;
		else if(attr.equalsIgnoreCase("Name"))
			ba = BookAttr.Name;
		else if(attr.equalsIgnoreCase("Author"))
			ba = BookAttr.Author;
		else if(attr.equalsIgnoreCase("Abstract"))
			ba = BookAttr.Abstract;
		else if(attr.equalsIgnoreCase("ISBN"))
			ba = BookAttr.ISBN;
		else if(attr.equalsIgnoreCase("AgeGroup"))
			ba = BookAttr.AgeGroup;
		else
		{
			System.err.println("Invalid Attr");
			return;
		}

		System.out.println("Find> Pattern?> ");
		patt = s.nextLine();
		pattern = Pattern.compile(patt, Pattern.CASE_INSENSITIVE);

		if(FindBook(ba, pattern))
		{
			// XXX add isRegistered()
			for(Book i : BookList.values())
			{
				for(BookAttr j : BookAttr.values())
					System.out.print(j + " | ");
				System.out.println();

				for(Book j : BookQuery)
				{
					for(BookAttr a : j.toMap().keySet())
						System.out.print(j.toMap().get(a) + " | ");
					System.out.println();
				}
			}
		}
		else
			System.err.println("No match has found");
	}

	public void AddAdmin()
	{
		/* isn't java wonderful billy? */
		String user, pass;

		System.out.println("Setting up Admin:");

		do
		{
			System.out.print("Add admin> ");
			user = this.GetUsername();

			if(user.equals(""))
				return;
		}while(CheckAdmin(user));

		do
		{
			pass = this.GetPassword();
		}while(pass.equals(""));

		NewAdmin(user, pass);
	}

	/* make checks better, reject empty input etc,
		regex perhaps? */
	public void AddBook()
	{
		int agegroup;
		String name, isbn, author, abs;
		Scanner s = new Scanner(System.in);

		System.out.print("Book> Name?> ");
		name = s.nextLine();
		if(name.equals(""))
			return;

		System.out.print("Book> ISBN?> ");
		isbn = s.nextLine();
		if(isbn.equals(""))
			return;

		System.out.print("Book> Author?> ");
		author = s.nextLine();
		if(author.equals(""))
			return;

		// XXX on a related note, should i care about dealing with multiline stuff?
		System.out.print("Book> Abstract?> ");
		abs = s.nextLine();
		if(abs.equals(""))
			return;

		System.out.print("Book> Age Group?> ");
		while(!s.hasNextInt())
		{
			System.out.println("Please enter a valid input");
			System.out.print("Book> Age Group?> ");
			s.nextLine();
		}
		agegroup = s.nextInt();

		Book b = NewBook(isbn, name, author, agegroup, abs);
	}

	public void Rembook()
	{

	}

	public void ListAdmin()
	{
		for(AdminAttr i : AdminAttr.values())
			System.out.print(i + " | ");
		System.out.println();

		for(Admin i : AdminList.values())
		{
			for(AdminAttr a : i.toMap().keySet())
					System.out.print(i.toMap().get(a) + " | ");
			System.out.println();
		}
	}

	public void ListUser()
	{
		for(UserAttr i : UserAttr.values())
			System.out.print(i + " | ");
		System.out.println();

		for(User i : UserList.values())
		{
			for(UserAttr a : i.toMap().keySet())
					System.out.print(i.toMap().get(a) + " | ");
			System.out.println();
		}
	}

	public void ListBook()
	{
		for(BookAttr i : BookAttr.values())
			System.out.print(i + " | ");
		System.out.println();

		for(Book i : BookList.values())
		{
			for(BookAttr a : i.toMap().keySet())
					System.out.print(i.toMap().get(a) + " | ");
			System.out.println();
		}
	}

	public void TakeMenu(Admin u)
	{
		String isbn;
		Scanner s = new Scanner(System.in);

		if(!u.getReserved().isEmpty())
		{
			System.err.println("Current Admin has already has a book (ISBN: " + u.getReserved() + ").");
			return;
		}

		System.out.println("Which book do you want to take? (you may find it's ISBN by Search menu)");
		
		do
		{
			System.out.println("Take> ISBN?> ");
			isbn = s.nextLine();

			if(isbn.equals(""))
				return;

			if(!CheckBook(isbn))
				System.err.println("No such book found, please try again");

		}while(isbn.contains(" ") || !CheckBook(isbn));

		Result(TakeBook(u, MatchBook(isbn)));
	}

	public void TakeMenu(User u)
	{
		String isbn;
		Scanner s = new Scanner(System.in);

		if(!u.getReserved().isEmpty())
		{
			System.err.println("Current User has already has a book (ISBN: " + u.getReserved() + ").");
			return;
		}

		System.out.println("Which book do you want to take? (you may find it's ISBN by Search menu)");
		
		do
		{
			System.out.println("Take> ISBN?> ");
			isbn = s.nextLine();

			if(isbn.equals(""))
				return;

			if(!CheckBook(isbn))
				System.err.println("No such book found, please try again");

		}while(isbn.contains(" ") || !CheckBook(isbn));

		Result(TakeBook(u, MatchBook(isbn)));
	}

	public void Result(Err res)
	{
		switch(res)
		{
			case NoBook:
				System.err.println("No such book has found");
				break;
			case BookReserved:
				System.err.println("This book is reserved by someone else");
				break;
			case NoBookReserved:
				System.err.println("No book is reserved");
				break;
			case NoUser:
				System.err.println("No such User has found");
				break;
			case UserHasABook:
				System.err.println("User has already registered a book");
				break;
			case UserHasNoBook:
				System.err.println("User has no book");
				break;
			case Age:
				System.err.println("Age doesn't match!");
				break;
			case Ok:
			default:
				System.out.println("Done!");
				break;
		}
	}

} /* end of conUI */
}

public class Main
{
	public static void main(String args[])
	{
		Lib l = new Lib();
		Lib.UI ui = l.new conUI();
		ui.Program();
	}
}