Advertisement
Guest User

UAS PD2

a guest
Jan 8th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.77 KB | None | 0 0
  1. package tapd2;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. public class TAPD2 {
  8.  
  9. public static void menu() {
  10. System.out.println("");
  11. System.out.println("###Pilihan Menu###");
  12. System.out.println(" ");
  13. System.out.println("1. Create");
  14. System.out.println("2. View");
  15. System.out.println("3. Delete");
  16. System.out.println("4. Update");
  17. System.out.println("5. Quit");
  18. System.out.println("");
  19. System.out.print("Pilihan no : ");
  20. }
  21.  
  22. public static void insert() throws Exception {
  23. double temp[] = new double[5];
  24. double tempbobot[] = new double[4];
  25. double tempbobot1[] = new double[4];
  26. Class.forName("com.mysql.jdbc.Driver");
  27. Connection conn = DriverManager.getConnection(
  28. "jdbc:mysql://127.0.0.1/uaspd2",
  29. "root",
  30. "");
  31. Statement stmt = conn.createStatement();
  32. ResultSet rsbobot = stmt.executeQuery("SELECT * from bobot WHERE id_bobot= 141111003 ;");
  33. while (rsbobot.next()) {
  34. tempbobot[0] = ((rsbobot.getInt("bobot_wawasan")) * 0.01);
  35. tempbobot[1] = ((rsbobot.getInt("bobot_jaringan")) * 0.01);
  36. tempbobot[2] = ((rsbobot.getInt("bobot_kepribadian")) * 0.01);
  37. tempbobot[3] = ((rsbobot.getInt("bobot_agama")) * 0.01);
  38. }
  39. System.out.print("ID\t: ");
  40. int id = new Scanner(System.in).nextInt();
  41. boolean ketemu = false;
  42. ResultSet rsid = stmt.executeQuery("SELECT id_mentah from karyawan_mentahan where id_mentah =" + id + ";");
  43. while (rsid.next()) {
  44. ketemu = true;
  45. }
  46. if (ketemu == true) {
  47. System.out.println("**ID sudah ada**");
  48. } else {
  49. System.out.print("Nama\t: ");
  50. String nama = new Scanner(System.in).nextLine();
  51. int wawasan, jaringan, kepribadian, agama;
  52. do {
  53. System.out.print("Tes Wawasan\t\t: ");
  54. wawasan = new Scanner(System.in).nextInt();
  55. } while (wawasan < 0 || wawasan > 100);
  56. do {
  57. System.out.print("Tes Instalasi Jaringan\t: ");
  58. jaringan = new Scanner(System.in).nextInt();
  59. } while (jaringan < 0 || jaringan > 100);
  60. do {
  61. System.out.print("Tes Kepribadian\t\t: ");
  62. kepribadian = new Scanner(System.in).nextInt();
  63. } while (kepribadian < 0 || kepribadian > 100);
  64. do {
  65. System.out.print("Tes Agama\t\t: ");
  66. agama = new Scanner(System.in).nextInt();
  67. } while (agama < 0 || agama > 100);
  68. double wawasan1 = wawasan * tempbobot[0];
  69. double jaringan1 = jaringan * tempbobot[1];
  70. double kepribadian1 = kepribadian * tempbobot[2];
  71. double agama1 = agama * tempbobot[3];
  72. double hasil = wawasan1 + jaringan1 + kepribadian1 + agama1;
  73. String create = "INSERT INTO karyawan (id,nama,wawasan,jaringan,kepribadian,agama)"
  74. + "VALUES ('" + id + "','" + nama + "'," + wawasan1 + "," + jaringan1 + "," + kepribadian1 + "," + agama1 + ");";
  75. String create_mentah = "INSERT INTO karyawan_mentahan (id_mentah,nama_mentah,wawasan_mentah,jaringan_mentah,kepribadian_mentah,agama_mentah)"
  76. + "VALUES ('" + id + "','" + nama + "'," + wawasan + "," + jaringan + "," + kepribadian + "," + agama + ");";
  77. stmt.executeUpdate(create);
  78. stmt.executeUpdate(create_mentah);
  79. String hasilhasil = "INSERT INTO hasil(hasil_akhir,id)" + "VALUES (" + hasil + ",'" + id + "');";
  80. stmt.executeUpdate(hasilhasil);
  81. System.out.println("*Create Sukses*");
  82. }
  83. }
  84.  
  85. public static void view() throws Exception {
  86. String lihat[] = new String[1];
  87. Class.forName("com.mysql.jdbc.Driver");
  88. Connection conn = DriverManager.getConnection(
  89. "jdbc:mysql://127.0.0.1/uaspd2",
  90. "root",
  91. "");
  92. Statement stmt = conn.createStatement();
  93. ResultSet rsview = stmt.executeQuery("SELECT karyawan.id from karyawan");
  94. while (rsview.next()) {
  95. lihat[0] = rsview.getString("id");
  96. };
  97. if (lihat[0] != null) {
  98. System.out.println("1. View data olahan");
  99. System.out.println("2. View data asli");
  100. System.out.println("3. View bobot kriteria");
  101. System.out.print("Pilih no : ");
  102. int no = new Scanner(System.in).nextInt();
  103. if (no == 1) {
  104. ResultSet rs = stmt.executeQuery("SELECT karyawan.id,karyawan.nama,karyawan.wawasan,karyawan.jaringan,karyawan.kepribadian,karyawan.agama,hasil.hasil_akhir from karyawan,hasil WHERE karyawan.id=hasil.id order by hasil.hasil_akhir desc;");
  105. System.out.println("+-------+-----------------+-------------------------------------+---------------------------------------+-----------------------+-----------------------+---------------+");
  106. System.out.println("|ID\t| Nama\t\t | Tes wawasan Sistem Informasi\t| Tes praktek instalasi Jaringan\t| Tes kepribadian\t| Tes pengetahuan Agama\t| Hasil Akhir\t|");
  107. System.out.println("+-------+-----------------+-------------------------------------+---------------------------------------+-----------------------+-----------------------+---------------+");
  108. while (rs.next()) {
  109. System.out.println("| " + rs.getString("id") + "\t| " + rs.getString("nama") + "\t\t | \t\t" + rs.getInt("wawasan") + "\t\t\t| \t\t" + rs.getInt("jaringan") + "\t\t\t| \t" + rs.getInt("kepribadian") + "\t\t| \t" + rs.getInt("agama") + "\t\t|\t" + rs.getInt("hasil_akhir") + "\t|");
  110. }
  111. System.out.println("+-------+-----------------+-------------------------------------+---------------------------------------+-----------------------+-----------------------+---------------+");
  112. } else if (no == 2) {
  113. ResultSet rst = stmt.executeQuery("SELECT karyawan_mentahan.id_mentah,karyawan_mentahan.nama_mentah,karyawan_mentahan.wawasan_mentah,karyawan_mentahan.jaringan_mentah,karyawan_mentahan.kepribadian_mentah,karyawan_mentahan.agama_mentah from karyawan_mentahan order by karyawan_mentahan.id_mentah;");
  114. System.out.println("+-------+-----------------+-------------------------------------+---------------------------------------+-----------------------+-----------------------+");
  115. System.out.println("|ID\t| Nama\t\t | Tes wawasan Sistem Informasi\t| Tes praktek instalasi Jaringan\t| Tes kepribadian\t| Tes pengetahuan Agama\t|");
  116. System.out.println("+-------+-----------------+-------------------------------------+---------------------------------------+-----------------------+-----------------------+");
  117. while (rst.next()) {
  118. System.out.println("| " + rst.getString("id_mentah") + "\t| " + rst.getString("nama_mentah") + "\t\t | \t\t" + rst.getInt("wawasan_mentah") + "\t\t\t| \t\t" + rst.getInt("jaringan_mentah") + "\t\t\t| \t" + rst.getInt("kepribadian_mentah") + "\t\t| \t" + rst.getInt("agama_mentah") + "\t\t|");
  119. }
  120. System.out.println("+-------+-----------------+-------------------------------------+---------------------------------------+-----------------------+-----------------------+");
  121. } else if (no == 3) {
  122. ResultSet rsbobot = stmt.executeQuery("SELECT * from bobot WHERE id_bobot= 141111003 ;");
  123. while (rsbobot.next()) {
  124. System.out.println("Bobot Tes Wawasan\t: " + (rsbobot.getInt("bobot_wawasan")) + "%");
  125. System.out.println("Bobot Tes Jaringan\t: " + (rsbobot.getInt("bobot_jaringan")) + "%");
  126. System.out.println("Bobot Tes Kepribadian\t: " + (rsbobot.getInt("bobot_kepribadian")) + "%");
  127. System.out.println("Bobot Tes Agama\t\t: " + (rsbobot.getInt("bobot_agama")) + "%");
  128. }
  129. } else {
  130. System.out.println("Pilihan No Salah");
  131. }
  132. } else {
  133. System.out.println("Data masih kosong");
  134. }
  135. }
  136.  
  137. public static void deleted() throws Exception {
  138. String terhapus[] = new String[1];
  139. Class.forName("com.mysql.jdbc.Driver");
  140. Connection conn = DriverManager.getConnection(
  141. "jdbc:mysql://127.0.0.1/uaspd2",
  142. "root",
  143. "");
  144. Statement stmt = conn.createStatement();
  145. System.out.print("Masukkan ID yang akan dihapus : ");
  146. String hapus = new Scanner(System.in).nextLine();
  147. ResultSet rshapus = stmt.executeQuery("SELECT karyawan.id from karyawan where karyawan.id=" + hapus + ";");
  148. while (rshapus.next()) {
  149. terhapus[0] = rshapus.getString("id");
  150. };
  151. if (terhapus[0] != null) {
  152. String delete = "DELETE FROM hasil WHERE hasil.id=" + hapus + ";";
  153. stmt.executeUpdate(delete);
  154. String sql_del = "DELETE FROM karyawan WHERE karyawan.id=" + hapus + ";";
  155. stmt.executeUpdate(sql_del);
  156. String sql_del_mentah = "DELETE FROM karyawan_mentahan WHERE karyawan_mentahan.id_mentah=" + hapus + ";";
  157. stmt.executeUpdate(sql_del_mentah);
  158. System.out.println("*Delete Sukses*");
  159. } else {
  160. System.out.println("ID tidak ada");
  161. }
  162. }
  163.  
  164. public static void updated() throws Exception {
  165. String terupdate[] = new String[1];
  166. double tempbobot[] = new double[4];
  167. int tempbobotasli[] = new int[4];
  168. ArrayList tempwawasan = new ArrayList();
  169. ArrayList tempjaringan = new ArrayList();
  170. ArrayList tempkepribadian = new ArrayList();
  171. ArrayList tempagama = new ArrayList();
  172. ArrayList tempid = new ArrayList();
  173. ArrayList tempid1 = new ArrayList();
  174. ArrayList tempid2 = new ArrayList();
  175. ArrayList tempid3 = new ArrayList();
  176. double temp[] = new double[4];
  177.  
  178. Scanner baca = new Scanner(System.in);
  179. Class.forName("com.mysql.jdbc.Driver");
  180. Connection conn = DriverManager.getConnection(
  181. "jdbc:mysql://127.0.0.1/uaspd2",
  182. "root",
  183. "");
  184. Statement stmt = conn.createStatement();
  185. System.out.println("Pilihan Update : ");
  186. System.out.println("1. Update Karyawan");
  187. System.out.println("2. Update Bobot Kriteria");
  188. System.out.println("");
  189. System.out.print("Pilih No : ");
  190. int pilihupdate = new Scanner(System.in).nextInt();
  191. if (pilihupdate == 1) {
  192. System.out.print("Masukkan ID yang akan diupdate : ");
  193. int id_update = new Scanner(System.in).nextInt();
  194. ResultSet rsupdate = stmt.executeQuery("SELECT karyawan.id from karyawan where karyawan.id=" + id_update + ";");
  195. while (rsupdate.next()) {
  196. terupdate[0] = rsupdate.getString("id");
  197. };
  198. if (terupdate[0] != null) {
  199. ResultSet rs = stmt.executeQuery("SELECT wawasan,jaringan,kepribadian,agama from karyawan WHERE id=" + id_update + ";");
  200. while (rs.next()) {
  201. temp[0] = (rs.getInt("wawasan"));
  202. temp[1] = (rs.getInt("jaringan"));
  203. temp[2] = (rs.getInt("kepribadian"));
  204. temp[3] = (rs.getInt("agama"));
  205. }
  206. System.out.println("");
  207. System.out.println("Bagian yang akan di update : ");
  208. System.out.println("1. Update Nama");
  209. System.out.println("2. Update Tes Wawasan");
  210. System.out.println("3. Update Tes Instalasi Jaringan");
  211. System.out.println("4. Update Tes Kepribadian");
  212. System.out.println("5. Update Tes Agama");
  213. System.out.println("");
  214. System.out.print("Update no : ");
  215. int pilih = baca.nextInt();
  216. if (pilih == 1) {
  217. System.out.print("Nama\t: ");
  218. String name = new Scanner(System.in).nextLine();
  219. String sql_update_nama = "UPDATE karyawan SET karyawan.nama='" + name + "' WHERE karyawan.id=" + id_update + ";";
  220. stmt.executeUpdate(sql_update_nama);
  221. String sql_update_nama_mentahan = "UPDATE karyawan_mentahan SET karyawan_mentahan.nama_mentah='" + name + "' WHERE karyawan_mentahan.id_mentah=" + id_update + ";";
  222. stmt.executeUpdate(sql_update_nama_mentahan);
  223. } else if (pilih == 2) {
  224. System.out.print("Tes Wawasan\t: ");
  225. int wawasan = new Scanner(System.in).nextInt();
  226. double wawasan1 = wawasan * tempbobot[0];
  227. temp[0] = wawasan1;
  228. String sql_update_wawasan = "UPDATE karyawan SET karyawan.wawasan=" + wawasan1 + " WHERE karyawan.id=" + id_update + ";";
  229. stmt.executeUpdate(sql_update_wawasan);
  230. String sql_update_wawasan_mentah = "UPDATE karyawan_mentahan SET karyawan_mentahan.wawasan_mentah=" + wawasan + " WHERE karyawan_mentahan.id_mentah=" + id_update + ";";
  231. stmt.executeUpdate(sql_update_wawasan_mentah);
  232. } else if (pilih == 3) {
  233. System.out.print("Tes Instalasi Jaringan\t: ");
  234. int jaringan = new Scanner(System.in).nextInt();
  235. double jaringan1 = jaringan * tempbobot[1];
  236. temp[1] = jaringan1;
  237. String sql_update_jaringan = "UPDATE karyawan SET karyawan.jaringan=" + jaringan1 + " WHERE karyawan.id=" + id_update + ";";
  238. stmt.executeUpdate(sql_update_jaringan);
  239. String sql_update_jaringan_mentah = "UPDATE karyawan_mentahan SET karyawan_mentahan.jaringan_mentah=" + jaringan + " WHERE karyawan_mentahan.id_mentah=" + id_update + ";";
  240. stmt.executeUpdate(sql_update_jaringan_mentah);
  241. } else if (pilih == 4) {
  242. System.out.print("Tes Kepribadian\t: ");
  243. int kepribadian = new Scanner(System.in).nextInt();
  244. double kepribadian1 = kepribadian * tempbobot[2];
  245. temp[2] = kepribadian1;
  246. String sql_update_kepribadian = "UPDATE karyawan SET karyawan.kepribadian=" + kepribadian1 + " WHERE karyawan.id=" + id_update + ";";
  247. stmt.executeUpdate(sql_update_kepribadian);
  248. String sql_update_kepribadian_mentah = "UPDATE karyawan_mentahan SET karyawan_mentahan.kepribadian_mentah=" + kepribadian + " WHERE karyawan_mentahan.id_mentah=" + id_update + ";";
  249. stmt.executeUpdate(sql_update_kepribadian_mentah);
  250. } else if (pilih == 5) {
  251. System.out.print("Tes Agama\t: ");
  252. int agama = new Scanner(System.in).nextInt();
  253. double agama1 = agama * tempbobot[3];
  254. temp[3] = agama1;
  255. String sql_update_agama = "UPDATE karyawan SET karyawan.agama=" + agama1 + " WHERE karyawan.id=" + id_update + ";";
  256. stmt.executeUpdate(sql_update_agama);
  257. String sql_update_agama_mentah = "UPDATE karyawan_mentahan SET karyawan_mentahan.agama_mentah=" + agama + " WHERE karyawan_mentahan.id_mentah=" + id_update + ";";
  258. stmt.executeUpdate(sql_update_agama_mentah);
  259. }
  260. double hasil = temp[0] + temp[1] + temp[2] + temp[3];
  261. String hasilhasil = "UPDATE hasil SET hasil.hasil_akhir=" + hasil + "WHERE hasil.id=" + id_update + ";";
  262. stmt.executeUpdate(hasilhasil);
  263. System.out.println("*Update Sukses*");
  264. } else {
  265. System.out.println("ID tidak ada");
  266. }
  267. } else if (pilihupdate == 2) {
  268. ResultSet rsbobot = stmt.executeQuery("SELECT * from bobot WHERE id_bobot= 141111003 ;");
  269. while (rsbobot.next()) {
  270. tempbobotasli[0] = (rsbobot.getInt("bobot_wawasan"));
  271. tempbobotasli[1] = (rsbobot.getInt("bobot_jaringan"));
  272. tempbobotasli[2] = (rsbobot.getInt("bobot_kepribadian"));
  273. tempbobotasli[3] = (rsbobot.getInt("bobot_agama"));
  274. }
  275.  
  276. tempbobot[0] = tempbobotasli[0] * 0.01;
  277. tempbobot[1] = tempbobotasli[1] * 0.01;
  278. tempbobot[2] = tempbobotasli[2] * 0.01;
  279. tempbobot[3] = tempbobotasli[3] * 0.01;
  280.  
  281. System.out.println("");
  282. System.out.println("Bobot wawasan sebelumnya\t: " + tempbobotasli[0] + "%");
  283. System.out.println("Bobot jaringan sebelumnya\t: " + tempbobotasli[1] + "%");
  284. System.out.println("Bobot kepribadian sebelumnya\t: " + tempbobotasli[2] + "%");
  285. System.out.println("Bobot agama sebelumnya\t\t: " + tempbobotasli[3] + "%");
  286. System.out.println("***Total Bobot harus 100%***");
  287. int totalbobot = 0;
  288. do {
  289. int bobotwawasan;
  290. int bobotjaringan;
  291. int bobotkepribadian;
  292. int bobotagama;
  293. do {
  294. System.out.print("Masukkan bobot Wawasan yang baru\t: ");
  295. bobotwawasan = new Scanner(System.in).nextInt();
  296. } while (bobotwawasan < 0 || bobotwawasan > 100);
  297.  
  298. do {
  299. System.out.print("Masukkan bobot Jaringan yang baru\t: ");
  300. bobotjaringan = new Scanner(System.in).nextInt();
  301. } while (bobotjaringan < 0 || bobotjaringan > 100);
  302.  
  303. do {
  304. System.out.print("Masukkan bobot Kepribadian yang baru\t: ");
  305. bobotkepribadian = new Scanner(System.in).nextInt();
  306. } while (bobotkepribadian < 0 || bobotkepribadian > 100);
  307.  
  308. do {
  309. System.out.print("Masukkan bobot Agama yang baru\t\t: ");
  310. bobotagama = new Scanner(System.in).nextInt();
  311. } while (bobotagama < 0 || bobotagama > 100);
  312. totalbobot = bobotwawasan + bobotjaringan + bobotkepribadian + bobotagama;
  313. System.out.println("");
  314. if (totalbobot == 100) {
  315. String sql_update_bobot_wawasan = "UPDATE bobot SET bobot.bobot_wawasan=" + bobotwawasan + " WHERE bobot.id_bobot= 141111003;";
  316. stmt.executeUpdate(sql_update_bobot_wawasan);
  317. ResultSet rs1 = stmt.executeQuery("SELECT * from karyawan_mentahan ;");
  318. while (rs1.next()) {
  319. tempwawasan.add(((rs1.getInt("wawasan_mentah")) * (bobotwawasan * 0.01)));
  320. tempid.add(rs1.getString("id_mentah"));
  321. }
  322.  
  323.  
  324. String sql_update_bobot_jaringan = "UPDATE bobot SET bobot.bobot_jaringan=" + bobotjaringan + " WHERE bobot.id_bobot= 141111003;";
  325. stmt.executeUpdate(sql_update_bobot_jaringan);
  326. ResultSet rs2 = stmt.executeQuery("SELECT * from karyawan_mentahan ;");
  327. while (rs2.next()) {
  328. tempjaringan.add(((rs2.getInt("jaringan_mentah")) * (bobotjaringan * 0.01)));
  329. tempid1.add(rs2.getString("id_mentah"));
  330. }
  331.  
  332.  
  333.  
  334. String sql_update_bobot_kepribadian = "UPDATE bobot SET bobot.bobot_kepribadian=" + bobotkepribadian + " WHERE bobot.id_bobot= 141111003;";
  335. stmt.executeUpdate(sql_update_bobot_kepribadian);
  336. ResultSet rs3 = stmt.executeQuery("SELECT * from karyawan_mentahan ;");
  337. while (rs3.next()) {
  338. tempkepribadian.add(((rs3.getInt("kepribadian_mentah")) * (bobotkepribadian * 0.01)));
  339. tempid2.add(rs3.getString("id_mentah"));
  340. }
  341.  
  342.  
  343. String sql_update_bobot_agama = "UPDATE bobot SET bobot.bobot_agama=" + bobotagama + " WHERE bobot.id_bobot= 141111003;";
  344. stmt.executeUpdate(sql_update_bobot_agama);
  345. ResultSet rs4 = stmt.executeQuery("SELECT * from karyawan_mentahan ;");
  346. while (rs4.next()) {
  347. tempagama.add(((rs4.getInt("agama_mentah")) * (bobotagama * 0.01)));
  348. tempid3.add(rs4.getString("id_mentah"));
  349. }
  350.  
  351. for (int i = 0; i < tempwawasan.size(); i++) {
  352. String sql_update_wawasan = "UPDATE karyawan SET wawasan=" + tempwawasan.get(i) + ",jaringan=" + tempjaringan.get(i) + ",kepribadian=" + tempkepribadian.get(i) +",agama=" + tempagama.get(i) +"WHERE ID = " + tempid.get(i);
  353. stmt.executeUpdate(sql_update_wawasan);
  354. }
  355.  
  356.  
  357. System.out.println("Update Bobot Sukses");
  358. } else if (totalbobot < 100) {
  359. System.out.println("Update bobot gagal, total bobot kurang dari 100%");
  360. System.out.println("Silahkan isi update lagi");
  361. } else {
  362. System.out.println("Update bobot gagal, total bobot lebih dari 100%");
  363. System.out.println("Silahkan isi update lagi");
  364. }
  365. } while (totalbobot != 100);
  366. } else {
  367. System.out.println("Pilihan Salah");
  368. }
  369. }
  370.  
  371. public static void main(String[] args) throws Exception {
  372. Scanner baca = new Scanner(System.in);
  373. Class.forName("com.mysql.jdbc.Driver");
  374. Connection conn = DriverManager.getConnection(
  375. "jdbc:mysql://127.0.0.1/uaspd2",
  376. "root",
  377. "");
  378. Statement stmt = conn.createStatement();
  379. double hasil[] = new double[1];
  380. boolean keluar = false;
  381. do {
  382. menu();
  383. int no = baca.nextInt();
  384. if (no == 1) {
  385. insert();
  386. } else if (no == 2) {
  387. view();
  388. } else if (no == 3) {
  389. deleted();
  390. } else if (no == 4) {
  391. updated();
  392. } else if (no == 5) {
  393. System.out.println("");
  394. System.out.println("**Terima Kasih**");
  395. System.out.println("");
  396. break;
  397. } else {
  398. System.out.println("Pilihan Salah");
  399. }
  400. } while (keluar == false);
  401. }
  402.  
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement