Advertisement
Guest User

Untitled

a guest
May 25th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 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(command.equals("rent file"))
  55. commandRent(parts);
  56.  
  57. else if(command.equals("refund file"))
  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. try{
  201. System.out.println("Please enter the name of the archive: ");
  202. String fileName = br.readLine();
  203. System.out.println("\n");
  204.  
  205. System.out.println("Please enter the name of the user: ");
  206. String userName = br.readLine();
  207. System.out.println("\n");
  208.  
  209. int rentResult = library.rentFile(userName, fileName);
  210.  
  211. if(rentResult == -1)
  212. System.out.println("User " + userName + " not found.");
  213. else if(rentResult == -2)
  214. System.out.println("File " + fileName + " not found.");
  215. else if(rentResult == -3)
  216. System.out.println("The book " + fileName + " is already rented, and there are no copies available.");
  217. else if(rentResult == -4)
  218. System.out.println("User " + userName + " already has max number of rented files.");
  219. else if(rentResult == -5)
  220. System.out.println("User " + userName + " doesn't have permission to rent the file " + fileName + ".");
  221. else if(rentResult == -6)
  222. System.out.println("User " + userName + " cant rent the file " + fileName + " because he/she is banned.");
  223. else
  224. System.out.println("File rented !");
  225. System.out.println("\n");
  226. }
  227. catch(IOException e){
  228. System.out.println("Error trying to get user input.");
  229. }
  230. }
  231.  
  232. public void commandRefund(String[] parts){
  233. try{
  234. System.out.println("Please enter the name of the archive: ");
  235. String fileName = br.readLine();
  236. System.out.println("\n");
  237.  
  238. System.out.println("Please enter the name of the user: ");
  239. String userName = br.readLine();
  240. System.out.println("\n");
  241.  
  242. int refundResult = library.refundFile(userName, fileName);
  243.  
  244. if(refundResult == -1)
  245. System.out.println("User " + userName + " not found.");
  246. else if(refundResult == -2)
  247. System.out.println("File " + fileName + " not found.");
  248. else if(refundResult == -3)
  249. System.out.println("The user doesnt have this book.");
  250. else
  251. System.out.println("File refunded !");
  252. System.out.println("\n");
  253. }
  254. catch(IOException e){
  255. System.out.println("Error trying to get user input.");
  256. }
  257. }
  258.  
  259. public void commandShow(String[] parts){
  260. System.out.println("\n-----------------------------------------------------------\n");
  261. if(parts[1].equals("users"))
  262. library.showUsers();
  263. else if(parts[1].equals("files"))
  264. library.showFiles();
  265. else if(parts[1].equals("rents"))
  266. library.showRents();
  267. else
  268. System.out.println("Unrecognized type. Supported types are [users] [files] [rents].");
  269. System.out.println("\n-----------------------------------------------------------\n");
  270. }
  271.  
  272. public void help(){
  273. System.out.println("\n===========================================================\n");
  274. System.out.println("Library available commands: ");
  275. System.out.println("add <type> [book] [note] [student] [teacher] [community]");
  276. System.out.println("add <type> <name> [book] [note] [student] [teacher] [community]");
  277. System.out.println("rent file");
  278. //System.out.println("rent file <file> [book] [note]");
  279. System.out.println("refund file");
  280. //System.out.println("refund file <file> [book] [note]");
  281. System.out.println("show <type> [user] [files]");
  282. System.out.println("\n===========================================================\n");
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement