Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.33 KB | None | 0 0
  1. package com.company;
  2. import java.io.IOException;
  3. import java.net.MalformedURLException;
  4. import java.rmi.Naming;
  5. import java.rmi.NotBoundException;
  6. import java.rmi.RemoteException;
  7.  
  8.  
  9. import java.rmi.server.UnicastRemoteObject;
  10. import java.sql.Connection;
  11. import java.sql.SQLException;
  12. import java.text.ParseException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.Scanner;
  17.  
  18. /**
  19. *
  20. */
  21. public class RMIClient extends UnicastRemoteObject implements InterfaceClient {
  22. protected RMIClient() throws RemoteException {
  23. super();
  24. }
  25. @Override
  26. public void printOnClient(String s) throws RemoteException {
  27. System.out.println("> " + s);
  28. }
  29.  
  30.  
  31.  
  32. public static void main(String[] args) throws IOException, NotBoundException, SQLException {
  33. InterfaceServer i = (InterfaceServer) Naming.lookup("infoMusicRegistry");
  34. RMIClient client = new RMIClient();
  35. //i.subscribe("cliente1", (InterfaceClient) c);
  36.  
  37. boolean enter = true;
  38. while(enter){
  39. String user = enterTheProgram(i);
  40. if (user != null){
  41. enter = menu(i, user, client);
  42. }
  43. else{
  44. System.out.println("thank you. we hope to see you again!");
  45. return;
  46. }
  47. }
  48. System.out.println("thank you. we hope to see you again!");
  49. }
  50.  
  51. public static String enterTheProgram(InterfaceServer i) throws RemoteException, MalformedURLException, NotBoundException {
  52. while (true) {
  53. System.out.println("menu: (type one of the options)");
  54. System.out.println("1. login");
  55. System.out.println("2. register");
  56. System.out.println("3. exit");
  57.  
  58. Scanner keyboard = new Scanner(System.in);
  59. int choice = keyboard.nextInt();
  60.  
  61. switch (choice) {
  62. case 1:
  63. System.out.println("type your username:");
  64. keyboard = new Scanner(System.in);
  65. String username = keyboard.nextLine();
  66. System.out.println("type your password:");
  67. keyboard = new Scanner(System.in);
  68. String password = keyboard.nextLine();
  69.  
  70. //if login succeeds
  71. if (i.loginOrRegister(username, password, false)) {
  72. //tbc
  73. System.out.println("login successful");
  74. return username;
  75. } else {
  76. //tbc
  77. System.out.println("wrong credentials, make sure to register first!");
  78. }
  79. break;
  80. case 2:
  81. System.out.println("type your username:");
  82. keyboard = new Scanner(System.in);
  83. username = keyboard.nextLine();
  84. //check if username exists in database
  85. System.out.println("type your password:");
  86. keyboard = new Scanner(System.in);
  87. password = keyboard.nextLine();
  88. //check if password is certain by asking two times
  89. if (i.loginOrRegister(username, password, true)) {
  90. System.out.println(username + " registed. please login now");
  91.  
  92. } else {
  93. System.out.println("someone already has that username!");
  94. }
  95. break;
  96. case 3:
  97. System.out.println("exiting...");
  98. System.exit(0);
  99. default:
  100. System.out.println("please enter valid option");
  101. }
  102. }
  103. }
  104.  
  105. public static boolean menu(InterfaceServer i, String username, InterfaceClient iClient) throws IOException, SQLException {
  106. Connection c = SQL.enterDatabase("infomusic");
  107. System.out.println("welcome username! what you want to do?");
  108.  
  109. //interface
  110. i.subscribe((InterfaceClient) iClient, username);
  111.  
  112. while (true) {
  113. System.out.println("menu: (type one of the options)");
  114. System.out.println("1. search for songs");
  115. System.out.println("2. search for some detail information about an artist or a specific album ");
  116. System.out.println("3. write a review to an album");
  117. System.out.println("4. upload/download a song");
  118. System.out.println("5. manage operations (admins only)"); //Só pode aparecer ao admin
  119. System.out.println("6. logout");
  120. System.out.println("7. exit");
  121. //System.out.println("7. Create a DataBase for Songs"); //para apagar, apenas para testar criar uma base de dados
  122. //System.out.println("8. Enter a song in the DataBase"); //para apagar, apenas para testar inserir uma musica base de dados
  123.  
  124. Scanner keyboard = new Scanner(System.in);
  125. int choice = keyboard.nextInt();
  126.  
  127. switch (choice) {
  128. case 1:
  129. // Search for Songs;
  130. break;
  131. case 2:
  132. //Search for some detail information about an artist
  133. System.out.println("what operation you want to do? (type one of the options)");
  134. System.out.println("1. search about an album");
  135. System.out.println("2. search about an artist");
  136. keyboard = new Scanner(System.in);
  137. int choice4 = keyboard.nextInt();
  138.  
  139. switch (choice4) {
  140. case 1:
  141. //alterar para enviar pelo protocolo
  142. SQL.printAllTable(c, "albums");
  143.  
  144.  
  145. System.out.println("type the album id u want to know more about");
  146. keyboard = new Scanner(System.in);
  147. int albumToSearch = keyboard.nextInt();
  148. String albumDetail = i.searchDetailAboutAlbum(albumToSearch);
  149. if(albumDetail != null) {
  150. System.out.println(albumDetail);
  151. }
  152. break;
  153. case 2:
  154. //alterar para enviar pelo protocolo
  155. SQL.printAllTable(c, "artists");
  156. System.out.println("type the album id u want to know more about");
  157. keyboard = new Scanner(System.in);
  158. int artistToSearch = keyboard.nextInt();
  159. String artistDetail = i.searchDetailAboutArtist(artistToSearch);
  160. if(artistDetail != null) {
  161. System.out.println(artistDetail);
  162. }
  163. break;
  164.  
  165. default:
  166. System.out.println("please enter valid option");
  167. }
  168. // a specific album
  169. break;
  170. case 3:
  171. //write a review to an album
  172.  
  173. //alterar para enviar pelo protocolo
  174. SQL.printAllTable(c, "albums");
  175. System.out.println("select the ID of the album you want to review");
  176. keyboard = new Scanner(System.in);
  177. int albumToReviewID = keyboard.nextInt();
  178. System.out.println("rating: (0 to 10)");
  179. keyboard = new Scanner(System.in);
  180. int albumRating = keyboard.nextInt();
  181. System.out.println("review: (max 300 char)");
  182. keyboard = new Scanner(System.in);
  183. String albumReview = keyboard.nextLine();
  184. if(albumRating < 11 && albumRating >= 0 && albumReview.length() < 301) {
  185. i.writeAlbumReview(albumToReviewID, albumRating, albumReview);
  186. }
  187. else {
  188. System.out.println("rating must be between 0 and 10 and review have 300 char limit");
  189. }
  190. break;
  191. case 4:
  192. System.out.println("which operation you want to do?");
  193. System.out.println("1. upload");
  194. System.out.println("2. download");
  195. keyboard = new Scanner(System.in);
  196. int choice5 = keyboard.nextInt();
  197. switch(choice5){
  198. case 1:
  199.  
  200. //alterar para enviar pelo protocolo
  201. SQL.printAllTable(c,"musics");
  202. //ConnectionFunctions.uploadMusicTCP("C:\\Users\\zmcdo\\Documents\\music.mp3", false, 1, "rita");
  203.  
  204. System.out.println("select the music's ID u want to upload");
  205. keyboard = new Scanner(System.in);
  206. int musicID = keyboard.nextInt();
  207. System.out.println("type the music location: ");
  208. keyboard = new Scanner(System.in);
  209. String location = keyboard.nextLine();
  210. break;
  211. case 2:
  212.  
  213. //alterar para enviar pelo protocolo
  214. SQL.printAllTable(c,"albums");
  215.  
  216. System.out.println("select the ID you want to change the name");
  217. keyboard = new Scanner(System.in);
  218. int albumID = keyboard.nextInt();
  219. System.out.println("type the new name: ");
  220. keyboard = new Scanner(System.in);
  221. String albumNewName = keyboard.nextLine();
  222.  
  223. break;
  224. default:
  225. System.out.println("please enter valid option");
  226. }
  227. /*HashMap<String, String> map = new HashMap<>();
  228. map.put("type", "upload");
  229. String s;
  230. Scanner sc = new Scanner(System.in);
  231. System.out.println("enter the path of the music you would like upload");
  232. s = sc.nextLine();
  233. // O user tem de meter da maneira correta (com \\)
  234. map.put("filePath", s);
  235. new Threads(map);*/
  236. break;
  237. case 5:
  238. //manage data
  239. if(i.checkIfUserIsAdmin(username)) {//check if user is admin
  240. System.out.println("what operation you want to do? (type one of the options)");
  241. System.out.println("1. add music/album/artist");
  242. System.out.println("2. change artist, album or music name");
  243. System.out.println("3. grant admin to user");
  244. System.out.println("4. add picture to an album");
  245. System.out.println("5. upload song lyrics");
  246. keyboard = new Scanner(System.in);
  247. int choice2 = keyboard.nextInt();
  248. switch(choice2) {
  249. case 1:
  250. System.out.println("which data you want to add? (type one of the options)");
  251. System.out.println("1. music");
  252. System.out.println("2. album");
  253. System.out.println("3. artist");
  254. keyboard = new Scanner(System.in);
  255. int choice6 = keyboard.nextInt();
  256. switch(choice6) {
  257. case 1:
  258. System.out.println("music's name: ");
  259. keyboard = new Scanner(System.in);
  260. String musicName = keyboard.nextLine();
  261. System.out.println("description: ");
  262. keyboard = new Scanner(System.in);
  263. String description = keyboard.nextLine();
  264. if(description.length() == 0) {
  265. description = "no description";
  266. }
  267. System.out.println("what is the duration: (in seconds)");
  268. keyboard = new Scanner(System.in);
  269. int duration = keyboard.nextInt();
  270. SQL.printAllTable(c, "albums");
  271. System.out.println("type the album's ID: ");
  272. keyboard = new Scanner(System.in);
  273. int albumID = keyboard.nextInt();
  274. SQL.printAllTable(c, "artists");
  275. System.out.println("type the artist's ID: ");
  276. keyboard = new Scanner(System.in);
  277. int artistID = keyboard.nextInt();
  278. i.addMusic(musicName, description, duration, albumID, artistID);
  279. break;
  280.  
  281. case 2:
  282. System.out.println("album name: ");
  283. keyboard = new Scanner(System.in);
  284. String albumName = keyboard.nextLine();
  285. System.out.println("album genre: ");
  286. keyboard = new Scanner(System.in);
  287. String albumGenre = keyboard.nextLine();
  288. System.out.println("date of the album: (in 'yyyy-mm-dd')");
  289. keyboard = new Scanner(System.in);
  290. String albumDate = keyboard.nextLine();
  291. SQL.printAllTable(c, "artists");
  292. System.out.println("type the artist's ID: ");
  293. keyboard = new Scanner(System.in);
  294. int artistID2 = keyboard.nextInt();
  295. i.addAlbum(albumName, albumGenre, albumDate, artistID2);
  296. break;
  297.  
  298. case 3:
  299. System.out.println("artist's name: ");
  300. keyboard = new Scanner(System.in);
  301. String artistName = keyboard.nextLine();
  302. System.out.println("description: ");
  303. keyboard = new Scanner(System.in);
  304. String descriptionArtist = keyboard.nextLine();
  305. if(descriptionArtist.length() == 0) {
  306. descriptionArtist = "no description";
  307. }
  308. i.addArtist(artistName, descriptionArtist);
  309. break;
  310. default:
  311. System.out.println("please enter valid option");
  312. }
  313. break;
  314. case 2:
  315. System.out.println("which you want to change the name?");
  316. System.out.println("1. artist");
  317. System.out.println("2. album");
  318. System.out.println("3. music");
  319. keyboard = new Scanner(System.in);
  320. int choice3 = keyboard.nextInt();
  321. switch(choice3){
  322. case 1:
  323. SQL.printAllTable(c,"artists");
  324. System.out.println("select the ID you want to change the name");
  325. keyboard = new Scanner(System.in);
  326. int artistID = keyboard.nextInt();
  327. System.out.println("type the new name: ");
  328. keyboard = new Scanner(System.in);
  329. String artistNewName = keyboard.nextLine();
  330. i.changeData("artists","name", artistID, artistNewName);
  331. break;
  332. case 2:
  333. SQL.printAllTable(c,"albums");
  334. System.out.println("select the ID you want to change the name");
  335. keyboard = new Scanner(System.in);
  336. int albumID = keyboard.nextInt();
  337. System.out.println("type the new name: ");
  338. keyboard = new Scanner(System.in);
  339. String albumNewName = keyboard.nextLine();
  340. i.changeData("albums","name", albumID, albumNewName);
  341. break;
  342. case 3:
  343. SQL.printAllTable(c,"musics");
  344. System.out.println("select the ID you want to change the name");
  345. keyboard = new Scanner(System.in);
  346. int musicID = keyboard.nextInt();
  347. System.out.println("type the new name: ");
  348. keyboard = new Scanner(System.in);
  349. String musicNewName = keyboard.nextLine();
  350. i.changeData("musics","name", musicID, musicNewName);
  351. break;
  352. default:
  353. System.out.println("please enter valid option");
  354.  
  355. }
  356. break;
  357. case 3:
  358. System.out.println("type the username you want to make admin: ");
  359. keyboard = new Scanner(System.in);
  360. username = keyboard.nextLine();
  361. if(i.grantAdminToUser(username)) {
  362. System.out.println(username + " admin granted");
  363. }
  364. else {
  365. System.out.println("already admin / username doesn't exist");
  366. }
  367. break;
  368. }
  369. }
  370. else{
  371. System.out.println("you are not an admin");
  372. }
  373. case 6:
  374. //logout
  375. return true;
  376. case 7:
  377. //exit
  378. return false;
  379. /*case 7:
  380. map = new HashMap<>();
  381. map.put("type", "CreateDataBaseforsong");
  382. new Threads(map);
  383. break;
  384. case 8:
  385. map = new HashMap<>();
  386. map.put("type","uploadbrink");
  387. new Threads(map);
  388. break;*/
  389. default:
  390. System.out.println("please enter valid option");
  391. }
  392.  
  393.  
  394. }
  395.  
  396. }
  397.  
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement