Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.usp.icmc.poo.TurmaA015.LibrarySystem;
- import br.usp.icmc.poo.TurmaA015.Rentable.*;
- import br.usp.icmc.poo.TurmaA015.User.*;
- import br.usp.icmc.poo.TurmaA015.Library.*;
- import java.io.*;
- import java.util.*;
- public class LibrarySystem {
- private Organizer library;
- private BufferedReader br;
- public static void main(String[] args) {
- LibrarySystem program = new LibrarySystem();
- program.start();
- }
- public void start(){
- System.out.println("System starting...");
- System.out.println("Select the date to start the system: ");
- br = new BufferedReader(new InputStreamReader(System.in));
- library = new Library();
- while(!readDate())
- System.out.println("Please enter a valid date. (xx/xx/xxxx)");
- System.out.println("Initializing library...");
- library.loadContent();
- String command = "";
- try {
- while(!command.equals("exit")){
- System.out.println("\n");
- command = br.readLine();
- String[] parts = command.split(" ");
- if(parts[0].equals("add")){
- if(parts.length > 1)
- commandAdd(parts);
- else
- System.out.println("Usage \"command add\": add <type> [book] [note] [student] [teacher] [community].");
- }
- else if(parts[0].equals("rent"))
- commandRent(parts);
- else if(parts[0].equals("refund"))
- commandRefund(parts);
- else if(parts[0].equals("show")){
- if(parts.length > 1)
- commandShow(parts);
- else
- System.out.println("Usage \"command show\": show <type> [users] [files].");
- }
- else if(command.equals("help"))
- help();
- else if(!command.equals("exit"))
- System.out.println("Unrecognized command. Try \"help\" to see available commands.");
- }
- System.out.println("\n\n");
- }
- catch(IOException e){
- System.out.println("Error trying to get user command.");
- }
- library.saveContent();
- }
- public boolean readDate(){
- try {
- String date;
- String[] numbers;
- date = br.readLine();
- numbers = date.split("/");
- if(numbers.length != 3)
- return false;
- library.setDate(Integer.parseInt(numbers[0]), Integer.parseInt(numbers[1]), Integer.parseInt(numbers[2]));
- return true;
- }
- catch(IOException e){
- System.out.println("Error trying to read date.");
- }
- return false;
- }
- public void commandAdd(String[] parts){
- try{
- if(parts[1].equals("book")){
- if (parts[2] == null){
- System.out.println("Please enter the name of the book you want to add: ");
- library.addFile(new Book(br.readLine()));
- System.out.println("\n");
- System.out.println("Added new book successfully.\n");
- }
- else{
- library.addFile(new Book(parts[2]));
- System.out.println("\n");
- System.out.println("Added new book successfully.\n");
- }
- }
- else if(parts[1].equals("note")){
- if (parts[2] == null){
- System.out.println("Please enter the name of the note you want to add: ");
- library.addFile(new Note(br.readLine()));
- System.out.println("\n");
- System.out.println("Added new note successfully.\n");
- }
- else {
- library.addFile(new Note(parts[2]));
- System.out.println("\n");
- System.out.println("Added new note successfully.\n");
- }
- }
- else if(parts[1].equals("student")){
- if (parts[2] == null){
- System.out.println("Please enter the name of the user you want to add: ");
- System.out.println("\n");
- if(library.addUser(new Student(br.readLine())))
- System.out.println("Added new user successfully.\n");
- else
- System.out.println("Theres already a student with this name !");
- }
- else{
- if(library.addUser(new Student(parts[2])))
- System.out.println("Added new user successfully.\n");
- else
- System.out.println("Theres already a student with this name !");
- }
- }
- else if(parts[1].equals("teacher")){
- if (parts[2] == null){
- System.out.println("Please enter the name of the user you want to add: ");
- System.out.println("\n");
- if(library.addUser(new Teacher(br.readLine())))
- System.out.println("Added new user successfully\n");
- else
- System.out.println("Theres already a teacher with this name !");
- }
- else{
- if(library.addUser(new Teacher(parts[2])))
- System.out.println("Added new user successfully\n");
- else
- System.out.println("Theres already a teacher with this name !");
- }
- }
- else if(parts[1].equals("community")){
- if (parts[2] == null){
- System.out.println("Please enter the name of the user you want to add: ");
- System.out.println("\n");
- if(library.addUser(new Community(br.readLine())))
- System.out.println("Added new user successfully.\n");
- else
- System.out.println("Theres already a community with this name !");
- }
- else{
- if(library.addUser(new Community(parts[2])))
- System.out.println("Added new user successfully.\n");
- else
- System.out.println("Theres already a community with this name !");
- }
- }
- else
- System.out.println("Usage \"command add\": add <type> [book] [note] [student] [teacher] [community].");
- System.out.println("\n");
- }
- catch(IOException e){
- System.out.println("Error trying to get user input.");
- }
- }
- public void commandRent(String[] parts){
- String fileName;
- String userName;
- try{
- if(parts[1] == null){
- System.out.println("Please enter the name of the archive: ");
- fileName = br.readLine();
- System.out.println("\n");
- }
- else{
- fileName = parts[1];
- }
- if(parts[2] == null){
- System.out.println("Please enter the name of the user: ");
- userName = br.readLine();
- System.out.println("\n");
- }
- else{
- userName = parts[2];
- }
- int rentResult = library.rentFile(userName, fileName);
- if(rentResult == -1)
- System.out.println("User " + userName + " not found.");
- else if(rentResult == -2)
- System.out.println("File " + fileName + " not found.");
- else if(rentResult == -3)
- System.out.println("The book " + fileName + " is already rented, and there are no copies available.");
- else if(rentResult == -4)
- System.out.println("User " + userName + " already has max number of rented files.");
- else if(rentResult == -5)
- System.out.println("User " + userName + " doesn't have permission to rent the file " + fileName + ".");
- else if(rentResult == -6)
- System.out.println("User " + userName + " cant rent the file " + fileName + " because he/she is banned.");
- else
- System.out.println("File rented !");
- System.out.println("\n");
- }
- catch(IOException e){
- System.out.println("Error trying to get user input.");
- }
- }
- public void commandRefund(String[] parts){
- try{
- System.out.println("Please enter the name of the archive: ");
- String fileName = br.readLine();
- System.out.println("\n");
- System.out.println("Please enter the name of the user: ");
- String userName = br.readLine();
- System.out.println("\n");
- int refundResult = library.refundFile(userName, fileName);
- if(refundResult == -1)
- System.out.println("User " + userName + " not found.");
- else if(refundResult == -2)
- System.out.println("File " + fileName + " not found.");
- else if(refundResult == -3)
- System.out.println("The user doesnt have this book.");
- else
- System.out.println("File refunded !");
- System.out.println("\n");
- }
- catch(IOException e){
- System.out.println("Error trying to get user input.");
- }
- }
- public void commandShow(String[] parts){
- System.out.println("\n-----------------------------------------------------------\n");
- if(parts[1].equals("users"))
- library.showUsers();
- else if(parts[1].equals("files"))
- library.showFiles();
- else if(parts[1].equals("rents"))
- library.showRents();
- else
- System.out.println("Unrecognized type. Supported types are [users] [files] [rents].");
- System.out.println("\n-----------------------------------------------------------\n");
- }
- public void help(){
- System.out.println("\n===========================================================\n");
- System.out.println("Library available commands: ");
- System.out.println("add <type> [book] [note] [student] [teacher] [community]");
- System.out.println("add <type> <name> [book] [note] [student] [teacher] [community]");
- System.out.println("rent");
- System.out.println("rent <file> [book] [note]");
- System.out.println("rent <file> <user> [book] [note] - [student] [teacher] [community]");
- System.out.println("refund");
- System.out.println("refund <file> [book] [note]");
- System.out.println("refund <file> <user> [book] [note] - [student] [teacher] [community]");
- System.out.println("show <type> [user] [files]");
- System.out.println("\n===========================================================\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement