ref: c29750ceb403bfaa8716480b6fdf06f578db21a9
parent: 300ffb5637b1484d96d27d05a9056f09018b787d
author: mkf <mkf@d510>
date: Fri May 26 07:34:49 EDT 2023
refactor?
--- a/Main.java
+++ b/Main.java
@@ -1,8 +1,7 @@
//package BookProj;
// XXX get rid of *s
-// maybe deconstrctor for stuff would be a good idea
-// delbook, takebook, returnbook
+// handle throws
/*
import java.util.regex.Pattern; // Pattern, Matcher
@@ -12,13 +11,13 @@
import java.util.*;
import java.time.*;
import java.util.regex.*;
-
-
+import java.io.Console;
enum Role
{
Admin,
- User,
+ User;
}
+
enum BookAttr
{
ISBN,
@@ -38,7 +37,6 @@
ID,
RegTime,
Age;
-
}
enum AdminAttr
@@ -54,89 +52,73 @@
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
- */
-
- /* objects */
- /* 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>();
-
- ArrayList<Admin> AdminQuery = new ArrayList<Admin>();
- ArrayList<User> UserQuery = new ArrayList<User>();
- ArrayList<Book> BookQuery = new ArrayList<Book>();
-
/* admin */
- boolean Login(Admin a);
+ boolean Login(Admin a, String pass);
void Logout(Admin a);
- Admin NewAdmin();
+ Admin NewAdmin(String user, String pass);
void DelAdmin(Admin a);
- boolean CheckAdmin(String a); // XXX get rid of this with FindAdmin
+ boolean CheckAdmin(String a);
+ Admin MatchAdmin(String un);
+ boolean FindAdmin(AdminAttr k, Pattern q);
- Admin FindAdmin(int ID);
-
/* user */
- // boolean Login(User u);
+ boolean Login(User u);
// void Logout(User u);
// User NewUser();
// void DelUser(User u);
- // User GetUser(); // Scanner ID or Username
boolean CheckUser(String u);
+ User MatchUser(String un);
+ boolean FindUser(String un);
- User FindUser(int ID);
-
- /* 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 */
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();
+ Book NewBook(String isbn, String name, String author, String abst, int agegroup);
void DelBook(Book b);
boolean CheckBook(String ISBN);
-
+ Book MatchBook(String isbn);
boolean FindBook(BookAttr k, Pattern q);
- void ListBook();
+ /*
+ 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
+ */
+
+
interface UI
{
+ void Program();
void LoginMenu();
+ /* checks password with user, gets input from GetPassword */
boolean AskPass(Admin u);
boolean AskPass(User u);
- /* different Menus, for different UIs to handle */
+ /* different Menus, for different Users to handle */
void Menu(Admin u);
void Menu(User u);
- /* generic */
+ /* Gets a String, either console or scanner, just check if it's valid or not */
String GetUsername();
String GetPassword();
- /* finds what we are looking for */
- Admin GetAdmin();
- User GetUser();
- Book GetBook();
+ /* asks for user/book info, returns username/isbn if it exists */
+ String AskAdmin();
+ String AskUser();
+ String AskBook();
/* search foolan menu */
void SearchAdmin();
@@ -160,7 +142,24 @@
}
}
+/* i personally don't use it
+ but here is a converter */
+class Books
+{
+ 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, HashMap> list = new HashMap<String, HashMap>();
+ for(Book i: bl.values())
+ {
+ list.put(i.getISBN(), i.toHashMap());
+ }
+ return list;
+ }
+}
+
class Book
{
/* book is untaken by default */
@@ -245,6 +244,19 @@
{
return this.Owner;
}
+
+ HashMap<BookAttr, String> toHashMap()
+ {
+ 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);
+
+ return temp;
+ }
}
abstract class BasicUser
@@ -253,7 +265,10 @@
private String Username, Reserved;
private int ID;
private LocalDateTime RegTime;
+ public boolean Active = false;
+
// abstract boolean IsReserved(String ISBN);
+
BasicUser(Role r, String u, int id, LocalDateTime rt)
{
this.role = r;
@@ -291,8 +306,7 @@
{
System.err.println("the user already have a book");
return false;
- }
-
+ }
}
boolean hasReserved()
@@ -312,13 +326,11 @@
{
return this.RegTime;
}
-
}
class Admin extends BasicUser
{
private String Password;
- public boolean Active = false;
Admin(String user)
{
super(Role.Admin, user, (int)(Math.random()*100), LocalDateTime.now());
@@ -382,7 +394,18 @@
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>();
+
+ private ArrayList<Admin> AdminQuery = new ArrayList<Admin>();
+ private ArrayList<User> UserQuery = new ArrayList<User>();
+ private ArrayList<Book> BookQuery = new ArrayList<Book>();
+
static Admin adm;
+ 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)
@@ -390,7 +413,7 @@
System.out.println(inputmsg);
while(!s.hasNextLine())
{
- System.out.println(errmsg);
+ System.err.println(errmsg);
System.out.print(inputmsg);
s.nextLine();
}
@@ -402,7 +425,7 @@
System.out.println(inputmsg);
while(!s.hasNextLine())
{
- System.out.println(errmsg);
+ System.err.println(errmsg);
System.out.print(inputmsg);
s.nextLine();
}
@@ -409,55 +432,57 @@
return s.nextLine();
}
- public boolean Login(Admin u)
+ public boolean Login(Admin u, String pass)
{
- while(!Askpass(u))
- {
- System.err.println("Please try again");
+ /* clear null */
+ adm = null;
+ if(u.Active)
+ throw new IllegalStateException("Admin is already active?");
+ /* maybe later i want to do more stuff here */
+ if(u.checkPass(pass))
+ {
+ u.Active = true;
+ adm = u;
+ return true;
}
- System.out.println("Login as " + u.getUsername() + " successful");
- u.Active = true;
- return true;
+ else
+ return false;
+
}
- public boolean Login(User u)
+ public void Logout(Admin u)
{
- /* maybe later i want to do more stuff here */
- System.out.println("Login as " + u.getUsername() + " successful");
- return true;
+ boolean b;
+
+ for(Admin i : this.AdminList.values())
+ {
+ if(i == u)
+ b = true;
+ }
+
+ if(!b)
+ throw new NoSuchElementException("No such Admin exists.");
+
+ u.Active = false;
}
- public void Logout(Admin u)
+ public boolean Login(User u)
{
- System.out.println("Logging out...");
- u.Active = false;
+ if(u.Active)
+ throw new IllegalStateException("Admin is already active?");
+ /* maybe later i want to do more stuff here */
+ u.Active = true;
}
- // XXX make boolan it you dummy, why?
- public Admin NewAdmin()
+ public Admin NewAdmin(String user, String pass)
{
- /* isn't java wonderful billy? */
Admin a;
- String user = "", pass = "";
- Scanner s = new Scanner(System.in);
- System.out.println("Setting up Admin:");
- while(user.isEmpty() || user.contains(" "))
+ for(Admin i : this.AdminList.values())
{
- System.out.print("Admin username?> ");
- user = s.nextLine();
- for(Admin i : this.AdminList.values())
- 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 */
- }
+ if(i.getUsername().equals(user))
+ throw new IllegalArgumentException("Admin already exists");
}
- while(pass.equals("") && !user.equals("admin"))
- {
- System.out.print("Admin password?> ");
- pass = s.nextLine();
- }
/* if user is "admin", use defaults */
if(user.equals("admin"))
a = new Admin("admin", "pass");
@@ -489,126 +514,14 @@
this.UserList.remove(u.getUsername(), u);
}
-
- public void Menu(Admin a)
+ /* should there be checks */
+ public Book NewBook(String isbn, String name, String author, String abst, int agegroup)
{
- int in;
- Scanner s = new Scanner(System.in);
- System.out.println("Welcome, " + a.getUsername() + "!");
- 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");
-
- System.out.print("menu> ");
- while(!s.hasNextInt())
- {
- System.out.println("Please enter a valid input");
- System.out.print("menu> ");
- s.nextLine();
- }
- in = s.nextInt();
-
- switch(in)
- {
-
- case 1:
- this.NewBook();
- break;
- case 2:
- //this.adduser();
- System.err.println("XXX todo");
- break;
- case 3:
- this.SearchBook();
- break;
- case 4:
- // this.TakeBook();
- break;
- case 5:
- // this.ReturnBook();
- break;
- case 6:
- this.Logout(this.adm);
- return;
- case 7:
- return;
- case 8:
- System.out.println("This action requires admin password");
- if(this.Askpass(this.adm)) /* current admin */
- this.NewAdmin();
- else
- System.out.println("sorry.");
- 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.");
- break;
- case 10:
- this.ListAdmin();
- break;
- case 11:
- this.ListBook();
- break;
- default:
- System.out.println("Invalid answer!, please try again.");
- break;
- }
- }
+ Book b = new Book(name, author, abst, isbn, agegroup);
+ this.BookList.put(isbn, b);
+ return b;
}
- /* 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.put(isbn, b));
- return true;
- }
public void DelBook(Book b)
{
@@ -620,38 +533,6 @@
this.BookList.remove(b.getISBN());
}
- public void ListBook()
- {
- System.out.println("total books registered: " + this.BookList.size());
- System.out.println("ISBN | Name | Author | Age Group");
- for(Book i : this.BookList.values())
- 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 boolean CheckBook(String isbn)
{
boolean b = false;
@@ -669,160 +550,23 @@
return b;
}
-
- public boolean CheckAdmin(int ID)
+ public Book MatchBook(String isbn)
{
- boolean b = false;
+ Book b = null;
- for(Admin i : this.AdminList.values())
+ for(Book i : this.BookList.values())
{
- if(i.getID() == ID)
- b = true;
+ if(i.getISBN().equals(isbn))
+ b = i;
}
- if(!b)
+ if(b == null)
{
- System.err.println("No such admin found, try again");
+ throw new NoSuchElementException("No such Admin exists.");
}
return b;
}
- public Admin FindAdmin(int ID)
- {
- Admin a = null;
-
- for(Admin i : this.AdminList.values())
- {
- if(i.getID() == ID)
- a = i;
- }
-
- if(a == null)
- {
- throw new NoSuchElementException("No such Admin exists.");
- }
- return a;
- }
-
- public User FindUser(int ID)
- {
- User u = null;
-
- for(User i : this.UserList.values())
- {
- if(i.getID() == ID)
- u = i;
- }
-
- 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;
- }
-
- public Admin GetAdmin()
- {
- 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.values())
- {
- if(i.getUsername().equals(user))
- {
- return i;
- }
- }
- /* didn't found any, try again */
- System.err.println("No such admin found, try again");
- user = "";
- }
- }
-
- public boolean CheckAdmin(String a)
- {
- for(Admin i : this.AdminList.values())
- {
- if(i.getUsername().equals(a))
- {
- return true;
- }
- }
- return false;
- }
-
- public boolean CheckUser(String u)
- {
- for(User i : this.UserList.values())
- {
- if(i.getUsername().equals(u))
- {
- return true;
- }
- }
- return false;
- }
-
-
- public boolean FindBook(boolean q)
- {
- /* flush last query */
- this.BookQuery.clear();
- for(Book i : BookList.values())
- {
- if(i.getStatus())
- this.BookQuery.add(i);
- }
- if(this.BookQuery.size() == 0)
- return false;
- else
- return true;
- }
-
public boolean FindBook(BookAttr k, Pattern p)
{
/* flush last query */
@@ -886,72 +630,76 @@
return true;
}
- public void SearchBook()
+ public boolean CheckAdmin(String a)
{
- String attr, patt;
- Pattern pattern;
- BookAttr ba;
- Scanner s = new Scanner(System.in);
- System.out.println("Attr: Status, Name, Abstract, ISBN, oISBN, AgeGroup etc.");
- System.out.println("Find> What?> ");
- attr = s.nextLine();
- System.out.println("Find> Pattern?> ");
- patt = s.nextLine();
- 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
+ for(Admin i : this.AdminList.values())
+ {
+ if(i.getUsername().equals(a))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+ public Admin MatchAdmin(String un)
+ {
+ Admin a = null;
+
+ for(Admin i : this.AdminList.values())
{
- System.err.println("Invalid Attr");
- return;
+ if(i.getUsername().equals(un))
+ a = i;
}
- if(FindBook(ba, pattern))
+
+ if(a == null)
{
- // XXX add isRegistered()
- System.out.println("ISBN | Name | Author | Age Group");
- for(Book i : BookList.values())
+ throw new NoSuchElementException("No such Admin exists.");
+ }
+ return a;
+ }
+
+ public boolean CheckUser(String u)
+ {
+ for(User i : this.UserList.values())
{
- System.out.println(i.getISBN() +
- " | " + i.getName() +
- " | " + i.getAuthor() +
- " | " + i.getAgeGroup());
+ if(i.getUsername().equals(u))
+ {
+ return true;
+ }
}
- }
- else
- System.out.println("No match has found");
+ return false;
}
- public void ListUsers()
+ public User MatchUser(String un)
{
- System.out.println("Username | ID | Reserved ISBN");
+ User u = null;
+
for(User i : this.UserList.values())
- System.out.println(i.getRole() +
- " | " + i.getUsername() +
- " | " + i.getID() +
- " | " + i.getReserved());
+ {
+ if(i.getUsername().equals(un))
+ u = i;
+ }
+
+ if(u == null)
+ {
+ throw new NoSuchElementException("No such User exists.");
+ }
+ return u;
}
- public void ListAdmin()
+ public boolean FindBook(boolean q)
{
- System.out.println("Username | ID | Active | RegTime");
- for(Admin i : this.AdminList.values())
-
- System.out.println(i.getUsername() +
- " | " + i.getID() +
- " | " + i.Active +
- " | " + i.getRegTime());
+ /* flush last query */
+ this.BookQuery.clear();
+ for(Book i : BookList.values())
+ {
+ if(i.getStatus())
+ this.BookQuery.add(i);
+ }
+ if(this.BookQuery.size() == 0)
+ return false;
+ else
+ return true;
}
/* user/admin's ID, ISBN */
@@ -959,9 +707,9 @@
{
Book b = this.BookList.get(isbn);
if(this.CheckUser(un))
- TakeBook(FindUser(un), b);
+ TakeBook(MatchUser(un), b);
else if(this.CheckAdmin(un))
- TakeBook(FindAdmin(un), b);
+ TakeBook(MatchAdmin(un), b);
else
System.err.println("No such User or Admin " + un + " has found");
}
@@ -984,6 +732,7 @@
}
u.setReserved(b.getISBN());
b.setOwner(u.getUsername());
+ this.ResCount++;
}
public void TakeBook(User u, Book b)
@@ -1002,6 +751,7 @@
}
u.setReserved(b.getISBN());
b.setOwner(u.getUsername());
+ this.ResCount++;
}
public void ReturnBook(Admin u)
@@ -1024,6 +774,7 @@
throw new NoSuchElementException("No such book exists.");
u.setReserved(null);
b.setOwner(null);
+ this.ResCount--;
}
public void ReturnBook(User u)
@@ -1046,38 +797,347 @@
throw new NoSuchElementException("No such book exists.");
u.setReserved(null);
b.setOwner(null);
+ this.ResCount--;
}
-}
-public class Main
+class conUI implements UI
{
- public static void main(String args[])
+ public void Program()
{
- int in;
- Scanner s = new Scanner(System.in);
- // Scanner sf = New Scanner(file)? XXX
-
- Lib l = new Lib();
-
while(true)
{
/* there is no admin! */
- if(l.AdminList.size() == 0)
+ if(AdminList.size() == 0)
{
- l.adm = l.NewAdmin();
- l.adm.Active = true;
+ AddAdmin();
+ adm.Active = true;
}
else
- l.Login(l.GetAdmin());
- while(l.adm.Active)
+ this.LoginMenu();
+ while(adm.Active || usr.Active)
{
/* only exits if user either logs out or smh */
- l.Menu(l.adm);
+ this.Menu(adm);
/* user wants to quit */
- if(l.adm.Active)
+ if(adm.Active)
return;
}
}
+ }
+
+ public void LoginMenu()
+ {
+ Scanner s;
+ String un;
+ String pass;
+
+ System.out.println("Login> Username?> ");
+ un = s.nextLine();
+
+ if(CheckAdmin(un))
+ {
+ System.out.println("Login> Pass?> ");
+ pass = this.GetPassword();
+
+ Admin a = MatchAdmin(un);
+ if(Login(a, pass))
+ {
+ System.out.println("Auth as " + a.getUsername() + " sucessful");
+ return;
+ }
+ else
+ {
+ System.out.println("Auth fail");
+ return;
+ }
+ }
+ else if(CheckUser(un))
+ {
+ User u = MatchUser(un);
+ if(Login(u))
+ {
+ System.out.println("Login sucessful");
+ }
+ }
+ }
+
+ /* checks if password is valid, kinda like passwd */
+ public boolean Askpass(Admin u)
+ {
+
+ }
+
+ public void Menu(Admin a)
+ {
+ int in;
+ Scanner s = new Scanner(System.in);
+ System.out.println("Welcome, " + a.getUsername() + "!");
+ 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");
+
+ System.out.print("menu> ");
+ while(!s.hasNextInt())
+ {
+ System.err.println("Please enter a valid input");
+ System.out.print("menu> ");
+ s.nextLine();
+ }
+ in = s.nextInt();
+
+ switch(in)
+ {
+
+ case 1:
+ this.AddBook();
+ break;
+ case 2:
+ //this.AddUserr();
+ System.err.println("XXX todo");
+ break;
+ case 3:
+ this.SearchBook();
+ break;
+ case 4:
+ // this.TakeBook();
+ break;
+ case 5:
+ // this.ReturnBook();
+ break;
+ case 6:
+ System.out.println("Logging out...");
+ Logout(adm);
+ return;
+ case 7:
+ return;
+ case 8:
+ System.out.println("This action requires admin password");
+ if(this.Askpass(adm)) /* current admin */
+ this.AddAdmin();
+ else
+ System.err.println("Incorrect Password.");
+ break;
+ case 9:
+ System.out.println("This action requires admin password");
+ // if(this.Askpass(this.adm)) /* current admin */
+ // this.DelAdmin();
+ // else
+ // System.err.println("Incorrect Password.");
+ break;
+ case 10:
+ this.ListAdmin();
+ break;
+ case 11:
+ this.ListUser();
+ break;
+ case 12:
+ this.ListBook();
+ break;
+ default:
+ System.out.println("Invalid answer!, please try again.");
+ break;
+ }
+ }
+ }
+
+ public String GetUsername()
+ {
+ Scanner s;
+ String user;
+
+ do
+ {
+ System.out.println("Username?> ");
+ user = s.nextLine();
+ }while(user.contains(" "));
+
+ return user;
+ }
+
+ public String GetPassword()
+ {
+ String pass;
+ if(System.console() != null)
+ {
+ Console c;
+ do
+ {
+ pass = new String(c.readPassword("[%s]", "Password?>"));
+ }while(pass.contains(" "));
+ }
+ else
+ {
+ /* no console? */
+ Scanner s = new Scanner(System.in);
+ do
+ {
+ System.out.print("Password?> ");
+ pass = s.nextLine();
+ }while(pass.contains(" "));
+ }
+ return pass;
+ }
+
+
+ public void SearchBook()
+ {
+ String attr, patt;
+ Pattern pattern;
+ BookAttr ba;
+ Scanner s = new Scanner(System.in);
+ System.out.println("Attr: Status, Name, Abstract, ISBN, oISBN, AgeGroup etc.");
+ System.out.println("Find> What?> ");
+ attr = s.nextLine();
+ System.out.println("Find> Pattern?> ");
+ patt = s.nextLine();
+ 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());
+ }
+ }
+ else
+ System.out.println("No match has found");
+ }
+
+ public void AddAdmin()
+ {
+ /* 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("Admin password?> ");
+ pass = s.nextLine();
+ }while(pass.equals(""));
+
+ NewAdmin(user, pass);
+ }
+
+ /* make checks better, reject empty input etc,
+ regex perhaps? */
+ public void AddBook()
+ {
+ 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;
+ System.out.println("Book> ISBN?> ");
+ isbn = s.nextLine();
+ if(isbn.equals("q"))
+ return;
+ System.out.println("Book> Author?> ");
+ author = s.nextLine();
+ if(author.equals("q"))
+ 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"))
+ return;
+ 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;
+ b = NewBook(name, author, abs, isbn, agegroup);
+ System.out.println(b);
+ return;
+ }
+
+ public void ListAdmin()
+ {
+ System.out.println("Username | ID | Active | RegTime");
+ for(Admin i : AdminList.values())
+
+ System.out.println(i.getUsername() +
+ " | " + i.getID() +
+ " | " + i.Active +
+ " | " + i.getRegTime());
+ }
+
+ public void ListUser()
+ {
+ System.out.println("Username | ID | Reserved ISBN");
+ for(User i : UserList.values())
+ System.out.println(i.getRole() +
+ " | " + i.getUsername() +
+ " | " + i.getID() +
+ " | " + i.getReserved());
+ }
+
+ public void ListBook()
+ {
+ System.out.println("total books registered: " + BookList.size());
+ System.out.println("ISBN | Name | Author | Age Group");
+ for(Book i : BookList.values())
+ System.out.println(i.getISBN() + " | " + i.getName() + " | " + i.getAuthor() + " | " + i.getAgeGroup());
+ }
+
+} /* end of conUI */
+}
+
+public class Main
+{
+ public static void main(String args[])
+ {
+ Lib l = new Lib();
+ Lib.UI ui = l.new conUI();
+ ui.Program();
}
}