Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package restauracja;
- import java.sql.*;
- import java.util.*;
- public class Start {
- public static void main(String[] args) throws SQLException {
- MenuZ();
- }
- public static void ListaZ() throws SQLException {
- String url = "jdbc:mysql://localhost:3306/wsei";
- String user = "root";
- String password = "root";
- Connection myConn = null;
- Statement myStmt = null;
- ResultSet myRs = null;
- try {
- myConn = DriverManager.getConnection(url, user, password);
- myStmt = myConn.createStatement();
- myRs = myStmt.executeQuery("select * from zamowienia");
- System.out.println("Lista zamówień: ");
- while (myRs.next()) {
- System.out.println("przystawka: " + myRs.getString("przystawka") + ", " + "zupa: " + myRs.getString("zupa") + ", " + "danie główne: " + myRs.getString("danie") + " - " + myRs.getString("cena") + " zł");
- }
- }
- catch (Exception exc) {
- exc.printStackTrace();
- }
- finally {
- if (myRs != null) {
- System.out.println("Koniec zamówień.");
- myRs.close();
- }
- if (myStmt != null) {
- myStmt.close();
- }
- if (myConn != null) {
- myConn.close();
- }
- }
- }
- public static void DeleteZ() throws SQLException {
- String url = "jdbc:mysql://localhost:3306/wsei";
- String user = "root";
- String password = "root";
- Connection myConn = null;
- Statement myStmt = null;
- int id = 0;
- Scanner del = new Scanner(System.in);
- System.out.println("Wprowadź numer zamówienia, które chcesz usunąć:");
- do {
- try {
- id = del.nextInt();
- if (id != 0 ) {
- System.out.println("Zamówienie o numerze id " + id + " zostanie usunięte.");
- }
- else {
- System.out.println("Podaj liczbę większą od 0.");
- id = del.nextInt();
- }
- }
- catch (InputMismatchException e) {
- System.out.println("Podaj liczbę większą od 0.");
- del.nextLine();
- }
- }
- while(id != 0);
- try {
- myConn = DriverManager.getConnection(url, user, password);
- myStmt = myConn.createStatement();
- String sql = "delete from zamowienia where id='"+id+"'";
- myStmt.executeUpdate(sql);
- System.out.println("Usunięto zamówienie.");
- }
- catch (Exception exc) {
- exc.printStackTrace();
- }
- finally {
- if (myStmt != null) {
- myStmt.close();
- }
- if (myConn != null) {
- myConn.close();
- }
- }
- del.close();
- }
- public static void DodajZ() throws SQLException {
- String url = "jdbc:mysql://localhost:3306/wsei";
- String user = "root";
- String password = "root";
- Connection myConn = null;
- Statement myStmt = null;
- String przystawka1 = " ";
- String zupa1 = " ";
- String danie1 = " ";
- int cena1 = 0;
- int p = 0;
- int z = 0;
- int d = 0;
- Scanner ppp = new Scanner(System.in);
- System.out.println("Złóż zamówienie: ");
- System.out.println("Wybierz przystawkę:");
- String[][] przystawki = {{"bułeczki czosnkowe x2", "7"},{"quesadilla", "11"},{"naczos z serem", "9"},{"brak", "0"}};
- for (int i = 0; i < przystawki.length; i++) {
- System.out.println((i+1) + ". " + przystawki[i][0] + " - " + przystawki[i][1] + "zł");
- }
- do {
- try {
- p = ppp.nextInt();
- if (p >= 1 && p <= 4) {
- przystawka1 = przystawki[p-1][0];
- cena1 += Integer.parseInt(przystawki[p-1][1]);
- }
- else {
- System.out.println("Podaj liczbę z przedziału 1-4.");
- p = ppp.nextInt();
- }
- }
- catch (InputMismatchException e) {
- System.out.println("Podaj liczbę z przedziału 1-4.");
- ppp.nextLine();
- }
- }
- while(p != 1 && p != 2 && p != 3 && p != 4);
- System.out.println("Wybierz zupę:");
- String[][] zupy = {{"rosół", "5"},{"pomidorowa", "6"},{"brokułowa", "7"},{"brak", "0"}};
- for (int i = 0; i < zupy.length; i++) {
- System.out.println((i+1) + ". " + zupy[i][0] + " - " + zupy[i][1] + "zł");
- }
- do {
- try {
- z = ppp.nextInt();
- if (z >= 1 && z <= 4) {
- zupa1 = zupy[z-1][0];
- cena1 += Integer.parseInt(zupy[z-1][1]);
- }
- else {
- System.out.println("Podaj liczbę z przedziału 1-4.");
- z = ppp.nextInt();
- }
- }
- catch (InputMismatchException e) {
- System.out.println("Podaj liczbę z przedziału 1-4.");
- ppp.nextLine();
- }
- }
- while(z != 1 && z != 2 && z != 3 && z != 4);
- System.out.println("Wybierz danie główne:");
- String[][] dania = {{"schabowy z ziemniakami", "11"},{"schabowy z frytkami", "12"},{"grillowany kurczak", "15"},{"brak", "0"}};
- for (int i = 0; i < dania.length; i++) {
- System.out.println(dania[i][0] + " - " + dania[i][1] + "zł");
- }
- do {
- try {
- d = ppp.nextInt();
- if (d >= 1 && d <= 4) {
- danie1 = dania[d-1][0];
- cena1 += Integer.parseInt(dania[d-1][1]);
- }
- else {
- System.out.println("Podaj liczbę z przedziału 1-4.");
- d = ppp.nextInt();
- }
- }
- catch (InputMismatchException e) {
- System.out.println("Podaj liczbę z przedziału 1-4.");
- ppp.nextLine();
- }
- }
- while(d != 1 && d != 2 && d != 3 && d != 4);
- try {
- myConn = DriverManager.getConnection(url, user, password);
- myStmt = myConn.createStatement();
- String sql = "insert into zamowienia" + " (przystawka, zupa, danie, cena)" + " values ('"+przystawka1+"', '"+zupa1+"', '"+danie1+"', '"+cena1+"')";
- myStmt.executeUpdate(sql);
- System.out.println("Dodano zamówienie.");
- }
- catch (Exception exc) {
- exc.printStackTrace();
- }
- finally {
- if (myStmt != null) {
- myStmt.close();
- }
- if (myConn != null) {
- myConn.close();
- }
- }
- ppp.close();
- }
- public static void MenuZ() throws SQLException {
- Scanner odczyt = new Scanner(System.in);
- int a = 0;
- System.out.println("Menu zarządzania zamówieniami:");
- System.out.println("1. Dodaj zamówienie.");
- System.out.println("2. Usuń zamówienie.");
- System.out.println("3. Lista zamówień.");
- System.out.println("4. Wyjście.");
- System.out.println("Wybierz akcję 1-4: ");
- do {
- try {
- a = odczyt.nextInt();
- if (a >= 1 && a <= 4) {
- System.out.println("WPISAŁEŚ: " + a);
- }
- else {
- System.out.println("Podaj liczbę z przedziału 1-4.");
- a = odczyt.nextInt();
- }
- }
- catch (InputMismatchException e) {
- System.out.println("Podaj liczbę z przedziału 1-4.");
- odczyt.nextLine();
- }
- }
- while(a != 1 && a != 2 && a != 3 && a != 4);
- switch(a) {
- case 1:
- DodajZ();
- System.out.println("Czy chcesz powrócić do menu? (1- tak, 0 - nie)");
- do {
- try {
- a = odczyt.nextInt();
- if (a == 1) {
- System.out.println("Powrót do menu. ");
- MenuZ();
- }
- else if (a == 0) {
- System.out.println("Koniec.");
- }
- else {
- System.out.println("Podaj liczbę 1 lub 0.");
- a = odczyt.nextInt();
- }
- }
- catch (InputMismatchException e) {
- System.out.println("Podaj liczbę 1 lub 0.");
- odczyt.nextLine();
- }
- }
- while(a != 0 && a != 1);
- break;
- case 2:
- DeleteZ();
- System.out.println("Czy chcesz powrócić do menu? (1- tak, 0 - nie)");
- do {
- try {
- a = odczyt.nextInt();
- if (a == 1) {
- System.out.println("Powrót do menu. ");
- MenuZ();
- }
- else if (a == 0) {
- System.out.println("Koniec.");
- }
- else {
- System.out.println("Podaj liczbę 1 lub 0.");
- a = odczyt.nextInt();
- }
- }
- catch (InputMismatchException e) {
- System.out.println("Podaj liczbę 1 lub 0.");
- odczyt.nextLine();
- }
- }
- while(a != 0 && a != 1);
- break;
- case 3:
- ListaZ();
- System.out.println("Czy chcesz powrócić do menu? (1- tak, 0 - nie)");
- do {
- try {
- a = odczyt.nextInt();
- if (a == 1) {
- System.out.println("Powrót do menu. ");
- MenuZ();
- }
- else if (a == 0) {
- System.out.println("Koniec.");
- }
- else {
- System.out.println("Podaj liczbę 1 lub 0.");
- a = odczyt.nextInt();
- }
- }
- catch (InputMismatchException e) {
- System.out.println("Podaj liczbę 1 lub 0.");
- odczyt.nextLine();
- }
- }
- while(a != 0 && a != 1);
- break;
- case 4:
- System.out.println("Koniec.");
- break;
- }
- odczyt.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement