Upload project files

This commit is contained in:
James Musselman 2025-01-16 19:37:13 -06:00
commit 9f8250ae15
Signed by: Musselman
GPG key ID: 1DAEFF35ECB5D6DB
20 changed files with 2936 additions and 0 deletions

View file

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.pawprintstudios</groupId>
<artifactId>LibraryManagementSystem</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>librarymanagementsystem.Client</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<artifactId>LibraryManagementClient</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.pawprintstudios</groupId>
<artifactId>LibraryManagementLibrary</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,492 @@
package librarymanagementsystem;
import java.io.*;
import java.net.Socket;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Client
*
* @author Pawprint Studios
*/
public class Client
{
static final int PORT = 51042;
static String userCommand = null;
static String command = null;
static String userType = null;
static String usersName = null;
static Long userId = null;
static boolean userLoggedIn = false;
static PrintWriter outToServer = null;
static Scanner inFromServer = null;
public static void main(String[] args)
{
int userInput = -1;
String searchedBook;
String borrowedBook;
String returnBook;
boolean exitLibrary = false;
Scanner scan = new Scanner(System.in);
System.out.println("*** Library Client ***\n");
System.out.println("Below are commands to use the library.");
System.out.println("Some commands are hidden as they require login.");
System.out.println("Input 0 to exit the client\n");
while (exitLibrary == false)
{
// print options for those not logged in
while (userLoggedIn == false)
{
System.out.println("Welcome Guest!");
System.out.println("\n1. Search The Library");
System.out.println("\n2. Apply For Membership");
System.out.println("\n3. Log in");
System.out.print("\nWhat would you like to do: ");
userInput = scan.nextInt();
while (userInput < 0 || userInput > 3)
{
System.out.println("Please Enter a valid option");
System.out.print("\nWhat would you like to do?: ");
userInput = scan.nextInt();
}
scan.nextLine();
switch (userInput)
{
case 0:
System.out.println("\nThanks for using the Library!");
System.exit(0);
case 1:
command = "search, ";
System.out.print("\nWhat would you like to search for: ");
searchedBook = scan.nextLine().trim();
userCommand = command + searchedBook;
break;
case 2:
command = "apply, ";
System.out.println("\nUsernames cannot have special"
+ " charecters or spaces.");
System.out.print("\nPlease enter your desired username: ");
String chosenUsername = scan.nextLine().trim() + ", ";
System.out.print("\nPlease enter your desired password: ");
String chosenPassword = scan.nextLine().trim() + ", ";
System.out.print("\nPlease enter your first and last "
+ "name: ");
String name = scan.nextLine().trim() + ", ";
System.out.print("\nPlease enter your prefered email " +
"address: ");
String preferedEmail = scan.nextLine().trim() + ", ";
System.out.print("\nPlease enter your prefered "
+ "phone number: ");
boolean badPhoneInput = true ;
String preferedPhoneNum = null;
while (badPhoneInput)
{
if (scan.hasNextLong())
{
badPhoneInput = false;
preferedPhoneNum = scan.nextLine().trim() + ", ";
}
else
{
System.out.println("Please enter Valid Phone "
+ "Number");
System.out.print("\nPlease enter your prefered "
+ "phone number: ");
preferedPhoneNum = scan.nextLine().trim() + ", ";
}
}
System.out.print("");
userCommand = command + chosenUsername + chosenPassword
+ name + preferedEmail + preferedPhoneNum;
break;
case 3:
command = "login, ";
System.out.println("Please enter your credentials.");
System.out.print("\nUsername: ");
String username = scan.next();
System.out.print("\nPassword: ");
String password = scan.next();
userCommand = command + username + ", " + password;
break;
default:
break;
}
getResults();
}
while (userLoggedIn && userType.equals("Patron"))
{
System.out.println("Welcome back, " + usersName + "!");
System.out.println("\n1. Search The Library");
System.out.println("\n2. Borrow Book");
System.out.println("\n3. View Loans");
System.out.println("\n4. Return Book");
System.out.println("\n5. Logout");
System.out.print("\nWhat would you like to do: ");
userInput = scan.nextInt();
while (userInput < 0 || userInput > 5)
{
System.out.println("Please Enter a valid option");
System.out.print("\nWhat would you like to do: ");
userInput = scan.nextInt();
}
scan.nextLine();
switch (userInput)
{
case 0:
System.out.println("\nThanks for using the Library!");
System.exit(0);
case 1:
command = "search, ";
System.out.print("\nWhat would you like to search for: ");
searchedBook = scan.nextLine().trim();
userCommand = command + searchedBook;
break;
case 2:
command = "borrow, ";
System.out.print("\nWhat book would you like to borrow "
+ "(use the Books ID Number): ");
borrowedBook = scan.nextLine().trim() + ", ";
userCommand = command + borrowedBook + userId;
break;
case 3:
command = "loans, ";
userCommand = command + userId;
break;
case 4:
command = "return, ";
System.out.print("\nWhat book would you like to return: ");
returnBook = scan.nextLine().trim() + ", ";
userCommand = command + returnBook + userId;
break;
case 5:
command = "logout, ";
userLoggedIn = false;
userType = null;
break;
}
getResults();
}
while (userLoggedIn && userType.equals("Librarian"))
{
System.out.println("Welcome back, " + usersName + "!");
System.out.println("\n1. Search The Library");
System.out.println("\n2. Add Book to Library");
System.out.println("\n3. Retire Book to Library");
System.out.println("\n4. Log out");
System.out.print("\nWhat would you like to do: ");
userInput = scan.nextInt();
while (userInput < 0 || userInput > 5)
{
System.out.println("Please Enter a valid option");
System.out.print("\nWhat would you like to do?: ");
userInput = scan.nextInt();
}
scan.nextLine();
switch (userInput)
{
case 0:
System.out.println("Thanks for using the Library!");
System.exit(0);
case 1:
command = "search, ";
System.out.print("\nWhat would you like to search for: ");
searchedBook = scan.next();
userCommand = command + searchedBook;
break;
case 2:
command = "addBook, ";
System.out.println("Please enter the book details.");
System.out.print("\nTitle: ");
String title = scan.nextLine().trim() + ", ";
System.out.print("\nAuthor: ");
String author = scan.nextLine().trim() + ", ";
System.out.print("\nYear released: ");
boolean badYearInput = true ;
String year = null;
while (badYearInput)
{
if (scan.hasNextInt())
{
badYearInput = false;
year = scan.nextLine().trim() + ", ";
}
else
{
System.out.println("Please enter Valid Year");
System.out.print("\nYear released: ");
year = scan.nextLine().trim() + ", ";
}
}
System.out.print("\nPublisher: ");
String publisher = scan.nextLine().trim() + ", ";
System.out.print("\nDate Published: ");
String publisherDate = scan.nextLine().trim() + ", ";
System.out.print("\nEdition: ");
boolean badEditionInput = true ;
String edition = null;
while (badEditionInput)
{
if (scan.hasNextInt())
{
badEditionInput = false;
edition = scan.nextLine().trim() + ", ";
}
else
{
System.out.println("Please enter a Valid Edition Number");
System.out.print("\nEdition: ");
edition = scan.nextLine().trim() + ", ";
}
}
System.out.print("\nISB Number: ");
boolean badISBNInput = true ;
String isbn = null;
while (badISBNInput)
{
if (scan.hasNextLong())
{
badISBNInput = false;
isbn = scan.nextLine().trim() + ", ";
}
else
{
System.out.println("Please enter a valid ISB Number");
System.out.print("\nISB Number: ");
isbn = scan.nextLine().trim() + ", ";
}
}
System.out.print("\nNumber of Copies the Library has: ");
boolean badCopiesInput = true ;
String numCopies = null;
while (badCopiesInput)
{
if (scan.hasNextInt())
{
int copies = scan.nextInt();
if (copies > 0)
{
badCopiesInput = false;
numCopies = copies + ", ";
}
else
{
System.out.println("Please enter a Valid Number of Copies");
System.out.print("\nNumber of Copies the Library has: ");
numCopies = scan.nextLine().trim() + ", ";
}
}
else
{
System.out.println("Please enter a Valid Number of Copies");
System.out.print("\nNumber of Copies the Library has: ");
numCopies = scan.nextLine().trim() + ", ";
}
}
scan.nextLine();
System.out.print("\nPrice of the Book: ");
boolean badPriceInput = true ;
String price = null;
while (badPriceInput)
{
if (scan.hasNextDouble())
{
double priceDouble = scan.nextDouble();
if (priceDouble > 0)
{
badPriceInput = false;
price = priceDouble + "";
}
else
{
System.out.println("Please enter a Valid Price");
System.out.print("\nPrice of the Book: ");
price = scan.nextLine().trim();
}
}
else
{
System.out.println("Please enter a Valid Price");
System.out.print("\nPrice of the Book: ");
price = scan.nextLine().trim();
}
}
scan.nextLine();
userCommand = command + title + author + year + publisher
+ publisherDate + edition + isbn + numCopies + price;
break;
case 3:
command = "retireBook, ";
System.out.print("\nWhat book would you like to retire "
+ "(use the ID Number): ");
String retiringBook = scan.nextLine().trim();
userCommand = command + retiringBook;
break;
case 4:
command = "logout, ";
userLoggedIn = false;
userType = null;
break;
}
getResults();
}
while (userLoggedIn && userType.equals("Admin"))
{
System.out.println("Welcome back, " + usersName + "!");
System.out.println("\n1. Search The Library");
System.out.println("\n2. Backup logs and database");
System.out.println("\n3. Remove a User");
System.out.println("\n4. Log out");
System.out.print("\nWhat would you like to do: ");
userInput = scan.nextInt();
while (userInput < 0 || userInput > 4)
{
System.out.println("Please enter a valid option");
System.out.print("\nWhat would you like to do: ");
userInput = scan.nextInt();
}
scan.nextLine();
switch (userInput)
{
case 0:
System.out.println("\nThanks for using the Library!");
System.exit(0);
case 1:
command = "search, ";
System.out.print("\nWhat would you like to search for: ");
searchedBook = scan.nextLine().trim();
userCommand = command + searchedBook;
break;
case 2:
command = "backup, ";
userCommand = command;
break;
case 3:
command = "removeUser, ";
System.out.print("\nWhat user would you like to remove "
+ "(Use ID Number): ");
String removedUser = scan.nextLine().trim();
userCommand = command + removedUser;
break;
case 4:
command = "logout, ";
userLoggedIn = false;
userType = null;
break;
}
getResults();
}
}
scan.close();
}
private static void getResults()
{
try
{
Socket clientToServer = new Socket("localhost", PORT);
outToServer = new PrintWriter(
clientToServer.getOutputStream());
inFromServer = new Scanner(
clientToServer.getInputStream());
outToServer.println(userCommand);
outToServer.flush();
String results = inFromServer.nextLine();
if (command.equals("search, ") && results.equals("Found"))
{
results = searchReturn(inFromServer);
}
else if (command.equals("loans, "))
{
results += searchReturn(inFromServer);
}
else if (command.equals("logout, "))
{
results = "Successfully logged out.";
}
System.out.println("\n\t\t\t+++Results From Library+++ "
+ "\n");
System.out.println(results);
System.out.println("\n\t\t\t+++--------------------+++ "
+ "\n");
if (command.equals("login, "))
{
userType = inFromServer.nextLine();
usersName = inFromServer.nextLine();
userId = inFromServer.nextLong();
userLoggedIn = true;
}
clientToServer.close();
}
catch (IOException e)
{
System.err.println(e);
}
catch (InputMismatchException e)
{
System.out.println("You must input a number.");
}
catch (NoSuchElementException e)
{
System.out.println(e);
}
}
/**
* Search the book database within server and prints results.
*
* @param inFromServer represents the results coming in from server.
* @return results
*/
private static String searchReturn(Scanner inFromServer)
{
int numResults = inFromServer.nextInt();
inFromServer.nextLine();
String results = "";
for (int i = 0; i < numResults; i++)
{
for (int j = 0; j < 5; j++)
{
results += "\n" + inFromServer.nextLine();
}
results += "\n";
}
return results;
}
}