wm: java

Download patch

ref: 91ed60928a248aa4f0007b39eae3d4e6f6d7f2f5
parent: 48e2e0e38472ce670c97a2e0bcbebcaca2572823
author: mkf <mkf@d510>
date: Fri May 26 12:09:38 EDT 2023

it compiles again!!!!

--- a/Main.java
+++ b/Main.java
@@ -27,10 +27,10 @@
 {
 	ISBN,
 	Status,
+	Name,
+	Author,
 	AgeGroup,
 	Abstract,
-	Author,
-	Name,
 	Owner;
 }
 
@@ -41,7 +41,8 @@
 	Reserved,
 	ID,
 	RegTime,
-	Age;
+	Age,
+	Active;
 }
 
 enum AdminAttr
@@ -66,7 +67,7 @@
 
 	boolean CheckAdmin(String a);
 	Admin MatchAdmin(String un);
-	boolean FindAdmin(AdminAttr k, Pattern q);
+	boolean FindAdmin(AdminAttr k, Pattern p);
 
 	/* user */
 	boolean Login(User u);
@@ -77,7 +78,7 @@
 
 	boolean CheckUser(String u);
 	User MatchUser(String un);
-	boolean FindUser(UserAttr k, Pattern q);
+	boolean FindUser(UserAttr k, Pattern p);
 
 	/* books */
 	void TakeBook(String un, String isbn);
@@ -92,11 +93,9 @@
 
 	boolean CheckBook(String ISBN);
 	Book MatchBook(String isbn);
-	boolean FindBook(BookAttr k, Pattern q);
+	boolean FindBook(BookAttr k, Pattern p);
 
 	/*
-	String FindBook(String ISBN, String value); // Pointer to ISBN
-	void ReturnBook(String ISBN);
 	void ReturnBook(Book b);
 	boolean WriteStatus();
 	boolean FindBook(boolean q);  for Status value searchs 
@@ -109,12 +108,12 @@
 		void LoginMenu();
 
 		/* checks password with user, gets input from GetPassword */
-		boolean AskPass(Admin u);
-		boolean AskPass(User u);
+		// boolean AskPass(Admin u);
+		// boolean AskPass(User u);
 
 		/* different Menus, for different Users to handle */
 		void Menu(Admin u);
-		void Menu(User u);
+		// void Menu(User u);
 
 		/* Gets a String, either console or scanner, just check if it's valid or not */
 		String GetUsername();
@@ -121,34 +120,33 @@
 		String GetPassword();
 		
 		/* asks for user/book info, returns username/isbn if it exists */
-		String AskAdmin();
-		String AskUser();
-		String AskBook();
+		// String AskAdmin();
+		// String AskUser();
+		// String AskBook();
 
 		/* search foolan menu */
-		void SearchAdmin();
-		void SearchUser();
-		void SearchBook();
+		// void SearchAdmin();
+		// void SearchUser();
+		// void SearchBook();
 
 		/* add foolan menu */
 		void AddAdmin();
-		void AddUser();
+		// void AddUser();
 		void AddBook();
 
 		/* remove foolan menu */
-		void RemAdmin();
-		void RemUser();
-		void RemBook(); // maybe it's stolen
+		// void RemAdmin();
+		// void RemUser();
+		// void RemBook(); // maybe it's stolen
 
 		/* list foolan menu */
-		void ListAdmin();
-		void ListUser();
-		void ListBook();
+		// void ListAdmin();
+		// void ListUser();
+		// void ListBook();
 	}
 }
 
-/* i personally don't use it 
-	but here is a converter */
+/* i personally don't use it but here is a converter */
 class Books
 {
 	HashMap<BookAttr, String> Book = new HashMap<BookAttr, String>();
@@ -159,7 +157,7 @@
 		HashMap<String, HashMap> list = new HashMap<String, HashMap>();
 		for(Book i: bl.values())
 		{
-			list.put(i.getISBN(), i.toHashMap());
+			list.put(i.getISBN(), i.toMap());
 		}
 		return list;
 	}
@@ -171,7 +169,7 @@
 	private	boolean Status = false;
 	private String Abstract, Author, Name, ISBN;
 	private String Owner = ""; /* Username of user who reserved it */
-	private int ageGroup;
+	private int AgeGroup;
 	/* should a register be a constractor? XXX
 		 should it even named register? */
 
@@ -182,12 +180,12 @@
 		this.Author = author;
 		this.Name = name;
 		this.ISBN = i;
-		this.ageGroup = age;
+		this.AgeGroup = age;
 	}
 
 	boolean AgeOk(int age)
 	{
-		if(this.ageGroup <= age)
+		if(this.AgeGroup <= age)
 			return true;
 		else
 			return false;
@@ -242,7 +240,7 @@
 
 	int getAgeGroup()
 	{
-		return this.ageGroup;
+		return this.AgeGroup;
 	}
 
 	String getOwner()
@@ -250,16 +248,18 @@
 		return this.Owner;
 	}
 
-	HashMap<BookAttr, String> toHashMap()
+	HashMap<BookAttr, String> toMap()
 	{
 		HashMap<BookAttr, String> temp = new HashMap<BookAttr, String>();
+
 		temp.put(BookAttr.ISBN, this.ISBN);
 		temp.put(BookAttr.Status, Boolean.toString(this.Status));
-		temp.put(BookAttr.AgeGroup, Integer.toString(this.ageGroup));
 		temp.put(BookAttr.Name, this.Name);
 		temp.put(BookAttr.Author, this.Author);
-		temp.put(BookAttr.Abstract, this.Abstract);
 
+		temp.put(BookAttr.AgeGroup, Integer.toString(this.AgeGroup));
+		temp.put(BookAttr.Abstract, this.Abstract);
+		temp.put(BookAttr.Owner, this.Owner);
 		return temp;
 	}
 }
@@ -368,6 +368,21 @@
 	{
 		this.Password = pass;
 	}
+
+	HashMap<AdminAttr, String> toMap()
+	{
+		HashMap<AdminAttr, String> temp = new HashMap<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);
+		temp.put(AdminAttr.Active, Boolean.toString(this.Active));
+
+		return temp;
+	}
 }
 
 class User extends BasicUser
@@ -394,7 +409,21 @@
 		else
 			return false;
 	}
-	
+
+	HashMap<UserAttr, String> toMap()
+	{
+		HashMap<UserAttr, String> temp = new HashMap<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
@@ -404,12 +433,7 @@
 	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 HashMap<String, HashMap> AdminList = new HashMap<String, HashMap>;
-	private HashMap<String, HashMap> UserList = new HashMap<String, HashMap>;
-	private HashMap<String, HashMap> BookList = new HashMap<String, HashMap>;
-	private HashMap<String, HashMap> = new HashMap<String, HashMap>;
-*/
+
 	private ArrayList<Admin> AdminQuery = new ArrayList<Admin>();
 	private ArrayList<User> UserQuery = new ArrayList<User>();
 	private ArrayList<Book> BookQuery = new ArrayList<Book>();
@@ -463,7 +487,7 @@
 
 	public void Logout(Admin u)
 	{
-		boolean b;
+		boolean b = false;
 
 		for(Admin i : this.AdminList.values())
 		{
@@ -483,6 +507,7 @@
 			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)
@@ -579,9 +604,8 @@
 		}
 
 		if(b == null)
-		{
 			throw new NoSuchElementException("No such Admin exists.");
-		}
+
 		return b;
 	}
 
@@ -647,6 +671,23 @@
 		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)
 	{
@@ -659,6 +700,7 @@
 			}
 			return false;
 	}
+
 	public Admin MatchAdmin(String un)
 	{
 		Admin a = null;
@@ -670,12 +712,26 @@
 		}
 
 		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())
@@ -704,9 +760,21 @@
 		}
 		return u;
 	}
-	public boolean FindUser(UserAttr k, Pattern q)
-	{
 
+	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)
@@ -768,7 +836,7 @@
 		/* he has some book? blasmphy! */
 		if(!u.getReserved().isEmpty())
 		{
-			System.err.println("User (ID: " + u.getID() + ") already has a book (" + u.getReserved() + ").");
+			System.err.println("User (User name: " + u.getUsername() + ") already has a book (" + u.getReserved() + ").");
 			return;
 		}
 		u.setReserved(b.getISBN());
@@ -849,9 +917,8 @@
 
 	public void LoginMenu()
 	{
-		Scanner s;
-		String un;
-		String pass;
+		Scanner s = new Scanner(System.in);
+		String un, pass;
 
 		System.out.println("Login> Username?> ");
 		un = s.nextLine();
@@ -886,7 +953,7 @@
 	/* checks if password is valid, kinda like passwd */
 	public boolean Askpass(Admin u)
 	{
-
+		return true;
 	}
 
 	public void Menu(Admin a)
@@ -975,7 +1042,7 @@
 
 	public String GetUsername()
 	{
-		Scanner s;
+		Scanner s = new Scanner(System.in);
 		String user;
 
 		do
@@ -990,9 +1057,10 @@
 	public String GetPassword()
 	{
 		String pass;
+
 		if(System.console() != null)
 		{
-			Console c;
+			Console c = System.console();
 			do
 			{
 				pass = new String(c.readPassword("[%s]", "Password?>"));
@@ -1078,7 +1146,7 @@
 			pass = s.nextLine();
 		}while(pass.equals(""));
 		
-		NewAdmin(user, pass);		
+		adm = NewAdmin(user, pass);		
 	}
 
 	/* make checks better, reject empty input etc,
@@ -1085,27 +1153,31 @@
 		regex perhaps? */
 	public void AddBook()
 	{
+		int agegroup;
 		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"))
+		if(name.equals(""))
 			return;
+
 		System.out.println("Book> ISBN?> ");
 		isbn = s.nextLine();
-		if(isbn.equals("q"))
+		if(isbn.equals(""))
 			return;
+
 		System.out.println("Book> Author?> ");
 		author = s.nextLine();
-		if(author.equals("q"))
+		if(author.equals(""))
 			return;
+
 		// 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"))
+		if(abs.equals(""))
 			return;
+
 		System.out.print("Book> Age Group?> ");
 		while(!s.hasNextInt())
 		{
@@ -1114,11 +1186,13 @@
 			s.nextLine();
 		}
 		agegroup = s.nextInt();
-		if(agegroup < -1)
-			return;
-		b = NewBook(name, author, abs, isbn, agegroup);
-		System.out.println(b);
-		return;
+
+		Book b = NewBook(name, author, abs, isbn, agegroup);
+	}
+
+	void Rembook()
+	{
+
 	}
 
 	public void ListAdmin()