Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. package restauracja;
  2. import java.sql.*;
  3. import java.util.*;
  4.  
  5.  
  6.  
  7. public class Start {
  8.  
  9. public static void main(String[] args) throws SQLException {
  10.  
  11. MenuZ();
  12. }
  13.  
  14.  
  15.  
  16.  
  17. public static void ListaZ() throws SQLException {
  18.  
  19. String url = "jdbc:mysql://localhost:3306/wsei";
  20. String user = "root";
  21. String password = "root";
  22. Connection myConn = null;
  23. Statement myStmt = null;
  24. ResultSet myRs = null;
  25.  
  26.  
  27. try {
  28. myConn = DriverManager.getConnection(url, user, password);
  29. myStmt = myConn.createStatement();
  30. myRs = myStmt.executeQuery("select * from zamowienia");
  31.  
  32. System.out.println("Lista zamówień: ");
  33. while (myRs.next()) {
  34. System.out.println("przystawka: " + myRs.getString("przystawka") + ", " + "zupa: " + myRs.getString("zupa") + ", " + "danie główne: " + myRs.getString("danie") + " - " + myRs.getString("cena") + " zł");
  35. }
  36. }
  37. catch (Exception exc) {
  38. exc.printStackTrace();
  39. }
  40. finally {
  41. if (myRs != null) {
  42. System.out.println("Koniec zamówień.");
  43. myRs.close();
  44. }
  45.  
  46. if (myStmt != null) {
  47. myStmt.close();
  48. }
  49.  
  50. if (myConn != null) {
  51. myConn.close();
  52. }
  53. }
  54.  
  55. }
  56.  
  57. public static void DeleteZ() throws SQLException {
  58.  
  59. String url = "jdbc:mysql://localhost:3306/wsei";
  60. String user = "root";
  61. String password = "root";
  62. Connection myConn = null;
  63. Statement myStmt = null;
  64.  
  65. int id = 0;
  66. Scanner del = new Scanner(System.in);
  67. System.out.println("Wprowadź numer zamówienia, które chcesz usunąć:");
  68. do {
  69. try {
  70.  
  71. id = del.nextInt();
  72. if (id != 0 ) {
  73. System.out.println("Zamówienie o numerze id " + id + " zostanie usunięte.");
  74. }
  75. else {
  76. System.out.println("Podaj liczbę większą od 0.");
  77. id = del.nextInt();
  78. }
  79. }
  80.  
  81. catch (InputMismatchException e) {
  82. System.out.println("Podaj liczbę większą od 0.");
  83. del.nextLine();
  84. }
  85. }
  86. while(id != 0);
  87.  
  88. try {
  89. myConn = DriverManager.getConnection(url, user, password);
  90. myStmt = myConn.createStatement();
  91. String sql = "delete from zamowienia where id='"+id+"'";
  92. myStmt.executeUpdate(sql);
  93. System.out.println("Usunięto zamówienie.");
  94. }
  95. catch (Exception exc) {
  96. exc.printStackTrace();
  97. }
  98. finally {
  99. if (myStmt != null) {
  100. myStmt.close();
  101. }
  102. if (myConn != null) {
  103. myConn.close();
  104. }
  105. }
  106. del.close();
  107.  
  108. }
  109.  
  110. public static void DodajZ() throws SQLException {
  111. String url = "jdbc:mysql://localhost:3306/wsei";
  112. String user = "root";
  113. String password = "root";
  114. Connection myConn = null;
  115. Statement myStmt = null;
  116.  
  117.  
  118. String przystawka1 = " ";
  119. String zupa1 = " ";
  120. String danie1 = " ";
  121. int cena1 = 0;
  122. int p = 0;
  123. int z = 0;
  124. int d = 0;
  125. Scanner ppp = new Scanner(System.in);
  126. System.out.println("Złóż zamówienie: ");
  127. System.out.println("Wybierz przystawkę:");
  128. String[][] przystawki = {{"bułeczki czosnkowe x2", "7"},{"quesadilla", "11"},{"naczos z serem", "9"},{"brak", "0"}};
  129. for (int i = 0; i < przystawki.length; i++) {
  130. System.out.println((i+1) + ". " + przystawki[i][0] + " - " + przystawki[i][1] + "zł");
  131. }
  132. do {
  133. try {
  134. p = ppp.nextInt();
  135. if (p >= 1 && p <= 4) {
  136. przystawka1 = przystawki[p-1][0];
  137. cena1 += Integer.parseInt(przystawki[p-1][1]);
  138. }
  139. else {
  140. System.out.println("Podaj liczbę z przedziału 1-4.");
  141. p = ppp.nextInt();
  142. }
  143. }
  144.  
  145. catch (InputMismatchException e) {
  146. System.out.println("Podaj liczbę z przedziału 1-4.");
  147. ppp.nextLine();
  148. }
  149. }
  150. while(p != 1 && p != 2 && p != 3 && p != 4);
  151.  
  152.  
  153. System.out.println("Wybierz zupę:");
  154. String[][] zupy = {{"rosół", "5"},{"pomidorowa", "6"},{"brokułowa", "7"},{"brak", "0"}};
  155. for (int i = 0; i < zupy.length; i++) {
  156. System.out.println((i+1) + ". " + zupy[i][0] + " - " + zupy[i][1] + "zł");
  157. }
  158. do {
  159. try {
  160. z = ppp.nextInt();
  161. if (z >= 1 && z <= 4) {
  162. zupa1 = zupy[z-1][0];
  163. cena1 += Integer.parseInt(zupy[z-1][1]);
  164. }
  165. else {
  166. System.out.println("Podaj liczbę z przedziału 1-4.");
  167. z = ppp.nextInt();
  168. }
  169. }
  170.  
  171. catch (InputMismatchException e) {
  172. System.out.println("Podaj liczbę z przedziału 1-4.");
  173. ppp.nextLine();
  174. }
  175. }
  176. while(z != 1 && z != 2 && z != 3 && z != 4);
  177.  
  178. System.out.println("Wybierz danie główne:");
  179. String[][] dania = {{"schabowy z ziemniakami", "11"},{"schabowy z frytkami", "12"},{"grillowany kurczak", "15"},{"brak", "0"}};
  180. for (int i = 0; i < dania.length; i++) {
  181. System.out.println(dania[i][0] + " - " + dania[i][1] + "zł");
  182. }
  183. do {
  184. try {
  185. d = ppp.nextInt();
  186. if (d >= 1 && d <= 4) {
  187. danie1 = dania[d-1][0];
  188. cena1 += Integer.parseInt(dania[d-1][1]);
  189. }
  190. else {
  191. System.out.println("Podaj liczbę z przedziału 1-4.");
  192. d = ppp.nextInt();
  193. }
  194. }
  195.  
  196. catch (InputMismatchException e) {
  197. System.out.println("Podaj liczbę z przedziału 1-4.");
  198. ppp.nextLine();
  199. }
  200. }
  201. while(d != 1 && d != 2 && d != 3 && d != 4);
  202.  
  203.  
  204. try {
  205. myConn = DriverManager.getConnection(url, user, password);
  206. myStmt = myConn.createStatement();
  207.  
  208. String sql = "insert into zamowienia" + " (przystawka, zupa, danie, cena)" + " values ('"+przystawka1+"', '"+zupa1+"', '"+danie1+"', '"+cena1+"')";
  209. myStmt.executeUpdate(sql);
  210. System.out.println("Dodano zamówienie.");
  211. }
  212. catch (Exception exc) {
  213. exc.printStackTrace();
  214. }
  215. finally {
  216. if (myStmt != null) {
  217. myStmt.close();
  218. }
  219. if (myConn != null) {
  220. myConn.close();
  221. }
  222. }
  223. ppp.close();
  224. }
  225.  
  226.  
  227.  
  228.  
  229. public static void MenuZ() throws SQLException {
  230.  
  231. Scanner odczyt = new Scanner(System.in);
  232. int a = 0;
  233.  
  234. System.out.println("Menu zarządzania zamówieniami:");
  235. System.out.println("1. Dodaj zamówienie.");
  236. System.out.println("2. Usuń zamówienie.");
  237. System.out.println("3. Lista zamówień.");
  238. System.out.println("4. Wyjście.");
  239. System.out.println("Wybierz akcję 1-4: ");
  240. do {
  241. try {
  242. a = odczyt.nextInt();
  243. if (a >= 1 && a <= 4) {
  244. System.out.println("WPISAŁEŚ: " + a);
  245. }
  246. else {
  247. System.out.println("Podaj liczbę z przedziału 1-4.");
  248. a = odczyt.nextInt();
  249. }
  250. }
  251.  
  252. catch (InputMismatchException e) {
  253. System.out.println("Podaj liczbę z przedziału 1-4.");
  254. odczyt.nextLine();
  255. }
  256. }
  257. while(a != 1 && a != 2 && a != 3 && a != 4);
  258.  
  259. switch(a) {
  260. case 1:
  261. DodajZ();
  262. System.out.println("Czy chcesz powrócić do menu? (1- tak, 0 - nie)");
  263. do {
  264. try {
  265. a = odczyt.nextInt();
  266. if (a == 1) {
  267. System.out.println("Powrót do menu. ");
  268. MenuZ();
  269. }
  270. else if (a == 0) {
  271. System.out.println("Koniec.");
  272. }
  273. else {
  274. System.out.println("Podaj liczbę 1 lub 0.");
  275. a = odczyt.nextInt();
  276. }
  277. }
  278.  
  279. catch (InputMismatchException e) {
  280. System.out.println("Podaj liczbę 1 lub 0.");
  281. odczyt.nextLine();
  282. }
  283. }
  284. while(a != 0 && a != 1);
  285. break;
  286. case 2:
  287. DeleteZ();
  288. System.out.println("Czy chcesz powrócić do menu? (1- tak, 0 - nie)");
  289. do {
  290. try {
  291. a = odczyt.nextInt();
  292. if (a == 1) {
  293. System.out.println("Powrót do menu. ");
  294. MenuZ();
  295. }
  296. else if (a == 0) {
  297. System.out.println("Koniec.");
  298. }
  299. else {
  300. System.out.println("Podaj liczbę 1 lub 0.");
  301. a = odczyt.nextInt();
  302. }
  303. }
  304.  
  305. catch (InputMismatchException e) {
  306. System.out.println("Podaj liczbę 1 lub 0.");
  307. odczyt.nextLine();
  308. }
  309. }
  310. while(a != 0 && a != 1);
  311. break;
  312. case 3:
  313. ListaZ();
  314. System.out.println("Czy chcesz powrócić do menu? (1- tak, 0 - nie)");
  315. do {
  316. try {
  317. a = odczyt.nextInt();
  318. if (a == 1) {
  319. System.out.println("Powrót do menu. ");
  320. MenuZ();
  321. }
  322. else if (a == 0) {
  323. System.out.println("Koniec.");
  324. }
  325. else {
  326. System.out.println("Podaj liczbę 1 lub 0.");
  327. a = odczyt.nextInt();
  328. }
  329. }
  330.  
  331. catch (InputMismatchException e) {
  332. System.out.println("Podaj liczbę 1 lub 0.");
  333. odczyt.nextLine();
  334. }
  335. }
  336. while(a != 0 && a != 1);
  337. break;
  338. case 4:
  339. System.out.println("Koniec.");
  340. break;
  341. }
  342. odczyt.close();
  343. }
  344.  
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement