Skip to content

Instantly share code, notes, and snippets.

@toroduque
Last active January 20, 2022 06:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toroduque/ee606f7b82e0a97c717e5646910b392d to your computer and use it in GitHub Desktop.
Save toroduque/ee606f7b82e0a97c717e5646910b392d to your computer and use it in GitHub Desktop.
import java.util.*;
/**
* Write a description of class BookList here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class BookList
{
// instance variables - replace the example below with your own;
private String[] books = new String[10];
/**
* Constructor for objects of class BookList
*/
public BookList()
{
books = new String[10];
}
/**
* Array of the 10 books available that users can rent
*
*/
public String[] books()
{
String[] books = new String[10];
books[0] = "Learn Java for Dummies,John Smith";
books[1] = "Learn JavaScript in 10 Minutes,Jack Arnold";
books[2] = "Learn IOS,Edgar Alan Poe";
books[3] = "Learn SQL,James Rodriguez";
books[4] = "Learning .Net is not boring,Hungry Jack";
books[5] = "Shine while learing Ruby,Britney Spears";
books[6] = "Move like a snake with Phython";
books[7] = "Learn PHP and work in Facebook,Bill Gates";
books[8] = "Learn C only if you are smart, Goofy";
books[9] = "Learn C++ in a year++, Atreyu";
return books;
}
public void displayAllBooks()
{
String[] displayBooks = books();
for (int count = 0; count < displayBooks.length; count++)
System.out.println((count + 1) + ") " +displayBooks[count]);
}
public String getBook(int thisBook)
{
String[] getBook = books();
return getBook[thisBook];
}
}
import java.util.*;
/**
* Write a description of class Library here.
*
* @author Daniel Toro
* @student 27045382
* @version May/2016
*/
public class Library
{
// instance variables - replace the example below with your own
private String borrowerMenu;
private String mainMenu;
private int userSelected;
private UserList userListLibrary;
private BookList bookListLibrary;
/**
* Constructor for objects of class Library
*/
public Library()
{
mainMenu = "";
borrowerMenu = "";
userSelected = 0;
userListLibrary = new UserList();
bookListLibrary = new BookList();
}
/**
* This method display the borrower menu of the library with the options available.
* Every option is attached to one functionality of the borrower's menu, therefore to other methods.
*/
public void displayBorrowerMenu()
{
System.out.println("Manage Borrowers Menu");
System.out.println("======================================");
// Start new game
System.out.println("(1) Borrow a book");
System.out.println("(2) Return a book");
System.out.println("(3) List borrowed books");
System.out.println("(4) Return to main menu");
System.out.println("------------------------");
System.out.println("Choose an option:");
}
/**
* Display the library help guide and describes the function of each option in the Library menu.
* This method correspond to the Option 4 of the mainMenu field.
*/
public void displayHelp()
{
System.out.println("Option 1: If you select this options the game will start.");
System.out.println(" The game consists in two players rolling dice until one of them reaches the maximum scored that is set at the begining of the game by the same players");
System.out.println("Option 2: This option will roll the dices for both players and gives you the sum of each score for this round");
System.out.println("Option 3: This option will gives the score of each player and will show who is leading the game");
System.out.println("Option 4: Will takes you to this help guide!");
System.out.println("Option 5: When you press '5' this will quit the game.");
System.out.println("");
}
/**
* This method display the menu of the library with the options available.
* Every option is attached to one functionality of the library, therefore to other methods.
*/
public void displayMainMenu()
{
System.out.println("Welcome to My Library!");
System.out.println("======================================");
// Start new game
System.out.println("(1) Register New Borrower");
System.out.println("(2) Manage Borrowers");
System.out.println("(3) List All Borrowers");
System.out.println("(4) Display help");
System.out.println("(5) Exit Library");
System.out.println("------------------------");
System.out.println("Choose an option:");
}
/**
* It will return the actual value of the main menu that is selected at the moment of it is called.
*/
public String getMainMenuChoice()
{
return mainMenu;
}
/**
* It will return the actual value of the borrower menu that is selected at the moment of it is called.
*/
public String getBorrowerMenuChoice()
{
return borrowerMenu;
}
/**
* It will return the actual value of userSelected field.
*/
public int getUserSelected()
{
return userSelected;
}
public void rentBook() // CHECK THIS METHOD!!
{
int bookSelected = 0;
Scanner console = new Scanner(System.in);
System.out.println("These are the available books to rent: ");
System.out.println("========================================");
bookListLibrary.displayAllBooks();
System.out.println("");
System.out.println("Please select the book the user would like to rent: ");
try
{
bookSelected = console.nextInt();
if ((bookSelected - 1) < bookListLibrary.books().length && bookSelected > 0)
userListLibrary.assignBook((getUserSelected()), bookListLibrary.getBook(bookSelected - 1));
else
System.out.println("Please select one of the available books");
}
catch (InputMismatchException noInteger)
{
System.out.println("Please enter a number");
System.out.println("");
}
}
public void returnBook()
{
int bookSelected;
Scanner console = new Scanner (System.in);
System.out.println("These are the books available to return:");
System.out.println("========================================");
userListLibrary.displayBorrowedBooks(getUserSelected());
System.out.println("Please select the book the user would like to return: ");
try
{
bookSelected = console.nextInt();
userListLibrary.removeBook(getUserSelected(), bookListLibrary.getBook(bookSelected -1));
}
catch (Exception noInteger)
{
System.out.println("");
System.out.println("Please enter a number");
System.out.println("");
console.nextLine();
}
}
/**
* Runs the Borrower Menu through a switch statement.
* Each case of the switch statement corresponds to one option of the functionality of the Borrower Menu.
*/
public void startBorrowerMenu()
{
selectUserManage(); // User select what borrower will manage
do {
displayBorrowerMenu();
setBorrowerMenuChoice();
switch(borrowerMenu)
{
case "1":
rentBook();
break;
case "2":
returnBook();
break;
case "3":
userListLibrary.displayBorrowedBooks(getUserSelected());
break;
default:
if(getBorrowerMenuChoice().equals("4"))
{
setUserSelected(0);
} //return to previus menu
else
{
System.out.println("Please select an option from the menu. (1 to 4)");
System.out.println("");
}
}
}
while (!getBorrowerMenuChoice().equals("4"));
}
/**
* Runs the they main Library menu through a switch statement. Each case of the switch statement corresponds to one option of the main Library Menu.
*/
public void startLibrary()
{
userListLibrary.readFile();
String mainMenuChoice;
do {
displayMainMenu();
setMainMenuChoice();
switch(mainMenu)
{
case "1":
userListLibrary.addNewUser();
break;
case "2":
startBorrowerMenu();
break;
case "3":
userListLibrary.displayBorrowers();
break;
case "4":
displayHelp();
break;
default:
if(getMainMenuChoice().equals("5"))
{
userListLibrary.writeFile();
System.out.println("Thanks visiting My Library!");
}
else
{
System.out.println("Please select an option from the menu. (1 to 5)");
System.out.println("");
}
}
}
while (!getMainMenuChoice().equals("5"));
}
/**
* Method selectUserManage
*
*/
public void selectUserManage()
{
int thisUser;
Scanner console = new Scanner(System.in);
try
{
System.out.println("These are the Users registered");
userListLibrary.displayManageBorrowers();
System.out.println("Please select the user you would like to manage it's books rentals");
thisUser = console.nextInt();
if ((thisUser -1) < userListLibrary.returnSize() && thisUser > 0)
setUserSelected(thisUser);
else
{
System.out.println("ERROR: Please try again and select one of the available users");
selectUserManage();
}
}
catch (Exception noInteger)
{
System.out.println("Please enter a number:");
System.out.println("");
selectUserManage();
}
}
/**
* This method assignates a value to the option of the manage borrowers menu. It will activate one of the options in the displayBorrowerMenu method.
*/
public void setBorrowerMenuChoice()
{
String borrowerMenuChoice;
Scanner console = new Scanner (System.in);
borrowerMenuChoice = console.nextLine();
borrowerMenu = borrowerMenuChoice;
}
/**
* This method assignates a value to the option of the menu. It will activate one of the options in the displayMainMenu method.
*/
public void setMainMenuChoice()
{
String mainMenuChoice;
Scanner console = new Scanner (System.in);
mainMenuChoice = console.nextLine();
mainMenu = mainMenuChoice;
}
/**
* This method assignates a value to the option of the menu. It will activate one of the options in the displayMainMenu method.
*/
public void setUserSelected(int newUserSelected)
{
userSelected = newUserSelected - 1;
}
}
import java.util.*;
/**
* User class will handle all the basic information about the users or borrowers of the book.
* This server class will provide the information to the UserList class.
*
* @author Daniel Toro
* @version (a version number or a date)
*/
public class User
{
private String name;
private int id;
private int age;
private String[] rentedBooks; // create 2 methods to add and remove from array
/**
* Default Constructor for objects of class User
*/
public User()
{
name = "";
id = 0;
age = 0;
rentedBooks = new String[2];
}
/**
* Accesor method that will return the age of the object of User's class
*
* @return age
*/
public int getAge()
{
return age;
}
public void addBook(int index, String newBook)
{
rentedBooks[index] = newBook;
}
public String getBook(int index)
{
return rentedBooks[index];
}
public void removeBook(int index)
{
rentedBooks[index] = null;
}
/**
* Accesor method that will return the id of the object of User's class
*
* @return id
*/
public int getId()
{
return id;
}
/**
* Accessor method that will return the name of the object of User's class
*
* @return name
*/
public String getName()
{
return name;
}
/**
* Mutator method that will set the age of the object of User's class
*/
public void setAge(int newAge)
{
age = newAge;
}
/**
* Mutator method that will set the id of the object of User's class
*/
public void setId(int newId)
{
id = newId;
}
/**
* Mutator method that will set the name of the object of User's class
*/
public void setName(String newName)
{
name = newName;
}
}
import java.io.*;
import java.util.*;
/**
* Write a description of class UserList here.
*
* @author Daniel Toro
* @student 27045382
* @version May/2016
*/
public class UserList
{
private ArrayList<User> userList;
private User newUser;
private User countUsers;
private String filename = "borrowers.txt";
/**
* Constructor for objects of class UserList
*/
public UserList()
{
userList = new ArrayList<User>();
newUser = new User();
countUsers = new User();
}
public void addNewUser()
{
User newUser = new User();
String newName;
int newAge = 0;
int newId = 0;
String emptyBookOne = "";
String emptyBookTwo = "";
boolean idAlreadyExist = false;
Scanner console = new Scanner(System.in);
while (newUser.getName().isEmpty() || !newUser.getName().matches("a-zA-Z_\\s"))
{
System.out.println("Please enter the name of new user: ");
newName = console.nextLine();
if(!newName.isEmpty())
{
newUser.setName(newName);
break;
}
else
System.out.println("Name can't be empty or can't be numbers or characters");
}
while (newAge < 5 || newAge > 110)
{
System.out.println("Please enter age of new user");
try
{
newAge = console.nextInt();
}
catch (Exception noInteger)
{
System.out.println("Enter a number for the Age of the user");
System.out.println("");
console.nextLine();
}
if (newAge >= 5 && newAge <= 110) {newUser.setAge(newAge);}
else{System.out.println("User Age must be between 5 and 110");}
}
while (newId < 1 || newId > 100 || idAlreadyExist == true)
{
idAlreadyExist = false;
System.out.println("Please enter ID of new user");
try
{
newId = console.nextInt();
// Check if the ID is between the established limits.
if (newId > 0 && newId <= 100)
newUser.setId(newId);
else{System.out.println("User ID must be between 1 and 100");}
// Search if the ID already exists
for (int count = 0; count < userList.size(); count++)
{
countUsers = userList.get(count);
if (countUsers.getId() == newId)
idAlreadyExist = true;
}
// print error if Id already exists
if (idAlreadyExist == false)
newUser.setId(newId);
else
{
System.out.println("ID already exists. Please try with other ID");
idAlreadyExist = true;
}
}
catch (Exception noInteger)
{
System.out.println("Enter a number for the ID of the user. Please try again.");
System.out.println("");
break;
}
}
userList.add(newUser);
}
public void assignBook(int thisUser, String thisBook)
{
countUsers = userList.get(thisUser);
if (countUsers.getBook(0) == null)
countUsers.addBook(0,thisBook);
else if (countUsers.getBook(1) == null)
countUsers.addBook(1,thisBook);
else if (!countUsers.getBook(0).isEmpty() && !countUsers.getBook(1).isEmpty())
System.out.println("User can only rent two books. Please return one book to rent a new one");
}
public void displayBorrowers()
{
System.out.println("inside displayBorrowers method, userList size is: " + userList.size());
for (int count = 0; count < userList.size(); count++)
{
countUsers = userList.get(count);
System.out.println("Name: " + countUsers.getName());
System.out.println("Age: " + countUsers.getAge());
System.out.println("ID: " + countUsers.getId());
if(countUsers.getBook(0) != null)
System.out.println("Book 1: " + countUsers.getBook(0));
if(countUsers.getBook(1) != null)
System.out.println("Book 2: " + countUsers.getBook(1));
System.out.println("");
}
}
public void displayBorrowedBooks(int thisUser)
{
countUsers = userList.get(thisUser);
if(countUsers.getBook(0) == null && countUsers.getBook(1) == null)
System.out.println("This user have not rented any book");
else if(countUsers.getBook(0) != null && countUsers.getBook(1) == null)
System.out.println("1) " + countUsers.getBook(0));
else if(countUsers.getBook(0).isEmpty() && !countUsers.getBook(1).isEmpty())
System.out.println("1) " + countUsers.getBook(1));
else if(!countUsers.getBook(0).isEmpty() && !countUsers.getBook(1).isEmpty())
{
System.out.println("1) " + countUsers.getBook(0));
System.out.println("2) " + countUsers.getBook(1));
}
System.out.println("");
}
public void displayManageBorrowers()
{
for(int count = 0; count < userList.size(); count++)
{
countUsers = userList.get(count);
System.out.println((count + 1 ) + ") " + countUsers.getName());
}
}
public void writeFile()
{
try
{
String filename = "borrowers.txt";
PrintWriter outputFile = new PrintWriter(filename);
for(int count = 0; count < userList.size(); count++)
{
countUsers = userList.get(count);
if (countUsers.getBook(0) == null && countUsers.getBook(1) == null)
outputFile.println(countUsers.getName() + "," + countUsers.getId() + "," + countUsers.getAge());
else if (countUsers.getBook(0) != null && countUsers.getBook(1) == null)
outputFile.println(countUsers.getName() + "," + countUsers.getId() + "," + countUsers.getAge() + "," + countUsers.getBook(0));
else if (countUsers.getBook(0) == null && countUsers.getBook(1) != null)
outputFile.println(countUsers.getName() + "," + countUsers.getId() + "," + countUsers.getAge() + "," + countUsers.getBook(1));
else if (countUsers.getBook(0) != null && countUsers.getBook(1) != null)
outputFile.println(countUsers.getName() + "," + countUsers.getId() + "," + countUsers.getAge() + "," + countUsers.getBook(0) + "," + countUsers.getBook(1));
}
outputFile.close();
}
catch (IOException e)
{
System.out.println("Somethig went wrong with accessing the file");
}
}
public void readFile()
{
try
{
String filename = "borrowers.txt";
FileReader inputFile = new FileReader(filename);
Scanner parser = new Scanner(inputFile);
while (parser.hasNextLine()){
User newUser = new User();
String readLine = parser.nextLine();
String [] userDetails = readLine.split(",");
String userName = userDetails[0];
newUser.setName(userName);
String userId = userDetails[1];
newUser.setId(Integer.parseInt(userId));
String userAge = userDetails[2];
newUser.setAge(Integer.parseInt(userAge));
if(userDetails.length > 5)
{
String userBookOne = userDetails[3];
String userAuthorOne = userDetails[4];
newUser.addBook(0,(userBookOne + "," + userAuthorOne));
String userBookTwo = userDetails[5];
String userAuthorTwo = userDetails[6];
newUser.addBook(1,(userBookTwo + "," + userAuthorTwo));
}
else if (userDetails.length > 3)
{
String userBookOne = userDetails[3];
String userAuthorOne = userDetails[4];
newUser.addBook(0,(userBookOne + "," + userAuthorOne));
}
userList.add(newUser);
}
inputFile.close();
}
catch (IOException ioException)
{
System.out.println("Unexpected I/O error occured");
}
}
public void removeBook(int thisUser, String thisBook) //CHECK THIS METHOD!!
{
countUsers = userList.get(thisUser);
if (countUsers.getBook(0).equals(thisBook))
countUsers.removeBook(0);
else if (countUsers.getBook(1).equals(thisBook))
countUsers.removeBook(1);
else if (countUsers.getBook(0) == null && countUsers.getBook(1) == null)
System.out.println("You have no books to return");
}
public int returnSize()
{
int size;
size = userList.size();
return size;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment