wm: java

Download patch

ref: c38e733d9de9f416af3f1e629bc351c4723e2425
parent: 3642ff2c921852babc8be83d5614fbadd32a8818
author: mkf <mkf@cloud9p.org>
date: Fri Sep 29 00:47:54 EDT 2023

changes made during exam, converted program from a library to a bookseller program

--- a/Main.java
+++ b/Main.java
@@ -21,16 +21,27 @@
 
 import java.time.LocalDateTime;
 
-enum TakeErr
+enum TakeErr {
+    Ok,
+
+    NoBook, /* no such book has found */
+    BookReserved, /* book is reserved by someone else */
+    NoBookReserved, /* no book is revserved */
+
+    NoUser, /* no such user has found */
+    UserHasABook, /* User has already registered a book */
+    Age; /* age doesn't match */
+}
+
+enum BuyErr
 {
 	Ok,
 
 	NoBook, /* no such book has found */
-	BookReserved, /* book is reserved by someone else */
-	NoBookReserved, /* no book is revserved */
+	NoCash,
+	NoBookLeft, /* no book is revserved */
 
 	NoUser, /* no such user has found */
-	UserHasABook, /* User has already registered a book */
 	Age; /* age doesn't match */
 }
 
@@ -58,7 +69,8 @@
 	Author,
 	AgeGroup,
 	Abstract,
-	Owner;
+	Count,
+	Price;
 }
 
 enum AdminAttr
@@ -69,7 +81,7 @@
 	ID,
 	RegTime,
 	// Password,
-	Active;
+	Active,
 }
 
 enum UserAttr
@@ -80,14 +92,15 @@
 	ID,
 	RegTime,
 	Age,
-	Active;
+	Active,
+	Cash;
 }
 
 interface Library 
 {
 	Admin NewAdmin(String user, String pass);
-	User NewUser(String user, int age);
-	Book NewBook(String isbn, String name, String author, int agegroup, String abst);
+	User NewUser(String user, int age, int cash);
+	Book NewBook(String isbn, String name, String author, int agegroup, int price, int count);
 
 	DelErr DelAdmin(Admin a);
 	DelErr DelUser(User u);
@@ -111,13 +124,9 @@
 	boolean FindUser(UserAttr k, Pattern p);
 	boolean FindBook(BookAttr k, Pattern p);
 
-	TakeErr TakeBook(String un, String isbn);
-	TakeErr TakeBook(Admin u, Book b);
-	TakeErr TakeBook(User u, Book b);
+	BuyErr BuyBook(String un, String isbn);
+	BuyErr BuyBook(User u, Book b);
 
-	TakeErr ReturnBook(User u);
-	TakeErr ReturnBook(Admin u);
-
 	void SaveLog(String fp, String in);
 
 	/*
@@ -164,13 +173,10 @@
 		void ListBook();
 
 		/* take and return book menus */
-		void TakeMenu(Admin a);
-		void TakeMenu(User u);
-		void ReturnMenu(Admin a);
-		void ReturnMenu(User u);
+		void BuyMenu(User u);
 
 		/* handles result of Take/IO/Del Funcations */
-		void Result(TakeErr res);
+		void Result(BuyErr res);
 		void Result(DelErr res);
 	}
 }
@@ -198,7 +204,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, Price, Count;
 
 	Book(String isbn, String name, String author, int age, String abs)
 	{
@@ -210,6 +216,16 @@
 		this.Abstract = abs;
 	}
 
+	Book(String isbn, String name, String author, int age, int price, int count)
+	{
+		/* joy of joys willy */
+		this.ISBN = isbn;
+		this.Name = name;
+		this.AgeGroup = age;
+		this.Price = price;
+		this.Count = count;
+	}
+
 	boolean AgeOk(int age)
 	{
 		if(this.AgeGroup <= age)
@@ -235,6 +251,16 @@
 		this.Status = b;
 	}
 
+	int getPrice()
+	{
+		return this.Price;
+	}
+
+	void setCount(int c)
+	{
+		this.Count = c; 
+	}
+
 	boolean getStatus()
 	{
 		return Status;
@@ -259,7 +285,12 @@
 	{
 		return this.ISBN;
 	}
+	int getCount()
+	{
+		return this.Count;
+	}
 
+
 	int getAgeGroup()
 	{
 		return this.AgeGroup;
@@ -281,7 +312,9 @@
 
 		temp.put(BookAttr.AgeGroup, Integer.toString(this.AgeGroup));
 		temp.put(BookAttr.Abstract, this.Abstract);
-		temp.put(BookAttr.Owner, this.Owner);
+
+		temp.put(BookAttr.Count, Integer.toString(this.Count));
+		temp.put(BookAttr.Price, Integer.toString(this.Price));
 		return temp;
 	}
 }
@@ -395,11 +428,12 @@
 
 class User extends BasicUser
 {
-	private int Age;
-	User(String username, int age)
+	private int Age, Cash;
+	User(String username, int age, int cash)
 	{
 		super(Role.User, username, (int)(Math.random()*1000+101), LocalDateTime.now());
 		this.Age = age;
+		this.Cash = cash;
 	}
 	int getAge()
 	{
@@ -406,6 +440,14 @@
 		return this.Age;
 	}
 
+	int getCash()
+	{
+		return this.Cash;
+	}
+	void setCash(int c)
+	{
+		this.Cash = c;
+	}
 	boolean IsReserved(Book b)
 	{
 		return (this.getReserved().equals(b.getISBN()));
@@ -527,13 +569,13 @@
 		return DelErr.Ok;
 	}
 
-	public User NewUser(String user, int age)
+	public User NewUser(String user, int age, int cash)
 	{
 		User u;
 		if(CheckUser(user))
 			throw new IllegalArgumentException("User already exists");
 
-		u = new User(user, age);
+		u = new User(user, age, cash);
 
 		this.UserList.put(user, u);
 		return u;
@@ -547,7 +589,7 @@
 		return DelErr.Ok;
 	}
 
-	public Book NewBook(String isbn, String name, String author, int agegroup, String abst)
+	public Book NewBook(String isbn, String name, String author, int agegroup, int price, int count)
 	{
 		Book b;
 		if(CheckBook(isbn))
@@ -558,7 +600,7 @@
 		if(agegroup < 2)
 			agegroup = 3;
 
-		b = new Book(isbn, name, author, agegroup, abst);
+		b = new Book(isbn, name, author, agegroup, price, count);
 		this.BookList.put(isbn, b);
 		return b;
 	}
@@ -795,132 +837,34 @@
 			return true;
 	}
 
-	/* user/admin's ID, ISBN */
-	public TakeErr TakeBook(String un, String isbn)
+	public BuyErr BuyBook(String un, String isbn)
 	{
 		Book b = this.BookList.get(isbn);
 		if(this.CheckUser(un))
-			return TakeBook(MatchUser(un), b);
-		else if(this.CheckAdmin(un))
-			return TakeBook(MatchAdmin(un), b);
+			return BuyBook(MatchUser(un), b);
+		if(CheckBook(isbn))
+			return BuyErr.NoBook;
 		else
-			return TakeErr.NoUser;
+			return BuyErr.NoUser;
 	}
 
 	/* they shouldn't be able to get a book, but still */
-	public TakeErr TakeBook(Admin a, Book b)
+	public BuyErr BuyBook(User u, Book b)
 	{
-		/* check if book is taken */
-		if(b.getStatus())
-			return TakeErr.BookReserved;
+		if(b.getCount() < 0)
+			return BuyErr.NoBookLeft;
 
-		/* he has some book? blasmphy! */
-		if(!a.getReserved().isEmpty())
-			return TakeErr.UserHasABook;
+		if(u.getCash() < b.getPrice())
+			return BuyErr.NoCash;
 
-		/* check if ages match admin has no age anyway
-		if(!b.AgeOk(a.getAge()))
-			return TakeErr.Age; */
+		u.setCash((u.getCash() - b.getPrice()));
 
-		a.setReserved(b.getISBN());
+		b.setCount((b.getCount()-1));
 
-		b.setOwner(a.getUsername());
-		b.setStatus(true);
-
-		this.ResCount++;
-		SaveLog(logfile, "[Take] " + b.getISBN() + " by " + a.getUsername() + '\n');
-		return TakeErr.Ok;
+		return BuyErr.Ok;
 	}
 
-	public TakeErr TakeBook(User u, Book b)
-	{
-		/* check if book is taken */
-		if(b.getStatus())
-			return TakeErr.BookReserved;
 
-		/* he has some book? blasmphy! */
-		if(!u.getReserved().isEmpty())
-			return TakeErr.UserHasABook;
-
-		/* check if ages match */
-		if(!b.AgeOk(u.getAge()))
-			return TakeErr.Age;
-
-		u.setReserved(b.getISBN());
-
-		b.setOwner(u.getUsername());
-		b.setStatus(true);
-
-		this.ResCount++;
-		SaveLog(logfile, "[Take] " + b.getISBN() + " by " + u.getUsername() + '\n');
-		return TakeErr.Ok;
-	}
-
-	public TakeErr ReturnBook(Admin a)
-	{
-		Book b;
-		if(a.getReserved().isEmpty())
-			return TakeErr.NoBookReserved;
-
-		if(!CheckBook(a.getReserved()))
-			return TakeErr.NoBook;
-
-		b = MatchBook(a.getReserved());
-
-		a.clearReserved();
-
-		b.setOwner(null);
-		b.setStatus(false);
-
-		this.ResCount--;
-		SaveLog(logfile, "[Return] " + b.getISBN() + " by " + a.getUsername() + '\n');
-		return TakeErr.Ok;
-	}
-
-	public TakeErr ReturnBook(User u)
-	{
-		Book b;
-		if(u.getReserved().isEmpty())
-			return TakeErr.NoBookReserved;
-
-		if(!CheckBook(u.getReserved()))
-			return TakeErr.NoBook;
-
-		b = MatchBook(u.getReserved());
-
-		u.clearReserved();
-
-		b.setOwner(null);
-		b.setStatus(false);
-
-		this.ResCount--;
-		SaveLog(logfile, "[Return] " + b.getISBN() + " by " + u.getUsername() + '\n');
-		return TakeErr.Ok;
-	}
-
-	/*
-	public IOErr CheckFile(String fp) throws IOException
-	{
-		File fd = new File(fp);
-
-		if(!fd.isFile())
-			return IOErr.NotAFile;
-
-		if(!fd.canWrite() || !fd.canRead())
-			return IOErr.PermissionDenied;
-
-		return IOErr.Ok;
-	}
-
-
-	public File OpenFile(String fp)
-	{
-		File fd = new File(fp);
-		fd.createNewFile();
-		return fd;
-	}
-	*/
-
 	public void SaveLog(String fp, String in)
 	{
 		try
@@ -934,52 +878,7 @@
 			e.printStackTrace();
 		}
 	}
-/*
-	public String LoadLog(File fd, int n) throws FileNotFoundException
-	{
-		int i = n;
-		String temp;
-		Scanner s = new Scanner(fd);
 
-		while(s.hasNextLine() && i > 0)
-		{
-			temp += s.nextLine();
-			i--;
-		}
-		return temp;
-	}
-
-	public void SaveState(File fd) throws IOException
-	{
-		FileWriter fw = new FileWriter(fd, false);
-
-		for(Book i : BookList.values())
-		{
-			for(BookAttr a : i.toMap().keySet())
-					fw.append(i.toMap().get(a) + '\n');
-		}
-		fw.flush();
-	}
-
-	public void LoadState(File fd) throws FileNotFoundException
-	{
-		int agegroup;
-		String isbn, name, author, abs;
-		Scanner s = new Scanner(fd);
-
-		while(!s.hasNextLine())
-		{
-			isbn = s.nextLine();
-			name = s.nextLine();
-			author = s.nextLine();
-			agegroup = s.nextInt();
-			s.nextLine();
-			abs = s.nextLine();
-		}
-		NewBook(isbn, name, author, agegroup, abs);
-	}
-*/
-
 class conUI implements UI
 {
 	public void Program()
@@ -1043,6 +942,21 @@
 		return u.checkPass(GetPassword());
 	}
 
+	public void AddCash(User u)
+	{
+		int c = u.getCash();
+		Scanner s = new Scanner(System.in);
+			System.out.println("Enter a number>");
+		while(!s.hasNextInt())
+		{
+			System.err.println("Enter a number>");
+			s.nextLine();
+		}
+		int t = u.getCash() + s.nextInt();
+		u.setCash(t);
+	}
+
+
 	public void Menu(Admin a)
 	{
 		int in;
@@ -1138,23 +1052,16 @@
 					this.RemBook();
 					break;
 
-				case 13:
-					this.TakeMenu(adm);
-					break;
-				case 14:
-					this.ReturnMenu(adm);
-					break;
-
 				case 15:
 					System.out.println("Logging out...");
 					Logout(adm);
 					return;
 				case 88:
-					NewBook("1", "java book", "jack c richard", 10, "a book about java and stuff");
-					NewBook("42", "C book", "dennis ritche", 18, "nightmares with pointers!");
-					NewBook("1453", "Fall of constantipole", "konstantinus", 5, "The city has fallen");
-					NewUser("ali", 20);
-					NewUser("jalal", 10);
+					NewBook("1", "java book", "jack c richard", 20, 10, 10);
+					NewBook("42", "C book", "dennis ritche", 18, 5, 20);
+					NewBook("1453", "Fall of constantipole", "konstantinus", 5, 5, 5);
+					NewUser("ali", 20, 30);
+					NewUser("jalal", 10, 40);
 					break;
 				/* quit */
 				case 99:
@@ -1181,11 +1088,11 @@
 			" 1) search book\n" +
 			" 2) list book\n\n" +
 
-			" 3) take book\n" +
-			" 4) return book\n\n" +
+			" 3) buy book\n" +
+			" 4) add cash\n" +
+			" 5) view cash\n\n" +
+			" 6) logout\n");
 
-			" 5) logout\n");
-
 			System.out.print("User Menu> ");
 			while(!s.hasNextInt())
 			{
@@ -1205,12 +1112,15 @@
 					this.ListBook();
 					break;
 				case 3:
-					this.TakeMenu(usr);
+					this.BuyMenu(usr);
 					break;
 				case 4:
-					this.ReturnMenu(usr);
+					this.AddCash(usr);
 					break;
 				case 5:
+					System.out.println(u.getCash());
+					break;
+				case 6:
 					System.out.println("Logging out...");
 					Logout(usr);
 					return;
@@ -1436,7 +1346,7 @@
 
 	public void AddUser()
 	{
-		int age;
+		int age, cash;
 		String user;
 		Scanner s = new Scanner(System.in);
 
@@ -1461,7 +1371,17 @@
 		}
 		age = s.nextInt();
 
-		NewUser(user, age);
+		System.out.print("Add User> Cash> ");
+		while(!s.hasNextInt())
+		{
+				System.err.println("Please enter a valid input");
+				System.out.print("Add User> Cash> ");
+				s.nextLine();
+		}
+		cash = s.nextInt();
+
+
+		NewUser(user, age, cash);
 	}
 
 
@@ -1469,7 +1389,7 @@
 		regex perhaps? */
 	public void AddBook()
 	{
-		int agegroup;
+		int agegroup, price, count;
 		String name, isbn, author, abs;
 		Scanner s = new Scanner(System.in);
 
@@ -1488,13 +1408,6 @@
 		author = s.nextLine();
 		if(author.equals(""))
 			return;
-
-		// XXX on a related note, should i care about dealing with multiline stuff?
-		System.out.print("Book> Abstract?> ");
-		abs = s.nextLine();
-		if(abs.equals(""))
-			return;
-
 		do
 		{
 			System.out.print("Book> Age Group?> ");
@@ -1506,9 +1419,35 @@
 			}
 			agegroup = s.nextInt();
 		}while((agegroup > 99) || (agegroup < 2));
+
+		do
+		{
+			System.out.print("Book> count> ");
+			while(!s.hasNextInt())
+			{
+				System.err.println("Please enter a valid input");
+				System.out.print("Book> count> ");
+				s.nextLine();
+			}
+			count = s.nextInt();
+		}while(count < 1);
+
+		do
+		{
+			System.out.print("Book> Price> ");
+			while(!s.hasNextInt())
+			{
+				System.err.println("Please enter a valid input");
+				System.out.print("Book> Price> ");
+				s.nextLine();
+			}
+			price = s.nextInt();
+		}while(price < 1);
+
+
 		/* ensure it's valid ^ */
 
-		Book b = NewBook(isbn, name, author, agegroup, abs);
+		Book b = NewBook(isbn, name, author, agegroup, price, count);
 	}
 
 	public void RemAdmin()
@@ -1631,17 +1570,11 @@
 		}
 	}
 
-	public void TakeMenu(Admin a)
+	public void BuyMenu(User u)
 	{
 		String isbn;
 		Scanner s = new Scanner(System.in);
 
-		if(!a.getReserved().isEmpty())
-		{
-			System.err.println("Current Admin has already has a book (ISBN: " + a.getReserved() + ").");
-			return;
-		}
-
 		System.out.println("Which book do you want to take? (you may find it's ISBN by Search menu)");
 		
 		do
@@ -1657,101 +1590,30 @@
 
 		}while(isbn.contains(" ") || !CheckBook(isbn));
 
-		Result(TakeBook(a, MatchBook(isbn)));
+		Result(BuyBook(u, MatchBook(isbn)));
 	}
 
-	public void TakeMenu(User u)
+	public void Result(BuyErr res)
 	{
-		String isbn;
-		Scanner s = new Scanner(System.in);
-
-		if(!u.getReserved().isEmpty())
-		{
-			System.err.println("Current User has already has a book (ISBN: " + u.getReserved() + ").");
-			return;
-		}
-
-		System.out.println("Which book do you want to take? (you may find it's ISBN by Search menu)");
-		
-		do
-		{
-			System.out.print("Take> ISBN?> ");
-			isbn = s.nextLine();
-
-			if(isbn.equals(""))
-				return;
-
-			if(!CheckBook(isbn))
-				System.err.println("No such book found, please try again");
-
-		}while(isbn.contains(" ") || !CheckBook(isbn));
-
-		Result(TakeBook(u, MatchBook(isbn)));
-	}
-
-	public void ReturnMenu(Admin a)
-	{
-		Scanner s = new Scanner(System.in);
-
-		if(a.getReserved().isEmpty())
-		{
-			System.err.println("You have no books reserved.");
-			return;
-		}
-
-		System.out.print("Return> Are you sure? (y/N)> ");
-
-		if(s.nextLine().equals("y"))
-			Result(ReturnBook(a));
-		else
-			System.out.println("Thought so.");
-	}
-
-	public void ReturnMenu(User u)
-	{
-		Scanner s = new Scanner(System.in);
-
-		if(u.getReserved().isEmpty())
-		{
-			System.err.println("You have no books reserved.");
-			return;
-		}
-
-		System.out.print("Return> Are you sure (y/N)?> ");
-
-		if(s.nextLine().equals("y"))
-			Result(ReturnBook(u));
-		else
-			System.out.println("Thought so.");
-	}
-
-	public void Result(TakeErr res)
-	{
 		switch(res)
 		{
 			case NoBook:
 				System.err.println("No such book has found");
 				break;
-			case BookReserved:
-				System.err.println("This book is reserved by someone else");
+			case NoBookLeft:
+				System.err.println("No book left");
 				break;
-			case NoBookReserved:
-				System.err.println("No book is reserved");
+			case NoCash:
+				System.err.println("Not enough credits");
 				break;
 			case NoUser:
 				System.err.println("No such User has found");
 				break;
-			case UserHasABook:
-				System.err.println("User has already reserved a book");
-				break;
-			case Age:
-				System.err.println("Age doesn't match!");
-				break;
 			case Ok:
 				System.out.println("Done!");
 				break;
 			default:
-				throw new IllegalStateException("Invalid TakeErr");
+				throw new IllegalStateException("Invalid BuyErr");
 		}
 	}