Upload project files
This commit is contained in:
commit
9f8250ae15
20 changed files with 2936 additions and 0 deletions
50
LibraryManagementServer/pom.xml
Normal file
50
LibraryManagementServer/pom.xml
Normal 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.Server</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<artifactId>LibraryManagementServer</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>
|
|
@ -0,0 +1,230 @@
|
|||
package librarymanagementsystem;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Represents the server.
|
||||
*
|
||||
* @author Pawprint Studios
|
||||
*/
|
||||
public class Server
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
final int PORT = 51042;
|
||||
System.out.println("***Library Server***");
|
||||
Library serverLibrary = new Library();
|
||||
|
||||
try
|
||||
{
|
||||
ServerSocket welcomeSocket = new ServerSocket(PORT);
|
||||
|
||||
System.out.printf("Listening on port %d...\n", PORT);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Socket connectionSocket = welcomeSocket.accept();
|
||||
System.out.println("Client connected...");
|
||||
|
||||
Scanner inFromClient = new Scanner(
|
||||
connectionSocket.getInputStream());
|
||||
PrintWriter outToClient = new PrintWriter(
|
||||
connectionSocket.getOutputStream());
|
||||
|
||||
String clientRequest = inFromClient.nextLine();
|
||||
|
||||
// James: Here we would split up the input to do the command with the data.
|
||||
System.out.println(clientRequest);
|
||||
// Parse clientRequest
|
||||
String[] userInput = clientRequest.split(", ");
|
||||
|
||||
|
||||
switch (userInput[0])
|
||||
{
|
||||
case "search":
|
||||
ArrayList<Book> matchedBook = serverLibrary.searchLibrary(
|
||||
userInput[1]);
|
||||
System.out.println("Server is searching for: "
|
||||
+ userInput[1]);
|
||||
if (!matchedBook.isEmpty())
|
||||
{
|
||||
System.out.println("Found " + matchedBook.size()
|
||||
+ " Books");
|
||||
outToClient.println("Found");
|
||||
outToClient.println(matchedBook.size());
|
||||
for (Book searchedBook : matchedBook)
|
||||
{
|
||||
System.out.println("Found: " + searchedBook.printBook());
|
||||
outToClient.print(searchedBook.printBook());
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Could not find: " + userInput[1]);
|
||||
outToClient.println("Could not find any matching "
|
||||
+ "titles for the query: " + userInput[1]);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case "apply":
|
||||
String desiredUsername = userInput[1];
|
||||
String desiredPassword = userInput[2];
|
||||
String desiredName = userInput[3];
|
||||
String desiredEmail = userInput[4];
|
||||
long desiredPhoneNumber = Long.parseLong(userInput[5]);
|
||||
boolean creation = serverLibrary.applyMembership(
|
||||
desiredUsername, desiredPassword, desiredName,
|
||||
desiredEmail, desiredPhoneNumber);
|
||||
if (creation)
|
||||
{
|
||||
outToClient.println("Application succeeded!");
|
||||
}
|
||||
else
|
||||
{
|
||||
outToClient.println("A user with that name "
|
||||
+ "already exists.\nPlease try again "
|
||||
+ "with annother name.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "login":
|
||||
String username = userInput[1];
|
||||
String password = userInput[2];
|
||||
System.out.println("Verify User: "
|
||||
+ username );
|
||||
GeneralUser user = serverLibrary.verifyLogin(
|
||||
username, password);
|
||||
if (user != null)
|
||||
{
|
||||
outToClient.println("Successfuly Logged in!");
|
||||
outToClient.println(user.getUserType());
|
||||
outToClient.println(user.getPatronName());
|
||||
outToClient.println(user.getUserId());
|
||||
}
|
||||
else
|
||||
{
|
||||
outToClient.println("Invalid username or password.");
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case "borrow":
|
||||
Book searchBook = serverLibrary.findBook(
|
||||
Long.parseLong(userInput[1]));
|
||||
long userId = Long.parseLong(userInput[2]);
|
||||
String borrowSuccess = serverLibrary
|
||||
.borrowBook(userId, searchBook);
|
||||
|
||||
if (borrowSuccess.equals("Borrowed"))
|
||||
{
|
||||
System.out.println(searchBook.getTitle()
|
||||
+ " was added to your shelf!");
|
||||
outToClient.println(searchBook.getTitle()
|
||||
+ " was added to your shelf!");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println(borrowSuccess);
|
||||
outToClient.println(borrowSuccess);
|
||||
}
|
||||
break;
|
||||
|
||||
case "loans":
|
||||
userId = Long.parseLong(userInput[1]);
|
||||
outToClient.println("Your Shelf:");
|
||||
|
||||
outToClient.println(serverLibrary.findUser(userId)
|
||||
.getNumLoans());
|
||||
|
||||
ArrayList<Book> loanedBooks = serverLibrary.viewLoans(
|
||||
userId);
|
||||
for (Book loan : loanedBooks)
|
||||
{
|
||||
System.out.print(loan.printLoan());
|
||||
outToClient.print(loan.printLoan());
|
||||
}
|
||||
break;
|
||||
|
||||
case "return":
|
||||
long bookId = Long.parseLong(userInput[1]);
|
||||
userId = Long.parseLong(userInput[2]);
|
||||
String returnSuccess = serverLibrary
|
||||
.returnBook(userId, bookId);
|
||||
Book returnBook = serverLibrary.findBook(bookId);
|
||||
|
||||
if (returnSuccess.equals("Returned"))
|
||||
{
|
||||
System.out.println(returnBook.getTitle()
|
||||
+ " was removed from your shelf!");
|
||||
outToClient.println(returnBook.getTitle()
|
||||
+ " was removed from your shelf!");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println(returnSuccess);
|
||||
outToClient.println(returnSuccess);
|
||||
}
|
||||
break;
|
||||
|
||||
case "addBook":
|
||||
String title = userInput[1];
|
||||
String author = userInput[2];
|
||||
int year = Integer.parseInt(userInput[3]);
|
||||
String publisher = userInput[4];
|
||||
String publisherDate = userInput[5];
|
||||
int edition = Integer.parseInt(userInput[6]);
|
||||
long isbn = Long.parseLong(userInput[7]);
|
||||
int numCopies = Integer.parseInt(userInput[8]);
|
||||
double price = Double.parseDouble(userInput[9]);
|
||||
ArrayList<Long> currentHolders = new ArrayList<>(1);
|
||||
|
||||
serverLibrary.addBook(new Book(title, author, year,
|
||||
publisher, publisherDate, edition, isbn, numCopies,
|
||||
0, serverLibrary.getHighestBookID(), price,
|
||||
currentHolders));
|
||||
|
||||
outToClient.println(userInput[1]
|
||||
+ " was added to Library!");
|
||||
break;
|
||||
case "retireBook":
|
||||
long retiringBook = Long.parseLong(userInput[1]);
|
||||
serverLibrary.retireBook(retiringBook);
|
||||
outToClient.println("Successfully retired book!");
|
||||
break;
|
||||
|
||||
case "backup":
|
||||
serverLibrary.backupDatabase();
|
||||
outToClient.println("Successfully backed up database!");
|
||||
break;
|
||||
|
||||
case "removeUser":
|
||||
long removedUser = Long.parseLong(userInput[1]);
|
||||
String usersName = serverLibrary.findUser(removedUser)
|
||||
.getPatronName();
|
||||
serverLibrary.removeUser(removedUser);
|
||||
outToClient.println(usersName
|
||||
+ " was removed form Library.");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Send info to client
|
||||
|
||||
outToClient.flush();
|
||||
connectionSocket.close();
|
||||
}
|
||||
|
||||
} catch (IOException e)
|
||||
{
|
||||
System.err.println(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package librarymanagementsystem;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
/**
|
||||
* Represents a ServerTask.
|
||||
* @author James
|
||||
*/
|
||||
|
||||
public class ServerTask implements Runnable
|
||||
{
|
||||
private Socket socket;
|
||||
private int clientNumber;
|
||||
|
||||
public ServerTask(int clientNumber, Socket socket)
|
||||
{
|
||||
this.socket = socket;
|
||||
this.clientNumber = clientNumber;
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
InetAddress clientAddress = socket.getInetAddress();
|
||||
System.out.println("Client " + clientNumber
|
||||
+ "'s IP address is "
|
||||
+ clientAddress.getHostAddress());
|
||||
|
||||
PrintWriter outToClient = new PrintWriter(
|
||||
socket.getOutputStream());
|
||||
Scanner inFromClient = new Scanner(
|
||||
socket.getInputStream());
|
||||
System.out.println("Are we setting up da pipes?");
|
||||
|
||||
String tmpMsg = "The ServerTask does only this currently.";
|
||||
outToClient.println(tmpMsg);
|
||||
outToClient.flush();
|
||||
socket.close();
|
||||
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.err.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue