Advertisement
Guest User

Untitled

a guest
May 13th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.79 KB | None | 0 0
  1. /**
  2.  * Created by arodrigues on 09/05/16.
  3.  */
  4.  
  5. import sun.management.snmp.util.SnmpTableCache;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.*;
  10.  
  11. public class Mesa {
  12.     static int colaboradorID;
  13.     public static void main(String[] args) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
  14.         String connectionUrl = "jdbc:mysql://localhost:3306/MakeMyDay;" +
  15.                 "databaseName=MakeMyDay;user=root;password=admin;";
  16.  
  17.         String url = "jdbc:mysql://localhost:3306/MakeMyDay";
  18.         Class.forName ("com.mysql.jdbc.Driver");
  19.         Connection con = DriverManager.getConnection (url, "root", "admin");
  20.  
  21.         Statement stmt = null;
  22.         ResultSet rs = null;
  23. /*
  24.         String numero1=("SELECT * FROM Colaboradores");
  25.         stmt = con.createStatement();
  26.         rs = stmt.executeQuery(numero1);
  27.  
  28.         String senha = null;
  29.         while(rs != null && rs.next()){
  30.             senha = rs.getString("Password");
  31.  
  32.             System.out.println(senha);
  33.         }
  34.         */
  35.  
  36.         System.out.println("---     MESA     ---");
  37.  
  38.         boolean flag=false;
  39.  
  40.         do{
  41.             System.out.println("Introduza o Username:");
  42.             String user=Ler.umaString();
  43.             System.out.println("Introduza a Password");
  44.             String passwd = Ler.umaString();
  45.  
  46.             stmt = con.createStatement();
  47.             rs = stmt.executeQuery("SELECT * FROM Colaboradores WHERE Username='" +user+ "' AND Password="+passwd);
  48.             colaboradorID=rs.getInt("Id");
  49.  
  50.             if(!rs.next())
  51.             {
  52.                 System.out.println("Username e/ou password incorretos, tente novamente!\n");
  53.             }
  54.             else
  55.             {
  56.                 flag=true;
  57.             }
  58.         }while (flag==false);
  59.  
  60.         menu();
  61.     }
  62.  
  63.     static void menu() throws ClassNotFoundException, SQLException {
  64.         int m1=0;
  65.  
  66.         String url = "jdbc:mysql://localhost:3306/MakeMyDay";
  67.         Class.forName ("com.mysql.jdbc.Driver");
  68.         Connection con = DriverManager.getConnection (url, "root", "admin");
  69.  
  70.         do {
  71.  
  72.             System.out.println("1 - Abrir Mesa");
  73.             System.out.println("2 - Inserir Produtos");
  74.             System.out.println("3 - Pedir Conta");
  75.             System.out.println("4 - Fechar Mesa");
  76.             System.out.println("5 - Marcar Mesa como Pronta");
  77.             m1=Ler.umInt();
  78.  
  79.             switch (m1) {
  80.                 case 1:
  81.  
  82.                     System.out.println("Introduza o ID do posto");
  83.                     int posto = Ler.umInt();
  84.                     System.out.println("Introduza o ID da mesa");
  85.                     int mesa = Ler.umInt();
  86.                     String mes= String.valueOf(mesa);
  87.                     String pos=String.valueOf(posto);
  88.                     String concatmesa=pos+mes;
  89.                     int concat= Integer.parseInt(concatmesa);
  90.  
  91.  
  92.                     Statement ped=null;
  93.                     ResultSet res=null;
  94.  
  95.                     ped = con.createStatement();
  96.                     res = ped.executeQuery("UPDATE Mesas set Estado='Aberto' WHERE PostoId='"+posto+"' AND NumeroMesa='"+concatmesa+"'");
  97.  
  98.                     ped=null;
  99.                     res=null;
  100.  
  101.                     res = ped.executeQuery("INSERT INTO Pedido (ColaboradorId, MesaId, PostoId) VALUES ('"+colaboradorID+"','"+concatmesa+"','"+posto+"');");
  102.  
  103.                     ped=null;
  104.                     res=null;
  105.  
  106.                     res = ped.executeQuery("SELECT IdPedido FROM Pedido WHERE ColaboradorId='"+colaboradorID+"' AND MesaId='"+concatmesa+"' AND PostoId='"+posto+"'");
  107.  
  108.                     int guardaidped = 0;
  109.                     while(res != null && res.next()){
  110.                         guardaidped=res.getInt("IdPedido");
  111.                     }
  112.  
  113.                     int stop=1;
  114.                     do{
  115.                         System.out.println("Introduza o nome do produto");
  116.                         int produtospedidos = Ler.umInt();
  117.                         System.out.println("Introduza a quantidade");
  118.                         int quantidade = Ler.umInt();
  119.  
  120.                         Statement pp=null;
  121.                         ResultSet prodp=null;
  122.  
  123.                         pp = con.createStatement();
  124.                         prodp = pp.executeQuery("INSERT INTO ProdutosPedidos (PedidoId, Quantidade, ProdutoId) VALUES ('"+guardaidped+"','"+quantidade+"','"+produtospedidos+"');");
  125.  
  126.                         do {
  127.                             System.out.println("Pretende adicionar outro produto?\n1 - Sim\n2-Não\n");
  128.                             stop = Ler.umInt();
  129.                             if(stop!=1 && stop !=2)
  130.                             {
  131.                                 System.out.println("Valor introduzido não reconhecido! Tente novamente.");
  132.                             }
  133.  
  134.                         }while(stop!=1 && stop !=2);
  135.                     }while (stop==1);
  136.  
  137.                     System.out.println("1 - Servir em simultaneo");
  138.                     System.out.println("2 - Servir em separado");
  139.                     int notas=Ler.umInt();
  140.  
  141.                     Statement tal=null;
  142.                     ResultSet talao=null;
  143.  
  144.                     if (notas==1)
  145.                     {
  146.                         talao=null;
  147.                         tal=null;
  148.  
  149.                         tal = con.createStatement();
  150.                         talao = tal.executeQuery("INSERT INTO Talao (PedidoId, Estado, Notas, PostoId) VALUES ('"+guardaidped+"', 'Aberto', 'Simultaneo', '1')");
  151.  
  152.                         talao=null;
  153.                         tal=null;
  154.  
  155.                         talao = tal.executeQuery("INSERT INTO Talao (PedidoId, Estado, Notas, PostoId) VALUES ('"+guardaidped+"', 'Aberto', 'Simultaneo', '2')");
  156.                     }
  157.                     else
  158.                     {
  159.                         talao=null;
  160.                         tal=null;
  161.                         talao = tal.executeQuery("INSERT INTO Talao (PedidoId, Estado, Notas, PostoId) VALUES ('"+guardaidped+"', 'Aberto', 'Separado', '1')");
  162.  
  163.                         talao=null;
  164.                         tal=null;
  165.                         talao = tal.executeQuery("INSERT INTO Talao (PedidoId, Estado, Notas, PostoId) VALUES ('"+guardaidped+"', 'Aberto', 'Separado', '2')");
  166.                     };
  167.                     System.out.println("Pedido feito com sucesso!");
  168.                     menu();
  169.  
  170.                 case 2:
  171.                     System.out.println("Introduza o ID do posto");
  172.                     posto = Ler.umInt();
  173.                     System.out.println("Introduza o ID da mesa");
  174.                     mesa = Ler.umInt();
  175.                     mes= String.valueOf(mesa);
  176.                     pos=String.valueOf(posto);
  177.                     concatmesa=pos+mes;
  178.                     concat= Integer.parseInt(concatmesa);
  179.  
  180. //VER QUAL É O PEDIDO MAIS RECENTE COM O ID DA MESA EM QUE A MESA ESTEJA EM ABERTO
  181.  
  182.                     ped=null;
  183.                     res=null;
  184.  
  185.                     ped = con.createStatement();
  186.                     res = ped.executeQuery("SELECT IdPedido FROM Pedido WHERE MesaId='"+concatmesa+"' AND PostoId='"+posto+"'");
  187.  
  188.                     guardaidped = 0;
  189.                     while(res != null && res.next()){
  190.                         guardaidped=res.getInt("IdPedido");
  191.                     }
  192.  
  193.                     stop=1;
  194.                     do{
  195.                         System.out.println("Introduza o nome do produto");
  196.                         int produtospedidos = Ler.umInt();
  197.                         System.out.println("Introduza a quantidade");
  198.                         int quantidade = Ler.umInt();
  199.  
  200.                         Statement pp=null;
  201.                         ResultSet prodp=null;
  202.  
  203.                         pp = con.createStatement();
  204.                         prodp = pp.executeQuery("INSERT INTO ProdutosPedidos (PedidoId, Quantidade, ProdutoId) VALUES ('"+guardaidped+"','"+quantidade+"','"+produtospedidos+"');");
  205.  
  206.                         do {
  207.                             System.out.println("Pretende adicionar outro produto?\n1 - Sim\n2-Não\n");
  208.                             stop = Ler.umInt();
  209.                             if(stop!=1 && stop !=2)
  210.                             {
  211.                                 System.out.println("Valor introduzido não reconhecido! Tente novamente.");
  212.                             }
  213.  
  214.                         }while(stop!=1 && stop !=2);
  215.  
  216.                     }while (stop==1);
  217.  
  218.                     System.out.println("1 - Servir em simultaneo");
  219.                     System.out.println("2 - Servir em separado");
  220.                     notas=Ler.umInt();
  221.  
  222.                     tal=null;
  223.                     talao=null;
  224.  
  225.                     if (notas==1)
  226.                     {
  227.                         talao=null;
  228.                         tal=null;
  229.                         tal = con.createStatement();
  230.                         talao = tal.executeQuery("INSERT INTO Talao (PedidoId, Estado, Notas, PostoId) VALUES ('"+guardaidped+"', 'Aberto', 'Simultaneo', '1')");
  231.                         talao=null;
  232.                         tal=null;
  233.  
  234.                         talao = tal.executeQuery("INSERT INTO Talao (PedidoId, Estado, Notas, PostoId) VALUES ('"+guardaidped+"', 'Aberto', 'Simultaneo', '2')");
  235.                     }
  236.                     else
  237.                     {
  238.                         talao=null;
  239.                         tal=null;
  240.  
  241.                         talao = tal.executeQuery("INSERT INTO Talao (PedidoId, Estado, Notas, PostoId) VALUES ('"+guardaidped+"', 'Aberto', 'Separado', '1')");
  242.  
  243.                         talao=null;
  244.                         tal=null;
  245.  
  246.                         talao = tal.executeQuery("INSERT INTO Talao (PedidoId, Estado, Notas, PostoId) VALUES ('"+guardaidped+"', 'Aberto', 'Separado', '2')");
  247.                     };
  248.                     System.out.println("Pedido feito com sucesso!");
  249.                     menu();
  250.  
  251.                 case 3:
  252.                     System.out.println("Introduza o ID do posto");
  253.                     posto = Ler.umInt();
  254.                     System.out.println("Introduza o ID da mesa");
  255.                     mesa = Ler.umInt();
  256.                     mes= String.valueOf(mesa);
  257.                     pos=String.valueOf(posto);
  258.                     concatmesa=pos+mes;
  259.                     concat= Integer.parseInt(concatmesa);
  260.  
  261. //VER QUAL É O PEDIDO MAIS RECENTE COM O ID DA MESA EM QUE A MESA ESTEJA EM ABERTO
  262.  
  263.                     ped=null;
  264.                     res=null;
  265.  
  266.                     ped = con.createStatement();
  267.                     res = ped.executeQuery("SELECT IdPedido FROM Pedido WHERE MesaId='"+concatmesa+"' AND PostoId='"+posto+"'");
  268.  
  269.                     guardaidped = 0;
  270.                     while(res != null && res.next()){
  271.                         guardaidped=res.getInt("IdPedido");
  272.                     }
  273.  
  274.                     Statement talao=null;
  275.                     ResultSet talaoconf=null;
  276.  
  277.                     talao = con.createStatement();
  278.                     talaoconf = talao.executeQuery("SELECT * FROM Produto WHERE IdProduto = (SELECT ProdutoId FROM ProdutosPId WHERE PedidoId='"+guardaidped+"'");
  279.  
  280.                     float total = 0;
  281.  
  282.                     while(talaoconf != null && talaoconf.next()){
  283.                         guardaidped=res.getInt("IdPedido");
  284.                         System.out.println("   Produto         Preço unitário        Quantidade  ");
  285.                         System.out.println(talaoconf.getString("Designacao")+"         "+talaoconf.getString("Preco")+"         "+talaoconf.getString("Quantidade"));
  286.                         total=total+(talaoconf.getFloat("Preco")*talaoconf.getInt("Quantidade"));
  287.                     }
  288.                     System.out.println("Total: "+total);
  289.  
  290.                     int ver=1;
  291.                     do{
  292.                         System.out.println("O talão apresentado confere com o seu pedido?\n1 - Sim  |  2 - Não");
  293.                         ver=Ler.umInt();
  294.                         if(ver!=1 && ver!=2)
  295.                         {
  296.                             System.out.println("Valor inválido. Tente novamente!");
  297.                         }
  298.                     }while (ver!=1 && ver!=2);
  299.  
  300.                     if(ver==1)
  301.                     {
  302.                         talao=null;
  303.                         talaoconf=null;
  304.  
  305.                         talao = con.createStatement();
  306.                         talaoconf = talao.executeQuery("UPDATE TalaoConf SET Estado='Conferido' WHERE PedidoId='"+guardaidped+"'");
  307.                     }
  308.                     else
  309.                     {
  310.                         talao=null;
  311.                         talaoconf=null;
  312.                         talao = con.createStatement();
  313.                         talaoconf = talao.executeQuery("UPDATE TalaoConf SET Estado='Errado' WHERE PedidoId='"+guardaidped+"'");
  314.                     }
  315.                     menu();
  316.  
  317.                 case 4:
  318.  
  319.  
  320.                     menu();
  321.  
  322.             }
  323.         } while (m1 != 1 && m1 != 2 && m1 != 3 && m1 != 4 && m1 != 5);
  324.  
  325.     }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement