wm: java

ref: dd82d34b0fb7572f9525ba52642cc460ba9cf4f6
dir: /Main.java/

View raw version
//package BookProj;

import java.util.*;
import java.time.*;

enum Types
{
	Status,
	AgeGroup,
	Abstract,
	Author,
	Name,
	ISBN,
}

enum Role
{
	Admin,
	User,
}

interface Prog 
{
	boolean logon(Admin u);
	boolean logon(RegularUser u);
 	/* askpass(User 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);

		/* 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, ' ');
			} */

			System.out.print("password?> ");
			t = s.nextLine();
			if(u.checkPass(t))
				return true;
			else
				return false;
	}

}


class Book
{
	/* hashmap, array list here */
	private	boolean Status;
	private String Abstract, Author, Name, ISBN;
//	Register()
//	List()
//	WriteStatus()
//	String LastStatus();
//	boolean AgeOk(int age);
}

abstract class User
{
	private Role role;
	private String Username, Reserved;
	private int ID;
	private LocalDateTime RegTime;
	// abstract boolean IsReserved(String ISBN);
	User(Role r, String u, int id, LocalDateTime rt)
	{
		this.role = r;
		this.Username = u;
		this.ID = id;
		this.RegTime = rt;
	}

	Role getRole()
	{
		return this.role;
	}

	String getUsername()
	{
		return this.Username;
	}

	int getID()
	{
		return this.ID;
	}

	boolean setReserved(String n)
	{
		/* ensure he has no books */
		if(this.Reserved == null)
		{
			this.setReserved(n);
			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;
	}

	LocalDateTime getRegTime()
	{
		return this.RegTime;
	}

}

class Admin extends User
{
	private Role role = Role.Admin;
	private String Password;
	public static boolean Active = false;
	Admin(String user, String pass)
	{
		// XXX, make an ID range for admins?
		super(Role.Admin, user, 1, LocalDateTime.now());
		/* maybe check if password is too easy? */
		this.Password = pass;
	}

	boolean setReserved(Book b)
	{
		return false; /* XXX */
	}

	boolean setReserved(String ISBN)
	{
		return false; /* XXX */
	}

	boolean isReserved(String ISBN)
	{
		return false; /* XXX */
	}
	boolean checkPass(String pass)
	{
		if(this.Password.equals(pass))
			return true;
		else
			return false;
	}
}

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());
		this.Age = age;
	}
/*
	boolean IsReserved(Book b)
	{
		if(this.Reserved == b.getISBN())
			return true;
		else
			return false;
	} */
	boolean IsReserved(String ISBN)
	{
		if(this.getReserved() == ISBN)
			return true;
		else
			return false;
	}
	
}

class Lib implements Prog
{

	// XXX think more, make read.password work if it can,
	ArrayList<RegularUser> UsersList = new ArrayList<RegularUser>();
	ArrayList<Book>	BooksList = new ArrayList<Book>();
	static Admin adm;
	public boolean logon(Admin u)
	{
			while(!Prog.askpass(u))
			{		
				System.err.println("Please try again");
			}
			System.out.println("Login as " + u.getUsername() + " successful");
			u.Active = true;
			return true;
	}

	public boolean logon(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()
	{
		/* isn't java wonderful? */
		String user = "", pass = "";
		Scanner s = new Scanner(System.in);
		System.out.println("Setting up Admin:");
		while(user == "")
		{
			System.out.print("Admin username?> ");
			user = s.nextLine();
		}

		while(pass == "")
		{

			System.out.print("Admin password?> ");
			pass = s.nextLine();
		}
		adm = new Admin(user, pass);
		
	}
	public void menu()
	{
		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");
	}

	public void getInput()
	{
		int in;
		boolean flag = true;
		Scanner s = new Scanner(System.in);
		while(flag)
		{
			System.out.print("menu> ");
			in = s.nextInt();
			switch(in)
			{
/* XXX
				case 1:
					l.addbook();
					break;
				case 2:
					l.adduser();
					break;
				case 3:
					l.findbook();
					break;
				case 4:
					l.takebook();
					break;
				case 5:
					l.returnbook();
					break;
*/
				case 6:
					this.logout(this.adm);
					flag = false;
					break;
/*
				case 7:
					l.quit();
					break;
				case 8:
					this.newadm(this.adm);
					break;
*/
				default:
					System.out.println("Invalid answer!, please try again.");
					break;
			}
		}
	}
}

public class Main
{
	public static void main(String args[])
	{
		int in;
		Scanner s = new Scanner(System.in);
//		Scanner sf = New Scanner(file)? XXX

		Lib l = new Lib();
		l.setup();
		while(true)
		{
			l.logon(l.adm);
			while(l.adm.Active)
			{	
				l.menu();
				/* only exists if user either logs out or smh */
				l.getInput();
				/* user wants to quit */
				if(l.adm.Active)
					return;
			}
		}
	}
}