wm: java

Download patch

ref: 1c0f4ee56ccae8da705f3259c1d6376bd989d183
parent: 91ed60928a248aa4f0007b39eae3d4e6f6d7f2f5
author: mkf <mkf@d510>
date: Fri May 26 17:34:53 EDT 2023

fix a lots of stuff

--- a/Main.java
+++ b/Main.java
@@ -17,6 +17,20 @@
 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,
@@ -81,12 +95,12 @@
 	boolean FindUser(UserAttr k, Pattern p);
 
 	/* books */
-	void TakeBook(String un, String isbn);
-	void TakeBook(Admin u, Book b);
-	void TakeBook(User u, Book b);
+	Err TakeBook(String un, String isbn);
+	Err TakeBook(Admin u, Book b);
+	Err TakeBook(User u, Book b);
 
-	void ReturnBook(User u);
-	void ReturnBook(Admin u);
+	Err ReturnBook(User u);
+	Err ReturnBook(Admin u);
 
 	Book NewBook(String isbn, String name, String author, String abst, int agegroup);
 	void DelBook(Book b);
@@ -96,7 +110,7 @@
 	boolean FindBook(BookAttr k, Pattern p);
 
 	/*
-	void ReturnBook(Book b);
+	Err ReturnBook(Book b);
 	boolean WriteStatus();
 	boolean FindBook(boolean q);  for Status value searchs 
 	*/
@@ -140,9 +154,18 @@
 		// void RemBook(); // maybe it's stolen
 
 		/* list foolan menu */
-		// void ListAdmin();
-		// void ListUser();
-		// void ListBook();
+		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);
 	}
 }
 
@@ -393,6 +416,10 @@
 		super(Role.User, username, (int)(Math.random()*1000+101), LocalDateTime.now());
 		this.Age = age;
 	}
+	int getAge()
+	{
+		return this.Age;
+	}
 
 	boolean IsReserved(Book b)
 	{
@@ -429,7 +456,6 @@
 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>();
@@ -442,6 +468,7 @@
 	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)
 	{
@@ -524,6 +551,10 @@
 			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;
 	}
@@ -793,101 +824,90 @@
 	}
 
 	/* user/admin's ID, ISBN */
-	public void TakeBook(String un, String isbn)
+	public Err TakeBook(String un, String isbn)
 	{
 		Book b = this.BookList.get(isbn);
 		if(this.CheckUser(un))
-			TakeBook(MatchUser(un), b);
+			return TakeBook(MatchUser(un), b);
 		else if(this.CheckAdmin(un))
-			TakeBook(MatchAdmin(un), b);
+			return TakeBook(MatchAdmin(un), b);
 		else
-			System.err.println("No such User or Admin " + un + " has found");
+			return Err.NoUser;
 	}
 
 	/* they shouldn't be able to get a book, but still */
-	public void TakeBook(Admin u, Book b)
+	public Err 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;
-		}
+			return Err.BookReserved;
+
 		/* he has some book? blasmphy! */
 		if(!u.getReserved().isEmpty())
-		{
-			System.err.println("User (ID: " + u.getUsername() + 
-			") already has a book (" + u.getReserved() + ").");
-			return;
-		}
+			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 void TakeBook(User u, Book b)
+	public Err 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;
-		}
+			return Err.BookReserved;
+
 		/* he has some book? blasmphy! */
 		if(!u.getReserved().isEmpty())
-		{
-			System.err.println("User (User name: " + u.getUsername() + ") already has a book (" + u.getReserved() + ").");
-			return;
-		}
+			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 void ReturnBook(Admin u)
+	public Err ReturnBook(Admin u)
 	{
-		Book b = null;
+		Book b;
 		if(u.getReserved().isEmpty())
-		{
-			System.err.println("Admin " + u.getUsername() + " has no book reserved");
-			return;
-		}
-		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.");
+			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 void ReturnBook(User u)
+	public Err ReturnBook(User u)
 	{
-		Book b = null;
+		Book b;
 		if(u.getReserved().isEmpty())
-		{
-			System.err.println("User " + u.getUsername() + " has no book reserved");
-			return;
-		}
-		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.");
+			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
@@ -904,28 +924,25 @@
 			}
 			else
 				this.LoginMenu();
-			while(adm.Active || usr.Active)
-			{
-				/* only exits if user either logs out or smh */
-				this.Menu(adm);
-				/* user wants to quit */
-				if(adm.Active)
-					return;
-			}
+
+			/* only exits if user either logs out or smh */
+			this.Menu(adm);
+
+			/* user wants to quit */
+			if(adm.Active)
+				return;
 		}
 	}
 
 	public void LoginMenu()
 	{
-		Scanner s = new Scanner(System.in);
 		String un, pass;
 
-		System.out.println("Login> Username?> ");
-		un = s.nextLine();
+		System.out.print("Login> ");
+		un = GetUsername();
 
 		if(CheckAdmin(un))
-		{
-			System.out.println("Login> Pass?> ");	
+		{	
 			pass = this.GetPassword();
 
 			Admin a = MatchAdmin(un);
@@ -937,7 +954,7 @@
 			else
 			{
 				System.out.println("Auth fail");
-				return;
+				this.LoginMenu();
 			}
 		}
 		else if(CheckUser(un))
@@ -948,12 +965,21 @@
 				System.out.println("Login sucessful");
 			}
 		}
+		else
+		{
+			/* fool em! */
+			this.GetPassword();
+			System.out.println("Auth fail");
+			this.LoginMenu();
+		}
 	}
 
-	/* checks if password is valid, kinda like passwd */
-	public boolean Askpass(Admin u)
+	/* checks if password is valid, kinda like login */
+	public boolean AskPass(Admin u)
 	{
-		return true;
+		String pass;
+		System.out.println("Enter " + u.getUsername() + "'s password:");
+		return u.checkPass(GetPassword());
 	}
 
 	public void Menu(Admin a)
@@ -964,18 +990,29 @@
 		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");
+			"\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())
 			{
@@ -987,52 +1024,76 @@
 
 			switch(in)
 			{
-			
 				case 1:
-					this.AddBook();
+					if(this.AskPass(adm))
+						this.AddAdmin();
+					else
+						System.err.println("Incorrect Password.");
 					break;
-				case 2:
-					//this.AddUserr();
-					System.err.println("XXX todo");
+/*				case 2:
+					if(this.AskPass(adm))
+						this.AddUser();
+					else
+						System.err.println("Incorrect Password.");
 					break;
+*/
 				case 3:
-					this.SearchBook();
+					this.AddBook();
 					break;
+/*
 				case 4:
-				//	this.TakeBook();
+					this.SearchAdmin();
 					break;
 				case 5:
-				//	this.ReturnBook();
+					this.SearchUser();
 					break;
 				case 6:
-					System.out.println("Logging out...");
-					Logout(adm);
-					return;
+					this.SearchUser();
+					break;
+*/
 				case 7:
-					return;
+					this.ListAdmin();
+					break;
 				case 8:
-					System.out.println("This action requires admin password");
-					if(this.Askpass(adm)) /* current admin */
-						this.AddAdmin();
-					else
-						System.err.println("Incorrect Password.");
+					this.ListUser();
 					break;
 				case 9:
-					System.out.println("This action requires admin password");
-					// if(this.Askpass(this.adm)) /* current admin */
-					//	this.DelAdmin();
+					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 10:
-					this.ListAdmin();
-					break;
+
 				case 11:
-					this.ListUser();
+					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.ListBook();
+					// 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:
+						return;
 				default:
 					System.out.println("Invalid answer!, please try again.");
 					break;
@@ -1047,7 +1108,7 @@
 
 		do
 		{
-			System.out.println("Username?> ");
+			System.out.print("Username?> ");
 			user = s.nextLine();
 		}while(user.contains(" "));
 
@@ -1063,7 +1124,7 @@
 			Console c = System.console();
 			do
 			{
-				pass = new String(c.readPassword("[%s]", "Password?>"));
+				pass = new String(c.readPassword("Password?> "));
 			}while(pass.contains(" "));
 		}
 		else
@@ -1131,22 +1192,24 @@
 	{
 		/* isn't java wonderful billy? */
 		String user, pass;
-		Scanner s = new Scanner(System.in);
 
 		System.out.println("Setting up Admin:");
-		System.out.print("Admin username?> ");
-		user = s.nextLine();
 
-		if(user.equals(""))
-			return;
+		do
+		{
+			System.out.print("Add admin> ");
+			user = this.GetUsername();
 
+			if(user.equals(""))
+				return;
+		}while(CheckAdmin(user));
+
 		do
 		{
-			System.out.print("Admin password?> ");
-			pass = s.nextLine();
+			pass = this.GetPassword();
 		}while(pass.equals(""));
-		
-		adm = NewAdmin(user, pass);		
+
+		NewAdmin(user, pass);
 	}
 
 	/* make checks better, reject empty input etc,
@@ -1190,7 +1253,7 @@
 		Book b = NewBook(name, author, abs, isbn, agegroup);
 	}
 
-	void Rembook()
+	public void Rembook()
 	{
 
 	}
@@ -1199,11 +1262,17 @@
 	{
 		System.out.println("Username | ID | Active | RegTime");
 		for(Admin i : AdminList.values())
-			
+		{
+			for(AdminAttr a : i.toMap().keySet())
+				System.out.print(i.toMap().get(a) + " | ");
+			System.out.println();
+		}
+/*
 			System.out.println(i.getUsername() + 
 			" | " + i.getID() +
 			" | " + i.Active +
 			" | " + i.getRegTime());
+*/
 	}
 
 	public void ListUser()
@@ -1222,6 +1291,96 @@
 		System.out.println("ISBN | Name | Author | Age Group");
 		for(Book i : BookList.values())
 			System.out.println(i.getISBN() + " | " + i.getName() + " | " + i.getAuthor() + " | " + i.getAgeGroup());
+	}
+
+	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 */