wm: java

Download patch

ref: b0e7cab307f99283490c5fd1d3e7d085492c4155
parent: 0d5aae7a298bb7b4307721554ba07fb1712b18d8
author: mkf <mkf@d510>
date: Sat May 27 18:35:08 EDT 2023

use TreeMap instead of HashMap, as it gives us sorted values, get rid of ugly search, list methods, impl. Search user, fix a bug with NewBook which values were not in order

--- a/Main.java
+++ b/Main.java
@@ -72,8 +72,6 @@
 	Active;
 }
 
-
-
 interface Library 
 {
 	/* admin */
@@ -106,7 +104,7 @@
 	Err ReturnBook(User u);
 	Err ReturnBook(Admin u);
 
-	Book NewBook(String isbn, String name, String author, String abst, int agegroup);
+	Book NewBook(String isbn, String name, String author, int agegroup, String abst);
 	void DelBook(Book b);
 
 	boolean CheckBook(String ISBN);
@@ -144,7 +142,7 @@
 
 		/* search foolan menu */
 		// void SearchAdmin();
-		// void SearchUser();
+		void SearchUser();
 		void SearchBook();
 
 		/* add foolan menu */
@@ -179,9 +177,9 @@
 	HashMap<BookAttr, String> Book = new HashMap<BookAttr, String>();
 	HashMap<String, HashMap> Booklist = new HashMap<String, HashMap>();
 
-	HashMap<String, HashMap> Mkbl(HashMap<String, Book> bl)
+	HashMap<String, TreeMap> Mkbl(HashMap<String, Book> bl)
 	{
-		HashMap<String, HashMap> list = new HashMap<String, HashMap>();
+		HashMap<String, TreeMap> list = new HashMap<String, TreeMap>();
 		for(Book i: bl.values())
 		{
 			list.put(i.getISBN(), i.toMap());
@@ -200,14 +198,14 @@
 	/* should a register be a constractor? XXX
 		 should it even named register? */
 
-	Book(String name, String author, String abs, String i, int age)
+	Book(String isbn, String name, String author, int age, String abs)
 	{
 		/* joy of joys willy */
-		this.Abstract = abs;
-		this.Author = author;
+		this.ISBN = isbn;
 		this.Name = name;
-		this.ISBN = i;
+		this.Author = author;
 		this.AgeGroup = age;
+		this.Abstract = abs;
 	}
 
 	boolean AgeOk(int age)
@@ -275,9 +273,9 @@
 		return this.Owner;
 	}
 
-	HashMap<BookAttr, String> toMap()
+	TreeMap<BookAttr, String> toMap()
 	{
-		HashMap<BookAttr, String> temp = new HashMap<BookAttr, String>();
+		TreeMap<BookAttr, String> temp = new TreeMap<BookAttr, String>();
 
 		temp.put(BookAttr.ISBN, this.ISBN);
 		temp.put(BookAttr.Status, Boolean.toString(this.Status));
@@ -396,9 +394,9 @@
 		this.Password = pass;
 	}
 
-	HashMap<AdminAttr, String> toMap()
+	TreeMap<AdminAttr, String> toMap()
 	{
-		HashMap<AdminAttr, String> temp = new HashMap<AdminAttr, String>();
+		TreeMap<AdminAttr, String> temp = new TreeMap<AdminAttr, String>();
 
 		temp.put(AdminAttr.Username, getUsername());
 		temp.put(AdminAttr.role, getRole().toString());
@@ -441,9 +439,9 @@
 			return false;
 	}
 
-	HashMap<UserAttr, String> toMap()
+	TreeMap<UserAttr, String> toMap()
 	{
-		HashMap<UserAttr, String> temp = new HashMap<UserAttr, String>();
+		TreeMap<UserAttr, String> temp = new TreeMap<UserAttr, String>();
 
 		temp.put(UserAttr.Username, this.getUsername());
 		temp.put(UserAttr.role, getRole().toString());
@@ -585,18 +583,15 @@
 		this.UserList.remove(u.getUsername(), u);
 	}
 
-	/* should there be checks */
-	public Book NewBook(String isbn, String name, String author, String abst, int agegroup)
+	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(name, author, abst, isbn, agegroup);
+		Book b = new Book(isbn, name, author, agegroup,  abst);
 		this.BookList.put(isbn, b);
 		return b;
 	}
@@ -643,15 +638,15 @@
 
 		return b;
 	}
-
+/*
 	public boolean FindBook(BookAttr k, Pattern p)
 	{
-		/* flush last query */
+		//
 		this.BookQuery.clear();
 		Matcher m;
 		switch(k)
 		{
-			/* XXX should i use i.getFoolan.contains()? */
+			// XXX should i use i.getFoolan.contains()?
 			case AgeGroup:
 				for(Book i : BookList.values())
 				{
@@ -706,7 +701,8 @@
 		else 
 			return true;
 	}
-/*
+*/
+
 	public boolean FindBook(BookAttr k, Pattern p)
 	{
 		Matcher m;
@@ -722,7 +718,6 @@
 		else
 			return true;
 	}
-*/
 
 	public boolean CheckAdmin(String a)
 	{
@@ -1051,10 +1046,10 @@
 				case 5:
 					this.SearchUser();
 					break;
+*/
 				case 6:
-					this.SearchUser();
+					this.SearchBook();
 					break;
-*/
 				case 7:
 					this.ListAdmin();
 					break;
@@ -1151,18 +1146,71 @@
 	}
 
 
-	public void SearchBook()
+	public void SearchUser()
 	{
 		String attr, patt;
 		Pattern pattern;
-		BookAttr ba;
+		UserAttr ua;
 		Scanner s = new Scanner(System.in);
-		System.out.println("Attr: Status, Name, Abstract, ISBN, oISBN, AgeGroup etc.");
-		System.out.println("Find> What?> ");
+
+		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"))
@@ -1173,8 +1221,6 @@
 			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
@@ -1182,20 +1228,30 @@
 			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()
-			System.out.println("ISBN | Name | Author | Age Group");
 			for(Book i : BookList.values())
 			{
-				System.out.println(i.getISBN() +
-				" | " + i.getName() +
-				" | " + i.getAuthor() +
-				" | " + i.getAgeGroup());
+				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.out.println("No match has found");
+			System.err.println("No match has found");
 	}
 
 	public void AddAdmin()
@@ -1230,23 +1286,23 @@
 		String name, isbn, author, abs;
 		Scanner s = new Scanner(System.in);
 
-		System.out.println("Book> Name?> ");
+		System.out.print("Book> Name?> ");
 		name = s.nextLine();
 		if(name.equals(""))
 			return;
 
-		System.out.println("Book> ISBN?> ");
+		System.out.print("Book> ISBN?> ");
 		isbn = s.nextLine();
 		if(isbn.equals(""))
 			return;
 
-		System.out.println("Book> Author?> ");
+		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.println("Book> Abstract?> ");
+		System.out.print("Book> Abstract?> ");
 		abs = s.nextLine();
 		if(abs.equals(""))
 			return;
@@ -1260,7 +1316,7 @@
 		}
 		agegroup = s.nextInt();
 
-		Book b = NewBook(name, author, abs, isbn, agegroup);
+		Book b = NewBook(isbn, name, author, agegroup, abs);
 	}
 
 	public void Rembook()
@@ -1270,7 +1326,6 @@
 
 	public void ListAdmin()
 	{
-		// System.out.println("Username | role | Reserved | ID | RegTime | Active | ");
 		for(AdminAttr i : AdminAttr.values())
 			System.out.print(i + " | ");
 		System.out.println();
@@ -1285,20 +1340,30 @@
 
 	public void ListUser()
 	{
-		System.out.println("Username | ID | Reserved ISBN");
+		for(UserAttr i : UserAttr.values())
+			System.out.print(i + " | ");
+		System.out.println();
+
 		for(User i : UserList.values())
-			System.out.println(i.getRole() +
-			" | " + i.getUsername() +
-			" | " + i.getID() +
-			" | " + i.getReserved());
+		{
+			for(UserAttr a : i.toMap().keySet())
+					System.out.print(i.toMap().get(a) + " | ");
+			System.out.println();
+		}
 	}
 
 	public void ListBook()
 	{
-		System.out.println("total books registered: " + BookList.size());
-		System.out.println("ISBN | Name | Author | Age Group");
+		for(BookAttr i : BookAttr.values())
+			System.out.print(i + " | ");
+		System.out.println();
+
 		for(Book i : BookList.values())
-			System.out.println(i.getISBN() + " | " + i.getName() + " | " + i.getAuthor() + " | " + i.getAgeGroup());
+		{
+			for(BookAttr a : i.toMap().keySet())
+					System.out.print(i.toMap().get(a) + " | ");
+			System.out.println();
+		}
 	}
 
 	public void TakeMenu(Admin u)