ref: c99a80f9d76d45e80691652af3a363eaecbd174f
parent: 77e77686bcbc23499f8fd2d714bbec9d08e23c90
author: mkf <mkf@d510>
date: Wed May 24 11:39:44 EDT 2023
god knows what i did
--- a/Main.java
+++ b/Main.java
@@ -4,65 +4,113 @@
// 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.util.regex.Pattern; // Pattern, Matcher
+import java.util.Matcher; // Scanner, ArrayList
+import java.util.Scanner;
+*/
+import java.util.*;
import java.time.*;
+import java.util.regex.*;
-enum Role
+
+/* XXX
+enum BAttr
{
- Admin,
- User,
+
}
+*/
+enum BookAttr
+{
+ Status,
+ AgeGroup,
+ Abstract,
+ Author,
+ Name,
+ ISBN,
+}
-interface Prog
+enum UserAttr
{
- /* portable? stuff */
- // XXX
- /*
+
+}
+
+interface Library
+{
+ /* portable? stuff XXX
int readin(Scanner s, String errmsg, String inputmsg);
String readin(Scanner s, String errmsg, String inputmsg);
- */
-
- /* admin stuff */
+ String getpass(); sudo/su-like
//void setup();
- Admin newadm();
- void deladm();
- boolean login(Admin u);
- void logout(Admin u);
- void MenuAdmin();
- boolean askpass(Admin u);
- // String getpass(); sudo/su-like
- Admin FindAdmin();
- boolean CheckAdmin(String a);
- void ListAdmins();
-
- /* user stuff
- // void usermenu();
- RegularUser newusr();
- void deluser();
- boolean login(RegularUser u);
-
- void logout(User u);
- askpass(User u);
*/
- void ListUsers();
- boolean CheckUser(String u);
-
- /*
+ /* book stuff
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 FindBook(boolean q); for Status value searchs */
+
+ /* objects */
+ /* id, 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);
+ void Logout(Admin a);
+
+ Admin NewAdmin();
+ void DelAdmin(Admin a);
+
+ Admin GetAdmin(); // ID or Username
+ boolean Askpass(Admin a);
+ boolean CheckAdmin(String a); // XXX get rid of this with FindAdmin
+
+ Admin FindAdmin(int ID);
+ void ListAdmin();
+
+ void Menu(Admin a);
+
+ /* user */
+ boolean Login(User u);
+ void Logout(User u);
+
+ User NewUser();
+ void DelUser(User u);
+
+ User GetUser(); // ID or Username
+ boolean Askpass(User u);
+ boolean CheckUser(int ID);
+ boolean CheckUser(String u);
+
+ User FindUser(int ID);
+ void ListUser();
+
+ void Menu(User u);
+
+ /* books */
+ boolean TakeBook(User u, Book b);
+ boolean ReturnBook(User u);
+
boolean NewBook();
- void ListBooks();
+ void DelBook(Book b);
+ boolean CheckBook(String ISBN);
+ void SearchBook(); // ISBN
+
+ boolean SearchBook(BookAttr b);
+ Book FindBook(int ISBN);
+ void ListBook();
+
}
@@ -71,6 +119,7 @@
/* book is untaken by default */
private boolean Status = false;
private String Abstract, Author, Name, ISBN;
+ private int Owner = 0; /* ID of user who reserved it */
private int ageGroup;
/* should a register be a constractor? XXX
should it even named register? */
@@ -93,6 +142,17 @@
return false;
}
+ boolean setOwner(int ID)
+ {
+ if(this.Owner != 0)
+ {
+ System.err.println("A User (ID:" + this.getOwner() + ") already got the book");
+ return false;
+ }
+ this.Owner = ID;
+ return true;
+ }
+
boolean setStatus(boolean b)
{
if(this.Status == b)
@@ -100,11 +160,8 @@
System.err.println("book (ISBN: " + this.getISBN() + ") status already set to: " + this.Status);
return false;
}
- else
- {
- this.Status = b;
- return true;
- }
+ this.Status = b;
+ return true;
}
boolean getStatus()
@@ -136,9 +193,14 @@
{
return this.ageGroup;
}
+
+ int getOwner()
+ {
+ return this.Owner;
+ }
}
-abstract class User
+abstract class BasicUser
{
private Role role;
private String Username, Reserved;
@@ -145,7 +207,7 @@
private int ID;
private LocalDateTime RegTime;
// abstract boolean IsReserved(String ISBN);
- User(Role r, String u, int id, LocalDateTime rt)
+ BasicUser(Role r, String u, int id, LocalDateTime rt)
{
this.role = r;
this.Username = u;
@@ -168,7 +230,7 @@
return this.ID;
}
- boolean setReserved(String n, ArrayList b)
+ boolean setReserved(String ISBN)
{
/* ensure he has no books */
if(this.Reserved == null)
@@ -186,6 +248,14 @@
}
+ boolean hasReserved()
+ {
+ if(this.Reserved.isEmpty())
+ return false;
+ else
+ return true;
+ }
+
String getReserved()
{
return this.Reserved;
@@ -198,7 +268,7 @@
}
-class Admin extends User
+class Admin extends BasicUser
{
private String Password;
public boolean Active = false;
@@ -241,10 +311,10 @@
}
}
-class RegularUser extends User
+class User extends BasicUser
{
private int Age;
- RegularUser(String username, int age)
+ User(String username, int age)
{
super(Role.User, username, (int)(Math.random()*1000+101), LocalDateTime.now());
this.Age = age;
@@ -268,18 +338,20 @@
}
-class Lib implements Prog
+class Lib implements Library
{
- // XXX think more, make read.password work if it can,
- // switch to HashMap? no thanks
- ArrayList<Admin> AdminList = new ArrayList<Admin>();
- /* a bit of overkill, but still */
+ // XXX think more, make read.password.
+ /*
+ HashMap<Integer, Admin> AdminList = new HashMap<Integer, Admin>();
+ HashMap<Integer, User> UserList = new HashMap<Integer, User>();
+ HashMap<String, Book> BookList = new HashMap<String, Book>();
+ */
+
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<User> UserQuery = new ArrayList<User>();
ArrayList<Book> BookQuery = new ArrayList<Book>();
+
static Admin adm;
/* is hasNextLine is of any of use? i wonder, says timmy */
@@ -307,9 +379,9 @@
return s.nextLine();
}
- public boolean login(Admin u)
+ public boolean Login(Admin u)
{
- while(!askpass(u))
+ while(!Askpass(u))
{
System.err.println("Please try again");
}
@@ -318,7 +390,7 @@
return true;
}
- public boolean login(RegularUser u)
+ public boolean Login(User u)
{
/* maybe later i want to do more stuff here */
System.out.println("Login as " + u.getUsername() + " successful");
@@ -325,7 +397,7 @@
return true;
}
- public void logout(Admin u)
+ public void Logout(Admin u)
{
System.out.println("Logging out...");
u.Active = false;
@@ -332,7 +404,7 @@
}
// XXX make boolan it you dummy, why?
- public Admin newadm()
+ public Admin NewAdmin()
{
/* isn't java wonderful billy? */
Admin a;
@@ -343,7 +415,7 @@
{
System.out.print("Admin username?> ");
user = s.nextLine();
- for(Admin i : this.AdminList)
+ for(Admin i : this.AdminList.values())
if(i.getUsername().equals(user))
{
System.out.println("User name " + user + " already exist");
@@ -351,22 +423,23 @@
}
}
- /* if user is "admin", use defaults */
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");
else
a = new Admin(user, pass);
- this.AdminList.add(a);
+ this.AdminList.put(a.getUsername(), a);
return a;
}
// XXX urgent WIP
- public void deladm()
+ // deconstrcutor?
+ public void DelAdmin()
{
Admin a;
String user = "", pass = "";
@@ -376,7 +449,7 @@
{
System.out.print("Admin username?> ");
user = s.nextLine();
- for(Admin i : this.AdminList)
+ for(Admin i : this.AdminList.values())
{
if(i.getUsername().equals(user))
{
@@ -392,21 +465,86 @@
//this.AdminList(
}
- public void MenuAdmin()
+ public void Menu(Admin a)
{
- System.out.printf(
- " 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");
+ 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();
+ System.err.println("XXX todo");
+ break;
+ case 5:
+ //this.ReturnBook();
+ System.err.println("XXX todo");
+ 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;
+ }
+ }
}
/* make checks better, reject empty input etc,
@@ -445,18 +583,18 @@
if(agegroup < -1)
return false;
b = new Book(name, author, abs, isbn, agegroup);
- System.out.println(this.BookList.add(b));
+ System.out.println(this.BookList.put(isbn, b));
return true;
}
- public void ListBooks()
+ 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)
+ 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? */
+ public boolean Askpass(Admin u) /* make it normal? */
{
String t;
Scanner s = new Scanner(System.in);
@@ -480,8 +618,76 @@
return false;
}
- public Admin FindAdmin()
+ public boolean CheckUser(int ID)
{
+ boolean b = false;
+
+ for(User i : this.UserList.values())
+ {
+ if(i.getID() == ID)
+ b = true;
+ }
+
+ if(!b)
+ {
+ System.err.println("No such user found, try again");
+ }
+ return b;
+ }
+
+ public boolean CheckAdmin(int ID)
+ {
+ boolean b = false;
+
+ for(Admin i : this.AdminList.values())
+ {
+ if(i.getID() == ID)
+ b = true;
+ }
+
+ if(!b)
+ {
+ System.err.println("No such admin found, try again");
+ }
+ 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 Admin exists.");
+ }
+ return u;
+ }
+
+ public Admin GetAdmin()
+ {
Scanner s = new Scanner(System.in);
String user = "";
@@ -494,7 +700,7 @@
}
/* see if it's used */
- for(Admin i : this.AdminList)
+ for(Admin i : this.AdminList.values())
{
if(i.getUsername().equals(user))
{
@@ -502,7 +708,7 @@
}
}
/* didn't found any, try again */
- System.out.println("No such admin found, try again");
+ System.err.println("No such admin found, try again");
user = "";
}
}
@@ -509,7 +715,7 @@
public boolean CheckAdmin(String a)
{
- for(Admin i : this.AdminList)
+ for(Admin i : this.AdminList.values())
{
if(i.getUsername().equals(a))
{
@@ -519,11 +725,11 @@
return false;
}
- public boolean CheckUser(String a)
+ public boolean CheckUser(String u)
{
- for(RegularUser i : this.UserList)
+ for(User i : this.UserList.values())
{
- if(i.getUsername().equals(a))
+ if(i.getUsername().equals(u))
{
return true;
}
@@ -531,11 +737,12 @@
return false;
}
+
public boolean FindBook(boolean q)
{
/* flush last query */
this.BookQuery.clear();
- for(Book i : BookList)
+ for(Book i : BookList.values())
{
if(i.getStatus())
this.BookQuery.add(i);
@@ -553,8 +760,9 @@
Matcher m;
switch(k)
{
+ /* XXX should i use i.getFoolan.contains()? */
case "AgeGroup":
- for(Book i : BookList)
+ for(Book i : BookList.values())
{
m = p.matcher(Integer.toString(i.getAgeGroup()));
if(m.find())
@@ -563,7 +771,7 @@
break;
case "Abstract":
- for(Book i : BookList)
+ for(Book i : BookList.values())
{
m = p.matcher(i.getAbstract());
if(m.find())
@@ -572,7 +780,7 @@
break;
case "Author":
- for(Book i : BookList)
+ for(Book i : BookList.values())
{
m = p.matcher(i.getAuthor());
if(m.find())
@@ -581,7 +789,7 @@
break;
case "Name":
- for(Book i : BookList)
+ for(Book i : BookList.values())
{
m = p.matcher(i.getName());
if(m.find())
@@ -590,7 +798,7 @@
break;
case "ISBN":
- for(Book i : BookList)
+ for(Book i : BookList.values())
{
m = p.matcher(i.getISBN());
if(m.find())
@@ -608,7 +816,7 @@
return true;
}
- public void FindBook()
+ public void SearchBook()
{
String what, patt;
Pattern p;
@@ -623,7 +831,7 @@
{
// XXX add isRegistered()
System.out.println("ISBN | Name | Author | Age Group");
- for(Book i : BookList)
+ for(Book i : BookList.values())
{
System.out.println(i.getISBN() + " | " + i.getName() + " | " + i.getAuthor() + " | " + i.getAgeGroup());
}
@@ -635,86 +843,80 @@
public void ListUsers()
{
System.out.println("Username | ID | Reserved ISBN");
- for(User i : this.UserList)
+ for(User i : this.UserList.values())
System.out.println(i.getRole() + " | " + i.getUsername() + " | " + i.getID() + " | " + i.getReserved());
}
- public void ListAdmins()
+ public void ListAdmin()
{
System.out.println("Username | ID | Active | RegTime");
- for(Admin i : this.AdminList)
+ for(Admin i : this.AdminList.values())
+
System.out.println(i.getUsername() + " | " + i.getID() + " | " + i.Active + " | " + i.getRegTime());
}
- public void getInput()
+ /* they shouldn't be able to get a book, but still */
+ public boolean TakeBook(Admin u, Book b)
{
- int in;
- boolean flag = true;
- Scanner s = new Scanner(System.in);
- while(true)
+ /* check if book is taken */
+ if(b.getStatus())
{
- System.out.print("menu> ");
- while(!s.hasNextInt())
- {
- System.out.println("Please enter a valid input");
- System.out.print("menu> ");
- s.nextLine();
- }
- in = s.nextInt();
+ System.err.println("Given book (ISBN: " + b.getISBN() + ") has been reserved.");
+ return false;
+ }
+ /* he has some book? blasmphy! */
+ if(!u.getReserved().isEmpty())
+ {
+ System.err.println("User (ID: " + u.getID() + ") already has a book (" + u.getReserved() + ").");
+ return false;
+ }
+ u.setReserved(b.getISBN());
+ b.setOwner(u.getID());
+ return true;
+ }
- switch(in)
- {
-
- case 1:
- this.NewBook();
- break;
- // XXX
- case 2:
- //this.adduser();
- System.err.println("XXX todo");
- break;
- case 3:
- this.FindBook();
- break;
- case 4:
- //this.TakeBook();
- System.err.println("XXX todo");
- break;
- case 5:
- //this.ReturnBook();
- System.err.println("XXX todo");
- 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.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;
- }
+ public boolean 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 false;
}
+ /* he has some book? blasmphy! */
+ if(!u.getReserved().isEmpty())
+ {
+ System.err.println("User (ID: " + u.getID() + ") already has a book (" + u.getReserved() + ").");
+ return false;
+ }
+ u.setReserved(b.getISBN());
+ b.setOwner(u.getID());
+ return true;
}
+
+ /* user/admin's ID, ISBN */
+/* public boolean TakeBook(int ID, String isbn)
+ {
+ if(this.CheckUser(ID))
+ {
+ User u = FindUser(ID);
+ }
+ else if(this.CheckAdmin(ID))
+ {
+ Admin u = FindAdmin(ID);
+ }
+ else
+ {
+ System.err.println("No such User or Admin has found");
+ return false;
+ }
+
+ Book b = this.BookList.get(isbn);
+
+ return this.TakeBook(u, b);
+
+ }
+*/
}
public class Main
@@ -726,7 +928,6 @@
// Scanner sf = New Scanner(file)? XXX
Lib l = new Lib();
- //l.setup();
while(true)
{
@@ -733,16 +934,15 @@
/* there is no admin! */
if(l.AdminList.size() == 0)
{
- l.adm = l.newadm();
+ l.adm = l.NewAdmin();
l.adm.Active = true;
}
else
- l.login(l.FindAdmin());
+ l.Login(l.GetAdmin());
while(l.adm.Active)
- {
- l.MenuAdmin();
+ {
/* only exits if user either logs out or smh */
- l.getInput();
+ l.Menu(l.adm);
/* user wants to quit */
if(l.adm.Active)
return;