wm: java

Download patch

ref: 300ffb5637b1484d96d27d05a9056f09018b787d
parent: c99a80f9d76d45e80691652af3a363eaecbd174f
author: mkf <mkf@d510>
date: Wed May 24 15:58:23 EDT 2023

refactor

--- a/Main.java
+++ b/Main.java
@@ -14,48 +14,55 @@
 import java.util.regex.*;
 
 
-/* XXX
-enum BAttr
+enum Role
 {
-
+	Admin,
+	User,
 }
-*/
-
 enum BookAttr
 {
+	ISBN,
 	Status,
 	AgeGroup,
 	Abstract,
 	Author,
 	Name,
-	ISBN,
+	Owner;
 }
 
 enum UserAttr
 {
+	Username,
+	role,
+	Reserved,
+	ID,
+	RegTime,
+	Age;
+	
+}
 
+enum AdminAttr
+{
+	Username,
+	role,
+	Reserved,
+	ID,
+	RegTime,
+	Password,
+	Active;
 }
 
 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 */
+	/* Username, 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>();
@@ -71,46 +78,86 @@
 	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);
+	// boolean Login(User u);
+	// void Logout(User u);
 
-	User NewUser();
-	void DelUser(User u);
+	// User NewUser();
+	// void DelUser(User u);
 
-	User GetUser(); // ID or Username
-	boolean Askpass(User u);
-	boolean CheckUser(int ID);
+	// User GetUser(); // Scanner ID or Username
 	boolean CheckUser(String u);
 
 	User FindUser(int ID);
-	void ListUser();
 
-	void Menu(User u);
+	/* 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 */
+
 	/* books */
-	boolean TakeBook(User u, Book b);
-	boolean ReturnBook(User u);
+	void TakeBook(String un, String isbn);
+	void TakeBook(Admin u, Book b);
+	void TakeBook(User u, Book b);
+	void ReturnBook(User u);
+	void ReturnBook(Admin u);
 
 	boolean NewBook();
 	void DelBook(Book b);
 
 	boolean CheckBook(String ISBN);
-	void SearchBook(); // ISBN
 	
-	boolean SearchBook(BookAttr b);
-	Book FindBook(int ISBN);
+	boolean FindBook(BookAttr k, Pattern q);
 	void ListBook();
 
+	interface UI
+	{
+		void LoginMenu();
+
+		boolean AskPass(Admin u);
+		boolean AskPass(User u);
+
+		/* different Menus, for different UIs to handle */
+		void Menu(Admin u);
+		void Menu(User u);
+
+		/* generic */
+		String GetUsername();
+		String GetPassword();
+		
+		/* finds what we are looking for */
+		Admin GetAdmin();
+		User GetUser();
+		Book GetBook();
+
+		/* 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();
+	}
 }
 
 
@@ -119,7 +166,7 @@
 	/* 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 String Owner = ""; /* Username of user who reserved it */
 	private int ageGroup;
 	/* should a register be a constractor? XXX
 		 should it even named register? */
@@ -142,14 +189,14 @@
 			return false;
 	}
 
-	boolean setOwner(int ID)
+	boolean setOwner(String u)
 	{
-		if(this.Owner != 0)
+		if(!this.Owner.isEmpty())
 		{
 			System.err.println("A User (ID:" + this.getOwner() + ") already got the book");
 			return false;
 		}
-		this.Owner = ID;
+		this.Owner = u;
 		return true;
 	}
 
@@ -194,7 +241,7 @@
 		return this.ageGroup;
 	}
 
-	int getOwner()
+	String getOwner()
 	{
 		return this.Owner;
 	}
@@ -284,11 +331,6 @@
 		this.setPassword(Pass);
 	}
 
-	boolean setReserved(Book b)
-	{
-		return false; /* XXX */
-	}
-
 	boolean setReserved(String ISBN)
 	{
 		return false; /* XXX */
@@ -340,18 +382,6 @@
 
 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 */
@@ -437,34 +467,29 @@
 		return a;
 	}
 
-	// XXX urgent WIP
-	// deconstrcutor?
-	public void DelAdmin()
+	/* i could've made them overload,
+		however, this might led to unwanted results */
+	public void DelAdmin(Admin u)
 	{
-		Admin a;
-		String user = "", pass = "";
-		Scanner s = new Scanner(System.in);
-		System.out.println("Deleting admin:");
-		while(user.isEmpty() || user.contains(" "))
+		if(!u.getReserved().isEmpty())
 		{
-			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))
+			System.err.println("Admin " + u.getUsername() + " has a reserved book.");
+			return;
 		}
-		//this.AdminList(
+		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 void Menu(Admin a)
 	{
 		int in;
@@ -508,12 +533,10 @@
 					this.SearchBook();
 					break;
 				case 4:
-					//this.TakeBook();
-					System.err.println("XXX todo");
+				//	this.TakeBook();
 					break;
 				case 5:
-					//this.ReturnBook();
-					System.err.println("XXX todo");
+				//	this.ReturnBook();
 					break;
 				case 6:
 					this.Logout(this.adm);
@@ -529,10 +552,10 @@
 					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.");
+					// if(this.Askpass(this.adm)) /* current admin */
+					//	this.DelAdmin();
+					// else
+					//	System.out.println("sorry.");
 					break;
 				case 10:
 					this.ListAdmin();
@@ -586,6 +609,17 @@
 		System.out.println(this.BookList.put(isbn, b));
 		return true;
 	}
+
+	public void DelBook(Book b)
+	{
+		if(!b.getOwner().isEmpty())
+		{
+			System.err.println("User " + b.getOwner() + " have reserved this book.");
+			return;
+		}
+		this.BookList.remove(b.getISBN());
+	}
+
 	public void ListBook()
 	{
 		System.out.println("total books registered: " + this.BookList.size());
@@ -618,13 +652,13 @@
 				return false;
 	}
 
-	public boolean CheckUser(int ID)
+	public boolean CheckBook(String isbn)
 	{
 		boolean b = false;
 
-		for(User i : this.UserList.values())
+		for(Book i : this.BookList.values())
 		{
-			if(i.getID() == ID)
+			if(i.getISBN().equals(isbn))
 				b = true;
 		}
 
@@ -635,6 +669,7 @@
 		return b;
 	}
 
+
 	public boolean CheckAdmin(int ID)
 	{
 		boolean b = false;
@@ -681,8 +716,43 @@
 
 		if(u == null)
 		{
+			throw new NoSuchElementException("No such User exists.");
+		}
+		return u;
+	}
+
+	public Admin FindAdmin(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 User FindUser(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;
 	}
 
@@ -753,7 +823,7 @@
 			return true;
 	}
 
-	public boolean FindBook(String k, Pattern p)
+	public boolean FindBook(BookAttr k, Pattern p)
 	{
 		/* flush last query */
 		this.BookQuery.clear();
@@ -761,7 +831,7 @@
 		switch(k)
 		{
 			/* XXX should i use i.getFoolan.contains()? */
-			case "AgeGroup":
+			case AgeGroup:
 				for(Book i : BookList.values())
 				{
 					m = p.matcher(Integer.toString(i.getAgeGroup()));
@@ -770,7 +840,7 @@
 				}
 				break;
 
-			case "Abstract":
+			case Abstract:
 				for(Book i : BookList.values())
 				{
 					m = p.matcher(i.getAbstract());
@@ -779,7 +849,7 @@
 				}
 				break;
 
-			case "Author":
+			case Author:
 				for(Book i : BookList.values())
 				{
 					m = p.matcher(i.getAuthor());
@@ -788,7 +858,7 @@
 				}
 				break;
 
-			case "Name":
+			case Name:
 				for(Book i : BookList.values())
 				{
 					m = p.matcher(i.getName());
@@ -797,7 +867,7 @@
 				}
 				break;
 
-			case "ISBN":
+			case ISBN:
 				for(Book i : BookList.values())
 				{
 					m = p.matcher(i.getISBN());
@@ -818,22 +888,45 @@
 
 	public void SearchBook()
 	{
-		String what, patt;
-		Pattern p;
+		String attr, patt;
+		Pattern pattern;
+		BookAttr ba;
 		Scanner s = new Scanner(System.in);
-		System.out.println("What: Status, Name, Abstract, ISBN, oISBN, AgeGroup etc.");
+		System.out.println("Attr: Status, Name, Abstract, ISBN, oISBN, AgeGroup etc.");
 		System.out.println("Find> What?> ");
-		what = s.nextLine();
+		attr = s.nextLine();
 		System.out.println("Find> Pattern?> ");
 		patt = s.nextLine();
-		p = Pattern.compile(patt, Pattern.CASE_INSENSITIVE);
-		if(FindBook(what, p))
+		pattern = Pattern.compile(patt, Pattern.CASE_INSENSITIVE);
+		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("oISBN"))
+		//	FindBook(BookAttr., p);
+		else if(attr.equalsIgnoreCase("AgeGroup"))
+			ba = BookAttr.AgeGroup;
+		else
 		{
+			System.err.println("Invalid Attr");
+			return;
+		}
+		if(FindBook(ba, pattern))
+		{
 			// 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());
+				System.out.println(i.getISBN() +
+				" | " + i.getName() +
+				" | " + i.getAuthor() +
+				" | " + i.getAgeGroup());
 			}
 		}
 		else
@@ -844,7 +937,10 @@
 	{
 		System.out.println("Username | ID | Reserved ISBN");
 		for(User i : this.UserList.values())
-			System.out.println(i.getRole() + " | " + i.getUsername() + " | " + i.getID() + " | " + i.getReserved());
+			System.out.println(i.getRole() +
+			" | " + i.getUsername() +
+			" | " + i.getID() +
+			" | " + i.getReserved());
 	}
 
 	public void ListAdmin()
@@ -852,71 +948,105 @@
 		System.out.println("Username | ID | Active | RegTime");
 		for(Admin i : this.AdminList.values())
 			
-			System.out.println(i.getUsername() + " | " + i.getID() + " | " + i.Active + " | " + i.getRegTime());
+			System.out.println(i.getUsername() + 
+			" | " + i.getID() +
+			" | " + i.Active +
+			" | " + i.getRegTime());
 	}
 
+	/* user/admin's ID, ISBN */
+	public void TakeBook(String un, String isbn)
+	{
+		Book b = this.BookList.get(isbn);
+		if(this.CheckUser(un))
+			TakeBook(FindUser(un), b);
+		else if(this.CheckAdmin(un))
+			TakeBook(FindAdmin(un), b);
+		else
+			System.err.println("No such User or Admin " + un + " has found");
+	}
+
 	/* they shouldn't be able to get a book, but still */
-	public boolean TakeBook(Admin u, Book b)
+	public void 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;
+			return;
 		}
 		/* he has some book? blasmphy! */
 		if(!u.getReserved().isEmpty())
 		{
-			System.err.println("User (ID: " + u.getID() + ") already has a book (" + u.getReserved() + ").");
-			return false;
+			System.err.println("User (ID: " + u.getUsername() + 
+			") already has a book (" + u.getReserved() + ").");
+			return;
 		}
 		u.setReserved(b.getISBN());
-		b.setOwner(u.getID());
-		return true;
+		b.setOwner(u.getUsername());
 	}
 
-	public boolean TakeBook(User u, Book b)
+	public void 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;
+			return;
 		}
 		/* he has some book? blasmphy! */
 		if(!u.getReserved().isEmpty())
 		{
 			System.err.println("User (ID: " + u.getID() + ") already has a book (" + u.getReserved() + ").");
-			return false;
+			return;
 		}
 		u.setReserved(b.getISBN());
-		b.setOwner(u.getID());
-		return true;
+		b.setOwner(u.getUsername());
 	}
 
-	/* user/admin's ID, ISBN */
-/*	public boolean TakeBook(int ID, String isbn)
+	public void ReturnBook(Admin u)
 	{
-		if(this.CheckUser(ID))
+		Book b = null;
+		if(u.getReserved().isEmpty())
 		{
-			User u = FindUser(ID);
+			System.err.println("Admin " + u.getUsername() + " has no book reserved");
+			return;
 		}
-		else if(this.CheckAdmin(ID))
+		for(String i : this.BookList.keySet())
 		{
-			Admin u = FindAdmin(ID);
+			if(u.getReserved().equals(i))
+			{
+				b = this.BookList.get(i);
+				break;
+			}
 		}
-		else
+		if(b == null)
+			throw new NoSuchElementException("No such book exists.");
+		u.setReserved(null);
+		b.setOwner(null);
+	}
+
+	public void ReturnBook(User u)
+	{
+		Book b = null;
+		if(u.getReserved().isEmpty())
 		{
-			System.err.println("No such User or Admin has found");
-			return false;
+			System.err.println("User " + u.getUsername() + " has no book reserved");
+			return;
 		}
-
-		Book b = this.BookList.get(isbn);
-
-		return this.TakeBook(u, b);
-		
+		for(String i : this.BookList.keySet())
+		{
+			if(u.getReserved().equals(i))
+			{
+				b = this.BookList.get(i);
+				break;
+			}
+		}
+		if(b == null)
+			throw new NoSuchElementException("No such book exists.");
+		u.setReserved(null);
+		b.setOwner(null);
 	}
-*/
 }
 
 public class Main