Advertisement
Guest User

Untitled

a guest
May 25th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.75 KB | None | 0 0
  1.  
  2. package br.usp.icmc.poo.TurmaA015.LibrarySystem;
  3.  
  4. import br.usp.icmc.poo.TurmaA015.Rentable.*;
  5. import br.usp.icmc.poo.TurmaA015.User.*;
  6. import br.usp.icmc.poo.TurmaA015.Library.*;
  7.  
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11. public class LibrarySystem {
  12. private Organizer library;
  13. private BufferedReader br;
  14.  
  15. public static void main(String[] args) {
  16. LibrarySystem program = new LibrarySystem();
  17. program.start();
  18. }
  19.  
  20. public void start(){
  21.  
  22. System.out.println("System starting...");
  23. System.out.println("Select the date to start the system: ");
  24.  
  25. br = new BufferedReader(new InputStreamReader(System.in));
  26.  
  27. library = new Library();
  28.  
  29. while(!readDate())
  30. System.out.println("Please enter a valid date. (xx/xx/xxxx)");
  31.  
  32. System.out.println("Initializing library...");
  33. library.loadContent();
  34.  
  35. String command = "";
  36.  
  37. try {
  38.  
  39. while(!command.equals("exit")){
  40. System.out.println("\n");
  41. command = br.readLine();
  42.  
  43. String[] parts = command.split(" ");
  44.  
  45. if(parts[0].equals("add")){
  46.  
  47. if(parts.length > 1)
  48. commandAdd(parts);
  49. else
  50. System.out.println("Usage \"command add\": add <type> [book] [note] [student] [teacher] [community].");
  51.  
  52. }
  53.  
  54. else if(parts[0].equals("rent"))
  55. commandRent(parts);
  56.  
  57. else if(parts[0].equals("refund"))
  58. commandRefund(parts);
  59.  
  60. else if(parts[0].equals("show")){
  61.  
  62. if(parts.length > 1)
  63. commandShow(parts);
  64.  
  65. else
  66. System.out.println("Usage \"command show\": show <type> [users] [files].");
  67.  
  68. }
  69. else if(command.equals("help"))
  70. help();
  71.  
  72. else if(!command.equals("exit"))
  73. System.out.println("Unrecognized command. Try \"help\" to see available commands.");
  74.  
  75. }
  76.  
  77.  
  78. System.out.println("\n\n");
  79.  
  80. }
  81. catch(IOException e){
  82. System.out.println("Error trying to get user command.");
  83. }
  84.  
  85. library.saveContent();
  86. }
  87.  
  88. public boolean readDate(){
  89. try {
  90. String date;
  91. String[] numbers;
  92.  
  93. date = br.readLine();
  94. numbers = date.split("/");
  95.  
  96. if(numbers.length != 3)
  97. return false;
  98.  
  99. library.setDate(Integer.parseInt(numbers[0]), Integer.parseInt(numbers[1]), Integer.parseInt(numbers[2]));
  100.  
  101. return true;
  102.  
  103. }
  104. catch(IOException e){
  105. System.out.println("Error trying to read date.");
  106. }
  107.  
  108. return false;
  109. }
  110.  
  111. public void commandAdd(String[] parts){
  112. try{
  113. if(parts[1].equals("book")){
  114. if (parts[2] == null){
  115. System.out.println("Please enter the name of the book you want to add: ");
  116. library.addFile(new Book(br.readLine()));
  117. System.out.println("\n");
  118. System.out.println("Added new book successfully.\n");
  119. }
  120. else{
  121. library.addFile(new Book(parts[2]));
  122. System.out.println("\n");
  123. System.out.println("Added new book successfully.\n");
  124. }
  125. }
  126. else if(parts[1].equals("note")){
  127. if (parts[2] == null){
  128. System.out.println("Please enter the name of the note you want to add: ");
  129. library.addFile(new Note(br.readLine()));
  130. System.out.println("\n");
  131. System.out.println("Added new note successfully.\n");
  132. }
  133. else {
  134.  
  135. library.addFile(new Note(parts[2]));
  136. System.out.println("\n");
  137. System.out.println("Added new note successfully.\n");
  138. }
  139. }
  140. else if(parts[1].equals("student")){
  141. if (parts[2] == null){
  142. System.out.println("Please enter the name of the user you want to add: ");
  143. System.out.println("\n");
  144. if(library.addUser(new Student(br.readLine())))
  145. System.out.println("Added new user successfully.\n");
  146. else
  147. System.out.println("Theres already a student with this name !");
  148. }
  149. else{
  150. if(library.addUser(new Student(parts[2])))
  151. System.out.println("Added new user successfully.\n");
  152. else
  153. System.out.println("Theres already a student with this name !");
  154. }
  155.  
  156. }
  157. else if(parts[1].equals("teacher")){
  158. if (parts[2] == null){
  159. System.out.println("Please enter the name of the user you want to add: ");
  160. System.out.println("\n");
  161. if(library.addUser(new Teacher(br.readLine())))
  162. System.out.println("Added new user successfully\n");
  163. else
  164. System.out.println("Theres already a teacher with this name !");
  165. }
  166. else{
  167. if(library.addUser(new Teacher(parts[2])))
  168. System.out.println("Added new user successfully\n");
  169. else
  170. System.out.println("Theres already a teacher with this name !");
  171. }
  172.  
  173. }
  174. else if(parts[1].equals("community")){
  175. if (parts[2] == null){
  176. System.out.println("Please enter the name of the user you want to add: ");
  177. System.out.println("\n");
  178. if(library.addUser(new Community(br.readLine())))
  179. System.out.println("Added new user successfully.\n");
  180. else
  181. System.out.println("Theres already a community with this name !");
  182. }
  183. else{
  184. if(library.addUser(new Community(parts[2])))
  185. System.out.println("Added new user successfully.\n");
  186. else
  187. System.out.println("Theres already a community with this name !");
  188. }
  189. }
  190. else
  191. System.out.println("Usage \"command add\": add <type> [book] [note] [student] [teacher] [community].");
  192. System.out.println("\n");
  193. }
  194. catch(IOException e){
  195. System.out.println("Error trying to get user input.");
  196. }
  197. }
  198.  
  199. public void commandRent(String[] parts){
  200.  
  201. String fileName;
  202. String userName;
  203.  
  204. try{
  205.  
  206. if(parts[1] == null){
  207. System.out.println("Please enter the name of the archive: ");
  208. fileName = br.readLine();
  209. System.out.println("\n");
  210. }
  211. else{
  212. fileName = parts[1];
  213. }
  214. if(parts[2] == null){
  215. System.out.println("Please enter the name of the user: ");
  216. userName = br.readLine();
  217. System.out.println("\n");
  218. }
  219. else{
  220. userName = parts[2];
  221. }
  222.  
  223. int rentResult = library.rentFile(userName, fileName);
  224.  
  225. if(rentResult == -1)
  226. System.out.println("User " + userName + " not found.");
  227. else if(rentResult == -2)
  228. System.out.println("File " + fileName + " not found.");
  229. else if(rentResult == -3)
  230. System.out.println("The book " + fileName + " is already rented, and there are no copies available.");
  231. else if(rentResult == -4)
  232. System.out.println("User " + userName + " already has max number of rented files.");
  233. else if(rentResult == -5)
  234. System.out.println("User " + userName + " doesn't have permission to rent the file " + fileName + ".");
  235. else if(rentResult == -6)
  236. System.out.println("User " + userName + " cant rent the file " + fileName + " because he/she is banned.");
  237. else
  238. System.out.println("File rented !");
  239. System.out.println("\n");
  240. }
  241. catch(IOException e){
  242. System.out.println("Error trying to get user input.");
  243. }
  244. }
  245.  
  246. public void commandRefund(String[] parts){
  247. try{
  248. System.out.println("Please enter the name of the archive: ");
  249. String fileName = br.readLine();
  250. System.out.println("\n");
  251.  
  252. System.out.println("Please enter the name of the user: ");
  253. String userName = br.readLine();
  254. System.out.println("\n");
  255.  
  256. int refundResult = library.refundFile(userName, fileName);
  257.  
  258. if(refundResult == -1)
  259. System.out.println("User " + userName + " not found.");
  260. else if(refundResult == -2)
  261. System.out.println("File " + fileName + " not found.");
  262. else if(refundResult == -3)
  263. System.out.println("The user doesnt have this book.");
  264. else
  265. System.out.println("File refunded !");
  266. System.out.println("\n");
  267. }
  268. catch(IOException e){
  269. System.out.println("Error trying to get user input.");
  270. }
  271. }
  272.  
  273. public void commandShow(String[] parts){
  274. System.out.println("\n-----------------------------------------------------------\n");
  275. if(parts[1].equals("users"))
  276. library.showUsers();
  277. else if(parts[1].equals("files"))
  278. library.showFiles();
  279. else if(parts[1].equals("rents"))
  280. library.showRents();
  281. else
  282. System.out.println("Unrecognized type. Supported types are [users] [files] [rents].");
  283. System.out.println("\n-----------------------------------------------------------\n");
  284. }
  285.  
  286. public void help(){
  287. System.out.println("\n===========================================================\n");
  288. System.out.println("Library available commands: ");
  289. System.out.println("add <type> [book] [note] [student] [teacher] [community]");
  290. System.out.println("add <type> <name> [book] [note] [student] [teacher] [community]");
  291. System.out.println("rent");
  292. System.out.println("rent <file> [book] [note]");
  293. System.out.println("rent <file> <user> [book] [note] - [student] [teacher] [community]");
  294. System.out.println("refund");
  295. System.out.println("refund <file> [book] [note]");
  296. System.out.println("refund <file> <user> [book] [note] - [student] [teacher] [community]");
  297. System.out.println("show <type> [user] [files]");
  298. System.out.println("\n===========================================================\n");
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement