wm: java

Download patch

ref: 77e77686bcbc23499f8fd2d714bbec9d08e23c90
parent: 85bd13fb2d589a45d9d8b4ff256bead3d2a46215
author: mkf <mkf@d510>
date: Mon May 22 16:43:36 EDT 2023

honestly, too much of changes to write a proper message for

--- a/Main.java
+++ b/Main.java
@@ -1,18 +1,13 @@
 //package BookProj;
 
-import java.util.*;
+// XXX get rid of *s
+// maybe deconstrctor for stuff would be a good idea
+// delbook, takebook, returnbook
+
+import java.util.regex.*; // Pattern, Matcher
+import java.util.*; // Scanner, ArrayList
 import java.time.*;
 
-enum Types
-{
-	Status,
-	AgeGroup,
-	Abstract,
-	Author,
-	Name,
-	ISBN,
-}
-
 enum Role
 {
 	Admin,
@@ -19,56 +14,128 @@
 	User,
 }
 
+
 interface Prog 
 {
-	boolean logon(Admin u);
-	boolean logon(RegularUser u);
- 	/* askpass(User u); */
+	/* portable? stuff */
+	// XXX
+	/*
+	int readin(Scanner s, String errmsg, String inputmsg);
+	String readin(Scanner s, String errmsg, String inputmsg);
+	*/
+
+	/* admin stuff */
+	//void setup();
+	Admin newadm();
+	void deladm();
+	boolean login(Admin u);
 	void logout(Admin u);
-	void setup();
-	/* void logout(User u);
-	void menu();
-	// Book FindBook(type key, String value); // Pointer to whole book
-	String FindBook(type key, String value); // Pointer to ISBN
-	void ReturnBook(String ISBN);
-	void ReturnBook(Book b); */
-	static boolean askpass(Admin u)
-	{
-		String t;
-		Scanner s = new Scanner(System.in);
+	void MenuAdmin();
+	boolean askpass(Admin u);
+	// String getpass(); sudo/su-like
+	Admin FindAdmin();
+	boolean CheckAdmin(String a);
+	void ListAdmins();
 
-		/* oh yeah, console and stuff
-		if(System.console() != null)
-			char passwd[], esc = 'q';
-			// oh yeah, password and stuff
-			if(passwd = cons.readPassword("[%s]", "Password:")) != null)
-			{
-				if(u.checkPass(passwd))
-					t = true;
-     			java.util.Arrays.fill(passwd, ' ');
-			} */
+	/* user stuff
+	// void usermenu();
+	RegularUser newusr();
+	void deluser();
+	boolean login(RegularUser u);
+	
+	void logout(User u);
+	askpass(User u);
+	*/
 
-			System.out.print("password?> ");
-			t = s.nextLine();
-			if(u.checkPass(t))
-				return true;
-			else
-				return false;
-	}
+	void ListUsers();
+	boolean CheckUser(String u);
 
+	/*
+	String FindBook(String ISBN, String value); // Pointer to ISBN
+	void ReturnBook(String ISBN);
+	void ReturnBook(Book b);
+	boolean WriteStatus();
+	String LastStatus();
+	*/
+	void FindBook(); /* no input? i will get my own! */
+	boolean FindBook(String k, Pattern p);
+	boolean FindBook(boolean q); /* for Status value searchs */
+	boolean NewBook();
+	void ListBooks();
+
 }
 
 
 class Book
 {
-	/* hashmap, array list here */
-	private	boolean Status;
+	/* book is untaken by default */
+	private	boolean Status = false;
 	private String Abstract, Author, Name, ISBN;
-//	Register()
-//	List()
-//	WriteStatus()
-//	String LastStatus();
-//	boolean AgeOk(int age);
+	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 setStatus(boolean b)
+	{
+		if(this.Status == b)
+		{
+			System.err.println("book (ISBN: " + this.getISBN() + ") status already set to: " + this.Status);
+			return false;  
+		}
+		else
+		{
+			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;
+	}
 }
 
 abstract class User
@@ -101,25 +168,26 @@
 		return this.ID;
 	}
 
-	boolean setReserved(String n)
+	boolean setReserved(String n, ArrayList b)
 	{
 		/* ensure he has no books */
 		if(this.Reserved == null)
 		{
-			this.setReserved(n);
+			/* ensure book is not taken by someone else 
+			if(
+			return true; */
 			return true;
 		}
-		// XXX add case if that book doesn't exist
 		else
 		{
 			System.err.println("the user already have a book");
 			return false;
 		}
+		
 	}
 
 	String getReserved()
 	{
-		/* ISBN */
 		return this.Reserved;
 	}
 
@@ -132,15 +200,18 @@
 
 class Admin extends User
 {
-	private Role role = Role.Admin;
 	private String Password;
-	public static boolean Active = false;
-	Admin(String user, String pass)
+	public boolean Active = false;
+	Admin(String user)
 	{
-		// XXX, make an ID range for admins?
-		super(Role.Admin, user, 1, LocalDateTime.now());
+		super(Role.Admin, user, (int)(Math.random()*100), LocalDateTime.now());
+
+	}
+	Admin(String user, String Pass)
+	{
 		/* maybe check if password is too easy? */
-		this.Password = pass;
+		super(Role.Admin, user, (int)(Math.random()*100), LocalDateTime.now());
+		this.setPassword(Pass);
 	}
 
 	boolean setReserved(Book b)
@@ -164,25 +235,29 @@
 		else
 			return false;
 	}
+	void setPassword(String pass)
+	{
+		this.Password = pass;
+	}
 }
 
 class RegularUser extends User
 {
-	private Role role = Role.User;
 	private int Age;
 	RegularUser(String username, int age)
 	{
-		super(Role.User, username, (int)Math.random()*100, LocalDateTime.now());
+		super(Role.User, username, (int)(Math.random()*1000+101), LocalDateTime.now());
 		this.Age = age;
 	}
-/*
+
 	boolean IsReserved(Book b)
 	{
-		if(this.Reserved == b.getISBN())
+		if(this.getReserved().equals(b.getISBN()))
 			return true;
 		else
 			return false;
-	} */
+	}
+
 	boolean IsReserved(String ISBN)
 	{
 		if(this.getReserved() == ISBN)
@@ -197,12 +272,44 @@
 {
 
 	// XXX think more, make read.password work if it can,
-	ArrayList<RegularUser> UsersList = new ArrayList<RegularUser>();
-	ArrayList<Book>	BooksList = new ArrayList<Book>();
+	// switch to HashMap? no thanks
+	ArrayList<Admin> AdminList = new ArrayList<Admin>();
+	/* a bit of overkill, but still */
+	ArrayList<Admin> AdminQuery = new ArrayList<Admin>();
+	ArrayList<RegularUser> UserList = new ArrayList<RegularUser>();
+	ArrayList<RegularUser> UserQuery = new ArrayList<RegularUser>();
+	ArrayList<Book>	BookList = new ArrayList<Book>();
+	ArrayList<Book> BookQuery = new ArrayList<Book>();
 	static Admin adm;
-	public boolean logon(Admin u)
+
+	/* is hasNextLine is of any of use? i wonder, says timmy */
+	public int readint(Scanner s, String errmsg, String inputmsg)
 	{
-			while(!Prog.askpass(u))
+		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");
 			}
@@ -211,62 +318,346 @@
 			return true;
 	}
 
-	public boolean logon(RegularUser u)
+	public boolean login(RegularUser 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;
 	}
-	public void setup()
+
+	// XXX make boolan it you dummy, why?
+	public Admin newadm()
 	{
-		/* isn't java wonderful? */
+		/* isn't java wonderful billy? */
+		Admin a;
 		String user = "", pass = "";
 		Scanner s = new Scanner(System.in);
 		System.out.println("Setting up Admin:");
-		while(user == "")
+		while(user.isEmpty() || user.contains(" "))
 		{
 			System.out.print("Admin username?> ");
 			user = s.nextLine();
+			for(Admin i : this.AdminList)
+				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 == "")
+		/* if user is "admin", use defaults */
+		while(pass.equals("") && !user.equals("admin"))
 		{
-
 			System.out.print("Admin password?> ");
 			pass = s.nextLine();
 		}
-		adm = new Admin(user, pass);
-		
+		if(user.equals("admin"))
+			a = new Admin("admin", "pass");
+		else
+			a = new Admin(user, pass);
+		this.AdminList.add(a);
+		return a;
 	}
-	public void menu()
+
+	// XXX urgent WIP
+	public void deladm()
 	{
+		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)
+			{
+				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 MenuAdmin()
+	{
 		System.out.printf(
-		"1) new book\n" +
-		"2) new user\n" +
-		"3) find book\n" +
-		"4) take book\n" +
-		"5) return book\n" +
-		"6) add new admin\n" +
-		"7) quit!\n" +
-		"8) logout\n");
+		" 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");
 	}
 
+	/* 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.add(b));
+		return true;
+	}
+	public void ListBooks()
+	{
+		System.out.println("total books registered: " + this.BookList.size());
+		System.out.println("ISBN | Name | Author | Age Group");
+		for(Book i : this.BookList)
+			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 Admin FindAdmin()
+	{
+		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)
+			{
+				if(i.getUsername().equals(user))
+				{
+					return i;
+				}
+			}
+			/* didn't found any, try again */
+			System.out.println("No such admin found, try again");
+			user = "";
+		}
+	}
+
+	public boolean CheckAdmin(String a)
+	{
+			for(Admin i : this.AdminList)
+			{
+				if(i.getUsername().equals(a))
+				{
+					return true;
+				}
+			}
+			return false;
+	}
+
+	public boolean CheckUser(String a)
+	{
+			for(RegularUser i : this.UserList)
+			{
+				if(i.getUsername().equals(a))
+				{
+					return true;
+				}
+			}
+			return false;
+	}
+
+	public boolean FindBook(boolean q)
+	{
+		/* flush last query */
+		this.BookQuery.clear();
+		for(Book i : BookList)
+		{
+			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)
+		{
+			case "AgeGroup":
+				for(Book i : BookList)
+				{
+					m = p.matcher(Integer.toString(i.getAgeGroup()));
+					if(m.find())
+						this.BookQuery.add(i);
+				}
+				break;
+
+			case "Abstract":
+				for(Book i : BookList)
+				{
+					m = p.matcher(i.getAbstract());
+					if(m.find())
+						this.BookQuery.add(i);
+				}
+				break;
+
+			case "Author":
+				for(Book i : BookList)
+				{
+					m = p.matcher(i.getAuthor());
+					if(m.find())
+						this.BookQuery.add(i);
+				}
+				break;
+
+			case "Name":
+				for(Book i : BookList)
+				{
+					m = p.matcher(i.getName());
+					if(m.find())
+						this.BookQuery.add(i);
+				}
+				break;
+
+			case "ISBN":
+				for(Book i : BookList)
+				{
+					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 FindBook()
+	{
+		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)
+			{
+				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)
+			System.out.println(i.getRole() + " | " + i.getUsername() + " | " + i.getID() + " | " + i.getReserved());
+	}
+
+	public void ListAdmins()
+	{
+		System.out.println("Username | ID | Active | RegTime");
+		for(Admin i : this.AdminList)
+			System.out.println(i.getUsername() + " | " + i.getID() + " | " + i.Active + " | " + i.getRegTime());
+	}
+
 	public void getInput()
 	{
 		int in;
-		boolean t = true, flag = true;
+		boolean flag = true;
 		Scanner s = new Scanner(System.in);
-		while(flag)
+		while(true)
 		{
 			System.out.print("menu> ");
 			while(!s.hasNextInt())
 			{
 				System.out.println("Please enter a valid input");
+				System.out.print("menu> ");
 				s.nextLine();
 			}
 			in = s.nextInt();
@@ -273,35 +664,51 @@
 
 			switch(in)
 			{
-/* XXX
+			
 				case 1:
-					l.addbook();
+					this.NewBook();
 					break;
+			// XXX
 				case 2:
-					l.adduser();
+					//this.adduser();
+					System.err.println("XXX todo");
 					break;
 				case 3:
-					l.findbook();
+					this.FindBook();
 					break;
 				case 4:
-					l.takebook();
+					//this.TakeBook();
+					System.err.println("XXX todo");
 					break;
 				case 5:
-					l.returnbook();
+					//this.ReturnBook();
+					System.err.println("XXX todo");
 					break;
-*/
 				case 6:
 					this.logout(this.adm);
-					flag = false;
-					break;
-/*
+					return;
 				case 7:
-					l.quit();
-					break;
+					return;
 				case 8:
-					this.newadm(this.adm);
+					System.out.println("This action requires admin password");
+					if(this.askpass(this.adm)) /* current admin */
+						this.newadm();
+					else
+						System.out.println("sorry.");
 					break;
-*/
+				case 9:
+					System.out.println("This action requires admin password");
+					if(this.askpass(this.adm)) /* current admin */
+						this.deladm();
+					else
+						System.out.println("sorry.");
+					break;
+				case 10:
+					this.ListAdmins();
+					break;
+				case 11:
+					this.ListBooks();
+					break;
 				default:
 					System.out.println("Invalid answer!, please try again.");
 					break;
@@ -316,17 +723,25 @@
 	{
 		int in;
 		Scanner s = new Scanner(System.in);
-//		Scanner sf = New Scanner(file)? XXX
+		// Scanner sf = New Scanner(file)? XXX
 
 		Lib l = new Lib();
-		l.setup();
+		//l.setup();
+
 		while(true)
 		{
-			l.logon(l.adm);
+			/* there is no admin! */
+			if(l.AdminList.size() == 0)
+			{
+				l.adm = l.newadm();
+				l.adm.Active = true;
+			}
+			else
+				l.login(l.FindAdmin());
 			while(l.adm.Active)
 			{	
-				l.menu();
-				/* only exists if user either logs out or smh */
+				l.MenuAdmin();
+				/* only exits if user either logs out or smh */
 				l.getInput();
 				/* user wants to quit */
 				if(l.adm.Active)