Guest User

Untitled

a guest
Jan 12th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.04 KB | None | 0 0
  1. package lis;
  2.  
  3. import java.util.Iterator;
  4.  
  5. public class Demo {
  6.  
  7. static ListaSeqOrd<Aluno> listaAlunos = new ListaSeqOrd<Aluno>();
  8. static ListaSeqOrd<Livro> listaLivros = new ListaSeqOrd<Livro>();
  9. static ListaSeqOrd<Livro> listaLivrosEmprestados = new ListaSeqOrd<Livro>();
  10.  
  11. private static void returnLivro() {
  12. char resp;
  13. do {
  14. Keyboard.clrscr();
  15. int matAluno = Keyboard.readInt("Digite a matricula do aluno: ");
  16. Aluno aluno = new Aluno(matAluno);
  17. aluno = listaAlunos.retrieve(aluno);
  18. if (aluno == null) {
  19. System.out.println("Aluno n‹o existe! ");
  20. }
  21. if (aluno != null) {
  22. int codLivro = Keyboard.readInt("Digite o codigo do livro a ser devolvido: ");
  23. Livro livro = new Livro(codLivro);
  24. livro = listaLivrosEmprestados.retrieve(livro);
  25. if (livro == null) {
  26. System.out.println("Livro n‹o est‡ emprestado! ");
  27. } else {
  28. if (livro != null && livro.getAluno().compareTo(aluno) == 0) {
  29. if (listaLivrosEmprestados.remove(livro)) {
  30. livro.setAluno(null);
  31. System.out.println("Livro devolvido! ");
  32. } else {
  33. System.out.println("Livro n‹o pode ser devolvido! ");
  34. }
  35. } else {
  36. System.out.println("Livro n‹o est‡ emprestado para este aluno! ");
  37. }
  38. }
  39. }
  40. resp = Keyboard.readChar("Deseja devolver outro livro(s/n)? ");
  41. } while (resp == 's');
  42. }
  43.  
  44. static void lendLivro() {
  45. char resp;
  46. do {
  47. Keyboard.clrscr();
  48. int matAluno = Keyboard.readInt("Digite a matricula do aluno: ");
  49. Aluno aluno = new Aluno(matAluno);
  50. aluno = listaAlunos.retrieve(aluno);
  51. if (aluno == null) {
  52. System.out.println("Aluno nao existe");
  53. } else {
  54. int codLivro = Keyboard.readInt("Digite o codigo do livro: ");
  55. Livro livro = new Livro(codLivro);
  56. livro = listaLivros.retrieve(livro);
  57. if (livro == null) {
  58. System.out.println("Livro nao existe");
  59. } else {
  60. livro = listaLivros.retrieve(livro);
  61. if (livro != null) {
  62. if (livro.getAluno() == null) {
  63. livro.setAluno(aluno);
  64. listaLivrosEmprestados.add(livro);
  65. System.out.println("Emprestimo realizado!");
  66. } else {
  67. System.out.println("Livro ja emprestado!");
  68. }
  69. }
  70. }
  71. }
  72.  
  73. resp = Keyboard.readChar("Deseja pegar outro livro(s/n)? ");
  74. } while (resp == 's');
  75. }
  76.  
  77. private static void listLivro() {
  78. Keyboard.clrscr();
  79. System.out.println("Codigo do Livro Nome do Livro");
  80. System.out.println("--------------- -----------------------------");
  81. Livro livro;
  82. Iterator<Livro> it = listaLivros.iterator();
  83. while (it.hasNext()) {
  84. livro = it.next();
  85. System.out.printf(" %-8d %-1s\n", livro.getAluno(),
  86. livro.getNomeLivro());
  87. }
  88. Keyboard.waitEnter();
  89.  
  90. }
  91.  
  92. private static void listLivroEmprestado() {
  93. Keyboard.clrscr();
  94. System.out.println("Codigo do Livro Nome do Livro Aluno");
  95. System.out.println("--------------- ----------------------------- -----------------");
  96. Livro livro;
  97. Iterator<Livro> it = listaLivrosEmprestados.iterator();
  98. while (it.hasNext()) {
  99. livro = it.next();
  100. System.out.printf(" %-8d %-13s %-10s\n",
  101. livro.getCodLivro(), livro.getNomeLivro(), livro.getAluno().getNomeAluno());
  102. }
  103. Keyboard.waitEnter();
  104.  
  105. }
  106.  
  107. private static void searchLivro() {
  108. char resp;
  109. do {
  110. Keyboard.clrscr();
  111. int codLivro = Keyboard.readInt("Digite o codigo do livro: ");
  112. Livro livro = new Livro(codLivro);
  113. if (listaLivros.contains(livro)) {
  114. livro = listaLivros.retrieve(livro);
  115. if (livro == null) {
  116. System.out.println("Livro n‹o existe!");
  117. }
  118. if (livro != null) {
  119. System.out.println("Codigo do Livro Nome do Livro");
  120. System.out.println("--------------- -----------------------------");
  121. System.out.printf(" %-8d %-29s\n", livro.getAluno(),
  122. livro.getNomeLivro());
  123. System.out.println("");
  124. System.out.println("Alunos com livro:");
  125. Aluno aluno;
  126. Iterator<Aluno> it = listaAlunos.iterator();
  127. while (it.hasNext()) {
  128. aluno = it.next();
  129. if (aluno.getMatAluno() == livro.getAluno().getMatAluno()) {
  130. System.out.println("Matricula Aluno: "
  131. + aluno.getMatAluno() + " Nome do Aluno: "
  132. + aluno.getNomeAluno());
  133. }
  134. }
  135. }
  136. } else {
  137. System.out.println("Cod não existe!");
  138. }
  139. System.out.println("");
  140. resp = Keyboard.readChar("Deseja consultar outro livro(s/n)? ");
  141. } while (resp == 's');
  142.  
  143. }
  144.  
  145. private static void deleteLivro() {
  146. Keyboard.clrscr();
  147. int codLivro = Keyboard.readInt("Digite o livro a ser excluido: ");
  148. Livro livro = new Livro(codLivro);
  149. if (listaLivros.contains(livro)) {
  150. livro = listaLivros.retrieve(livro);
  151. if (livro == null) {
  152. System.out.println("Livro n‹o existe!");
  153. } else {
  154. if (listaLivrosEmprestados.contains(livro)) {
  155. System.out.println("Livro n‹o pode ser deletado pois ele esta emprestado no momento!");
  156. } else {
  157. if (livro != null) {
  158. System.out.println(livro.getNomeLivro());
  159. char resp = Keyboard.readChar("Deseja remover o livro(s/n)? ");
  160. if (resp == 's') {
  161. listaLivros.remove(livro);
  162. System.out.println("Livro removido com sucesso!");
  163. } else {
  164. System.out.println("Livro n‹o foi removido");
  165. }
  166. }
  167. }
  168. }
  169. } else {
  170. System.out.println("Cod não existe!");
  171. }
  172. Keyboard.waitEnter();
  173. }
  174.  
  175. private static void insertLivro() {
  176. char resp;
  177. do {
  178. Keyboard.clrscr();
  179. int codLivro = Keyboard.readInt("Digite o codigo do livro: ");
  180. Livro livro = new Livro(codLivro);
  181. if (listaLivros.contains(livro)) {
  182. System.out.println("Livro ja existe! ");
  183. } else {
  184. String nomeLivro = Keyboard.readString("Digite o nome do livro: ");
  185. livro.setNomeLivro(nomeLivro);
  186. listaLivros.add(livro);
  187. }
  188. resp = Keyboard.readChar("Deseja cadastrar outro livro(s/n)? ");
  189.  
  190. } while (resp == 's');
  191.  
  192. }
  193.  
  194. private static void listAluno() {
  195. Keyboard.clrscr();
  196. System.out.println("Numero da Matricula Nome do Aluno");
  197. System.out.println("------------------- -----------------------------");
  198. Aluno aluno;
  199. Iterator<Aluno> it = listaAlunos.iterator();
  200. while (it.hasNext()) {
  201. aluno = it.next();
  202. System.out.printf(" %-10d %-1s\n", aluno.getMatAluno(),
  203. aluno.getNomeAluno());
  204. }
  205. Keyboard.waitEnter();
  206.  
  207. }
  208.  
  209. private static void searchAluno() {
  210. char resp;
  211. do {
  212. Keyboard.clrscr();
  213. int matAluno = Keyboard.readInt("Digite o codigo do aluno: ");
  214. Aluno aluno = new Aluno(matAluno);
  215. if (listaAlunos.contains(aluno)) {
  216. aluno = listaAlunos.retrieve(aluno);
  217. if (aluno == null) {
  218. System.out.println("Aluno n‹o existe! ");
  219. }
  220. if (aluno != null) {
  221. System.out.println("Numero da Matricula Nome do Aluno");
  222. System.out.println("------------------- -----------------------------");
  223. System.out.printf("%9d %-1s\n", aluno.getMatAluno(),
  224. aluno.getNomeAluno());
  225. System.out.println("");
  226. System.out.println("Livros Emprestados:");
  227. System.out.println("Codigo do Livro Nome do Livro");
  228. System.out.println("--------------- -----------------------------");
  229. Livro livro;
  230. Iterator<Livro> it = listaLivrosEmprestados.iterator();
  231. while (it.hasNext()) {
  232. livro = it.next();
  233. System.out.printf(" %-8d %-1s\n",
  234. livro.getCodLivro(), livro.getNomeLivro());
  235. }
  236. }
  237. } else {
  238. System.out.println(" Matricula não existente!");
  239. }
  240. resp = Keyboard.readChar("Deseja consultar outro aluno(s/n)? ");
  241. } while (resp == 's');
  242. }
  243.  
  244. private static void deleteAluno() {
  245. Keyboard.clrscr();
  246. int matAluno = Keyboard.readInt("Digite a matricula a ser excluida: ");
  247. Aluno aluno = new Aluno(matAluno);
  248. if (listaAlunos.contains(aluno)) {
  249. aluno = listaAlunos.retrieve(aluno);
  250. if (aluno == null) {
  251. System.out.println("Aluno não existe!");
  252. }
  253. if (listaLivrosEmprestados.isEmpty() == false) {
  254. if (aluno != null) {
  255. System.out.println(aluno.getNomeAluno());
  256. char resp = Keyboard.readChar("Deseja remover o aluno(s/n)? ");
  257. if (resp == 's') {
  258. Livro livro;
  259. Iterator<Livro> it = listaLivros.iterator();
  260. while (it.hasNext()) {
  261. livro = it.next();
  262. if (aluno == livro.getAluno()) {
  263. System.out.println("Aluno não pode ser removido pois possui pendencia de livros!");
  264. } else {
  265. listaAlunos.remove(aluno);
  266. System.out.println("Aluno removido com sucesso!");
  267. }
  268. }
  269. } else {
  270. System.out.println("Aluno não foi removido");
  271. }
  272. }
  273. } else {
  274. if (aluno != null) {
  275. System.out.println(aluno.getNomeAluno());
  276. char resp = Keyboard.readChar("Deseja remover o aluno(s/n)? ");
  277. if (resp == 's') {
  278. listaAlunos.remove(aluno);
  279. System.out.println("Aluno removido com sucesso!");
  280. }
  281. }
  282. }
  283. } else {
  284. System.out.println("Não existe!");
  285. }
  286.  
  287. Keyboard.waitEnter();
  288.  
  289. }
  290.  
  291. private static void insertAluno() {
  292. char resp;
  293. do {
  294. Keyboard.clrscr();
  295. int matAluno = Keyboard.readInt("Digite a matricula do aluno: ");
  296. Aluno aluno = new Aluno(matAluno);
  297. if (listaAlunos.contains(aluno)) {
  298. System.out.println("Aluno ja existe");
  299. } else {
  300. String nomeAluno = Keyboard.readString("Digite o nome do aluno: ");
  301. aluno.setNomeAluno(nomeAluno);
  302. listaAlunos.add(aluno);
  303. }
  304. resp = Keyboard.readChar("Deseja cadastrar outro aluno(s/n)? ");
  305.  
  306. } while (resp == 's');
  307. }
  308.  
  309. public static void main(String[] args) {
  310. int opcao;
  311. do {
  312. Keyboard.clrscr();
  313. opcao = Keyboard.menu("Incluir Aluno/Excluir Aluno/Consultar Aluno/Listar Aluno/Incluir Livro/"
  314. + "Excluir Livro/Consultar Livro/Listar Livro/Listar Livros Emprestados/"
  315. + "Emprestar Livro/Devolver Livro/Terminar");
  316. switch (opcao) {
  317. case 1:
  318. insertAluno();
  319. break;
  320. case 2:
  321. deleteAluno();
  322. break;
  323. case 3:
  324. searchAluno();
  325. break;
  326. case 4:
  327. listAluno();
  328. break;
  329. case 5:
  330. insertLivro();
  331. break;
  332. case 6:
  333. deleteLivro();
  334. break;
  335. case 7:
  336. searchLivro();
  337. break;
  338. case 8:
  339. listLivro();
  340. break;
  341. case 9:
  342. listLivroEmprestado();
  343. break;
  344. case 10:
  345. lendLivro();
  346. break;
  347. case 11:
  348. returnLivro();
  349. break;
  350. }
  351.  
  352. } while (opcao < 12);
  353. System.out.println("Fim do programa");
  354. }
  355. }
Add Comment
Please, Sign In to add comment