wm: java

ref: c99a80f9d76d45e80691652af3a363eaecbd174f
dir: /Main.java/

View raw version
//package BookProj;

// XXX get rid of *s
// maybe deconstrctor for stuff would be a good idea
// delbook, takebook, returnbook

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


/* XXX
enum BAttr
{

}
*/

enum BookAttr
{
	Status,
	AgeGroup,
	Abstract,
	Author,
	Name,
	ISBN,
}

enum UserAttr
{

}

interface Library 
{
	/* portable? stuff  XXX
	int readin(Scanner s, String errmsg, String inputmsg);
	String readin(Scanner s, String errmsg, String inputmsg);
	String getpass(); sudo/su-like
	//void setup();
	*/

	/* book stuff
	String FindBook(String ISBN, String value); // Pointer to ISBN
	void ReturnBook(String ISBN);
	void ReturnBook(Book b);
	boolean WriteStatus();

	boolean FindBook(String k, Pattern p);
	boolean FindBook(boolean q);  for Status value searchs */

	/* objects */
	/* id, Admin/User, ISBN, Book */
	HashMap<String, Admin> AdminList = new HashMap<String, Admin>();
	HashMap<String, User> UserList = new HashMap<String, User>();
	HashMap<String, Book> BookList = new HashMap<String, Book>();

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

	/* admin */
	boolean Login(Admin a);
	void Logout(Admin a);

	Admin NewAdmin();
	void DelAdmin(Admin a);

	Admin GetAdmin(); // ID or Username
	boolean Askpass(Admin a);
	boolean CheckAdmin(String a); // XXX get rid of this with FindAdmin

	Admin FindAdmin(int ID);
	void ListAdmin();

	void Menu(Admin a);

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

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

	User GetUser(); // ID or Username
	boolean Askpass(User u);
	boolean CheckUser(int ID);
	boolean CheckUser(String u);

	User FindUser(int ID);
	void ListUser();

	void Menu(User u);

	/* books */
	boolean TakeBook(User u, Book b);
	boolean ReturnBook(User u);

	boolean NewBook();
	void DelBook(Book b);

	boolean CheckBook(String ISBN);
	void SearchBook(); // ISBN
	
	boolean SearchBook(BookAttr b);
	Book FindBook(int ISBN);
	void ListBook();

}


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

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

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

	boolean setOwner(int ID)
	{
		if(this.Owner != 0)
		{
			System.err.println("A User (ID:" + this.getOwner() + ") already got the book");
			return false;
		}
		this.Owner = ID;
		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;
	}

	int getOwner()
	{
		return this.Owner;
	}
}

abstract class BasicUser
{
	private Role role;
	private String Username, Reserved;
	private int ID;
	private LocalDateTime RegTime;
	// 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;
	public boolean Active = false;
	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(Book b)
	{
		return false; /* XXX */
	}

	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;
	}
}

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;
	}

	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;
	}
	
}

class Lib implements Library
{

	// XXX think more, make read.password.
	/*
	HashMap<Integer, Admin> AdminList = new HashMap<Integer, Admin>();
	HashMap<Integer, User> UserList = new HashMap<Integer, User>();
	HashMap<String, Book> BookList = new HashMap<String, Book>();
	*/

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

	static Admin adm;

	/* 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.out.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.out.println(errmsg);
			System.out.print(inputmsg);
			s.nextLine();
		}
		return s.nextLine();
	}

	public boolean Login(Admin u)
	{
			while(!Askpass(u))
			{		
				System.err.println("Please try again");
			}
			System.out.println("Login as " + u.getUsername() + " successful");
			u.Active = true;
			return true;
	}

	public boolean Login(User u)
	{
		/* maybe later i want to do more stuff here */
		System.out.println("Login as " + u.getUsername() + " successful");
		return true;
	}

	public void Logout(Admin u)
	{
		System.out.println("Logging out...");
		u.Active = false;
	}

	// XXX make boolan it you dummy, why?
	public Admin NewAdmin()
	{
		/* isn't java wonderful billy? */
		Admin a;
		String user = "", pass = "";
		Scanner s = new Scanner(System.in);
		System.out.println("Setting up Admin:");
		while(user.isEmpty() || user.contains(" "))
		{
			System.out.print("Admin username?> ");
			user = s.nextLine();
			for(Admin i : this.AdminList.values())
				if(i.getUsername().equals(user))
				{
					System.out.println("User name " + user + " already exist");
					user = ""; /* why?, you say, well jimmy, i say, we need to be in loop */
				}
		}

		while(pass.equals("") && !user.equals("admin"))
		{
			System.out.print("Admin password?> ");
			pass = s.nextLine();
		}
		/* if user is "admin", use defaults */
		if(user.equals("admin"))
			a = new Admin("admin", "pass");
		else
			a = new Admin(user, pass);
		this.AdminList.put(a.getUsername(), a);
		return a;
	}

	// XXX urgent WIP
	// deconstrcutor?
	public void DelAdmin()
	{
		Admin a;
		String user = "", pass = "";
		Scanner s = new Scanner(System.in);
		System.out.println("Deleting admin:");
		while(user.isEmpty() || user.contains(" "))
		{
			System.out.print("Admin username?> ");
			user = s.nextLine();
			for(Admin i : this.AdminList.values())
			{
				if(i.getUsername().equals(user))
				{
					a = i;
					user = "";
				}
			}
			/* we haven't found a match */
			if(!user.isEmpty())
				System.out.println("No such admin exist");
			//else if(this.adm.getUsername.equals(user))
		}
		//this.AdminList(
	}

	public void Menu(Admin a)
	{
		int in;
		Scanner s = new Scanner(System.in);
		System.out.println("Welcome, " + a.getUsername() + "!");
		while(true)
		{
			System.out.print(
			" 1) new book\n" +
			" 2) new user\n" +
			" 3) find book\n" +
			" 4) take book\n" +
			" 5) return book\n" +
			" 6) logout\n" +
			" 7) quit!\n" +
			" 8) add new admin\n" +
			" 9) delete admin\n" +
			"10) list admins\n" +
			"11) list books\n");

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

			switch(in)
			{
			
				case 1:
					this.NewBook();
					break;
				case 2:
					//this.adduser();
					System.err.println("XXX todo");
					break;
				case 3:
					this.SearchBook();
					break;
				case 4:
					//this.TakeBook();
					System.err.println("XXX todo");
					break;
				case 5:
					//this.ReturnBook();
					System.err.println("XXX todo");
					break;
				case 6:
					this.Logout(this.adm);
					return;
				case 7:
					return;
				case 8:
					System.out.println("This action requires admin password");
					if(this.Askpass(this.adm)) /* current admin */
						this.NewAdmin();
					else
						System.out.println("sorry.");
					break;
				case 9:
					System.out.println("This action requires admin password");
					if(this.Askpass(this.adm)) /* current admin */
						this.DelAdmin();
					else
						System.out.println("sorry.");
					break;
				case 10:
					this.ListAdmin();
					break;
				case 11:
					this.ListBook();
					break;
				default:
					System.out.println("Invalid answer!, please try again.");
					break;
			}
		}
	}

	/* make checks better, reject empty input etc,
		regex perhaps? */
	public boolean NewBook()
	{
		String name, isbn, author, abs;
		Scanner s = new Scanner(System.in);
		int agegroup;
		Book b;
		System.out.println("Book> Name?> ");
		name = s.nextLine();
		if(name.equals("q"))
			return false;
		System.out.println("Book> ISBN?> ");
		isbn = s.nextLine();
		if(isbn.equals("q"))
			return false;
		System.out.println("Book> Author?> ");
		author = s.nextLine();
		if(author.equals("q"))
			return false;
		// XXX on a related note, should i care about dealing with multiline stuff?
		System.out.println("Book> Abstract?> ");
		abs = s.nextLine();
		if(abs.equals("q"))
			return false;
		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();
		if(agegroup < -1)
			return false;
		b = new Book(name, author, abs, isbn, agegroup);
		System.out.println(this.BookList.put(isbn, b));
		return true;
	}
	public void ListBook()
	{
		System.out.println("total books registered: " + this.BookList.size());
		System.out.println("ISBN | Name | Author | Age Group");
		for(Book i : this.BookList.values())
			System.out.println(i.getISBN() + " | " + i.getName() + " | " + i.getAuthor() + " | " + i.getAgeGroup());
	}

	public boolean Askpass(Admin u) /* make it normal? */
	{
		String t;
		Scanner s = new Scanner(System.in);

		/* oh yeah, console and stuff
		if(System.console() != null)
			char passwd[], esc = 'q';
			// oh yeah, passwords and stuff
			if(passwd = cons.readPassword("[%s]", "Password:")) != null)
			{
				if(u.checkPass(passwd))
					t = true;
     			java.util.Arrays.fill(passwd, ' ');
			} 
		*/

			System.out.print("Password?> ");
			if(u.checkPass(s.nextLine()))
				return true;
			else
				return false;
	}

	public boolean CheckUser(int ID)
	{
		boolean b = false;

		for(User i : this.UserList.values())
		{
			if(i.getID() == ID)
				b = true;
		}

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

	public boolean CheckAdmin(int ID)
	{
		boolean b = false;

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

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

	public Admin FindAdmin(int ID)
	{
		Admin a = null;

		for(Admin i : this.AdminList.values())
		{
			if(i.getID() == ID)
				a = i;
		}

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

	public User FindUser(int ID)
	{
		User u = null;

		for(User i : this.UserList.values())
		{
			if(i.getID() == ID)
				u = i;
		}

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

	public Admin GetAdmin()
	{
		Scanner s = new Scanner(System.in);
		String user = "";

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

			/* see if it's used */
			for(Admin i : this.AdminList.values())
			{
				if(i.getUsername().equals(user))
				{
					return i;
				}
			}
			/* didn't found any, try again */
			System.err.println("No such admin found, try again");
			user = "";
		}
	}

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

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


	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;
	}

	public boolean FindBook(String k, Pattern p)
	{
		/* flush last query */
		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 void SearchBook()
	{
		String what, patt;
		Pattern p;
		Scanner s = new Scanner(System.in);
		System.out.println("What: Status, Name, Abstract, ISBN, oISBN, AgeGroup etc.");
		System.out.println("Find> What?> ");
		what = s.nextLine();
		System.out.println("Find> Pattern?> ");
		patt = s.nextLine();
		p = Pattern.compile(patt, Pattern.CASE_INSENSITIVE);
		if(FindBook(what, p))
		{
			// XXX add isRegistered()
			System.out.println("ISBN | Name | Author | Age Group");
			for(Book i : BookList.values())
			{
				System.out.println(i.getISBN() + " | " + i.getName() + " | " + i.getAuthor() + " | " + i.getAgeGroup());
			}
		}
		else
			System.out.println("No match has found");
	}

	public void ListUsers()
	{
		System.out.println("Username | ID | Reserved ISBN");
		for(User i : this.UserList.values())
			System.out.println(i.getRole() + " | " + i.getUsername() + " | " + i.getID() + " | " + i.getReserved());
	}

	public void ListAdmin()
	{
		System.out.println("Username | ID | Active | RegTime");
		for(Admin i : this.AdminList.values())
			
			System.out.println(i.getUsername() + " | " + i.getID() + " | " + i.Active + " | " + i.getRegTime());
	}

	/* they shouldn't be able to get a book, but still */
	public boolean TakeBook(Admin u, Book b)
	{
		/* check if book is taken */
		if(b.getStatus())
		{
			System.err.println("Given book (ISBN: " + b.getISBN() + ") has been reserved.");
			return false;
		}
		/* he has some book? blasmphy! */
		if(!u.getReserved().isEmpty())
		{
			System.err.println("User (ID: " + u.getID() + ") already has a book (" + u.getReserved() + ").");
			return false;
		}
		u.setReserved(b.getISBN());
		b.setOwner(u.getID());
		return true;
	}

	public boolean TakeBook(User u, Book b)
	{
		/* check if book is taken */
		if(b.getStatus())
		{
			System.err.println("Given book (ISBN: " + b.getISBN() + ") has been reserved.");
			return false;
		}
		/* he has some book? blasmphy! */
		if(!u.getReserved().isEmpty())
		{
			System.err.println("User (ID: " + u.getID() + ") already has a book (" + u.getReserved() + ").");
			return false;
		}
		u.setReserved(b.getISBN());
		b.setOwner(u.getID());
		return true;
	}

	/* user/admin's ID, ISBN */
/*	public boolean TakeBook(int ID, String isbn)
	{
		if(this.CheckUser(ID))
		{
			User u = FindUser(ID);
		}
		else if(this.CheckAdmin(ID))
		{
			Admin u = FindAdmin(ID);
		}
		else
		{
			System.err.println("No such User or Admin has found");
			return false;
		}

		Book b = this.BookList.get(isbn);

		return this.TakeBook(u, b);
		
	}
*/
}

public class Main
{
	public static void main(String args[])
	{
		int in;
		Scanner s = new Scanner(System.in);
		// Scanner sf = New Scanner(file)? XXX

		Lib l = new Lib();

		while(true)
		{
			/* there is no admin! */
			if(l.AdminList.size() == 0)
			{
				l.adm = l.NewAdmin();
				l.adm.Active = true;
			}
			else
				l.Login(l.GetAdmin());
			while(l.adm.Active)
			{
				/* only exits if user either logs out or smh */
				l.Menu(l.adm);
				/* user wants to quit */
				if(l.adm.Active)
					return;
			}
		}
	}
}