Advertisement
Week045

Lab5

Nov 14th, 2023
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 11.25 KB | None | 0 0
  1. import java.awt.event.{ActionEvent, ActionListener}
  2. import javax.swing.{JButton, JFrame, JPanel, SwingUtilities}
  3. import java.awt.Dimension
  4. import javax.swing.{JFrame, JLabel,JTextField, JPanel, SwingUtilities}
  5. import java.sql.{Connection, DriverManager, ResultSet}
  6. import java.awt.Color
  7. import javax.swing.JTextArea
  8. import javax.swing.JScrollPane
  9. object ButtonModule  {
  10.   val frame = new JFrame("My Application")
  11.   frame.setSize(720, 400)
  12.   frame.setLayout(null)
  13.   frame.getContentPane().setBackground(Color.PINK)
  14.  
  15.   def main(args: Array[String]): Unit = {
  16.        SwingUtilities.invokeLater(() => {
  17.          
  18.          val label1=new JLabel("Title")
  19.          val label2=new JLabel("Price")
  20.          val label3=new JLabel("Count")
  21.          val label4=new JLabel("Debug")
  22.          
  23.          label1.setBounds(75,70,100,20)
  24.          label2.setBounds(235,70,70,20)
  25.          label3.setBounds(410,70,100,20)
  26.          label4.setBounds(150, 150, 100, 20)
  27.          
  28.          label1.setForeground(Color.DARK_GRAY)
  29.          label2.setForeground(Color.ORANGE)
  30.          label3.setForeground(Color.WHITE)
  31.          label4.setForeground(Color.RED)
  32.        
  33.          val button1 = new JButton("Insert")
  34.          val button2 = new JButton("Select")
  35.          val button3 = new JButton("Get max profit")
  36.          val button4 = new JButton("Get not less")
  37.          
  38.          val button5 = new JButton("Task #1")
  39.          val button6 = new JButton("Task #2")
  40.          
  41.          button1.setBackground(Color.GREEN)
  42.          button2.setBackground(Color.RED)
  43.          button3.setBackground(Color.YELLOW)
  44.          button4.setBackground(Color.CYAN)
  45.          
  46.          button1.setForeground(Color.CYAN)
  47.          button2.setForeground(Color.YELLOW)
  48.          button3.setForeground(Color.RED)
  49.          button4.setForeground(Color.GREEN)
  50.        
  51.          val textField = new JTextField(200)
  52.          val textField2 = new JTextField(20)
  53.          val textField3 = new JTextField(20)
  54.          
  55.          val textArea=new JTextArea()
  56.          val scrollPane = new JScrollPane(textArea)
  57.        
  58.          val debugTextArea = new JTextArea()
  59.          val debugScrollPane = new JScrollPane(debugTextArea)
  60.          
  61.          debugTextArea.setBackground(Color.LIGHT_GRAY)
  62.          textArea.setBackground(Color.LIGHT_GRAY)
  63.        
  64.          button1.setBounds(20,30,130,20)
  65.          button2.setBounds(190,30,130,20)
  66.          button3.setBounds(360,30,130,20)
  67.          button4.setBounds(530,30,130,20)
  68.          
  69.          button5.setBounds(530,250,130,20)
  70.          button6.setBounds(530,300,130,20)
  71.          
  72.          textField.setBounds(20,100,130,20)
  73.          textField2.setBounds(190,100,130,20)
  74.          textField3.setBounds(360,100,130,20)
  75.          scrollPane.setBounds(500,100,180,120)
  76.          debugScrollPane.setBounds(65, 180, 210, 150)
  77.        
  78.          frame.add(button1)
  79.          frame.add(button2)
  80.          frame.add(button3)
  81.          frame.add(button4)
  82.          frame.add(button5)
  83.          frame.add(button6)
  84.          frame.add(label1)
  85.          frame.add(textField)
  86.          frame.add(label2)
  87.          frame.add(textField2)
  88.          frame.add(label3)
  89.          frame.add(textField3)
  90.          frame.add(scrollPane)
  91.          frame.add(label4)
  92.          frame.add(debugScrollPane)
  93.      
  94.       button1.addActionListener(new ActionListener {
  95.         override def actionPerformed(e: ActionEvent): Unit = {
  96.           val url = "jdbc:mysql://localhost:3306/shop"
  97.           val username = "root"
  98.           val password = "adminadmin"
  99.          
  100.           Class.forName("com.mysql.jdbc.Driver")
  101.           val conn = DriverManager.getConnection(url, username, password)
  102.      
  103.           try {
  104.            val stmt = conn.createStatement()
  105.            val rs = stmt.execute("INSERT INTO products VALUES ('" + textField.getText + "'," + textField2.getText + "," + textField3.getText + ")")
  106.            
  107.            textField.setText("")
  108.            textField2.setText("")
  109.            textField3.setText("")
  110.            
  111.            debugTextArea.append("Added record\n")
  112.           }
  113.           finally {
  114.             conn.close()
  115.           }
  116.         }
  117.        
  118.       })
  119.          
  120.      
  121.       button2.addActionListener(new ActionListener {
  122.         override def actionPerformed(e: ActionEvent): Unit = {
  123.          
  124.           val url = "jdbc:mysql://localhost:3306/shop"
  125.           val username = "root"
  126.           val password = "adminadmin"
  127.      
  128.           Class.forName("com.mysql.jdbc.Driver")
  129.           val conn = DriverManager.getConnection(url, username, password)
  130.      
  131.           try {
  132.              val stmt = conn.createStatement()
  133.              val prod_name= textField.getText().toString().trim()
  134.              val rs = stmt.executeQuery("SELECT * FROM products WHERE name = '"+prod_name+"'")
  135.              while (rs.next()) {
  136.                
  137.                val name = rs.getString("name")
  138.                val price = rs.getInt("price")
  139.                val count = rs.getInt("count")
  140.                
  141.                textField2.setText(""+price)
  142.                textField3.setText(""+count)
  143.                }
  144.           }
  145.           finally {
  146.             conn.close()
  147.           }
  148.         }
  149.        
  150.       })
  151.      
  152.      
  153.       button3.addActionListener(new ActionListener {
  154.         override def actionPerformed(e: ActionEvent): Unit = {
  155.  
  156.           val url = "jdbc:mysql://localhost:3306/shop"
  157.           val username = "root"  
  158.           val password = "adminadmin"
  159.      
  160.           Class.forName("com.mysql.jdbc.Driver")
  161.           val conn = DriverManager.getConnection(url, username, password)
  162.          
  163.            try {
  164.              
  165.              val stmt = conn.createStatement()
  166.              val prod_name= textField.getText().toString().trim()
  167.              val rs = stmt.executeQuery("SELECT * FROM products")
  168.              var max_profit = 0
  169.              var max_product = ""
  170.              var max_price = 0
  171.              var max_count = 0
  172.              while (rs.next()) {
  173.          
  174.                val name = rs.getString("name")
  175.                val price = rs.getInt("price")
  176.                val count = rs.getInt("count")
  177.                
  178.                val current_profit = price*count
  179.                if (current_profit > max_profit){
  180.                  max_profit = current_profit
  181.                  max_product = name
  182.                  max_price = price
  183.                  max_count = count
  184.                }
  185.            
  186.              
  187.              }
  188.              
  189.              debugTextArea.append(max_product + "\n")
  190.              debugTextArea.append(max_price + "\n")
  191.              debugTextArea.append(max_profit + "\n")
  192.              
  193.               //val stmt = conn.createStatement()
  194.               //val prod_name= textField.getText().toString().trim()
  195.               //val rs = stmt.executeQuery("SELECT name, MAX(price * `count`) AS max_total_cost " +
  196.               //                      "FROM products " +
  197.               //                      "GROUP BY name " +
  198.               //                      "ORDER BY max_total_cost DESC "
  199.               //                      )
  200.              
  201.      
  202.               //if (rs.next()) {
  203.               //  val name = rs.getString("name")
  204.               //  val maxTotalCost = rs.getInt("max_total_cost")
  205.      
  206.               //  debugTextArea.append(s"Name: $name\n")
  207.               //  debugTextArea.append(s"Max profit: $maxTotalCost")
  208.               //}
  209.      
  210.                      
  211.              }
  212.              finally {
  213.                conn.close()
  214.              }
  215.         }
  216.        
  217.       })
  218.      
  219.      
  220.       button4.addActionListener(new ActionListener {
  221.         override def actionPerformed(e: ActionEvent): Unit = {
  222.        
  223.           val url = "jdbc:mysql://localhost:3306/shop"
  224.           val username = "root"
  225.           val password = "adminadmin"
  226.      
  227.           Class.forName("com.mysql.jdbc.Driver")
  228.           val conn = DriverManager.getConnection(url, username, password)
  229.          
  230.           val LessCount=500
  231.           try {
  232.              val stmt = conn.createStatement()
  233.              val prod_name = textField.getText().toString().trim()
  234.              val query =
  235.               s"""
  236.                 |SELECT name, price, count
  237.                 |FROM products
  238.                 |WHERE price >= $LessCount
  239.                 |""".stripMargin
  240.  
  241.              val rs = stmt.executeQuery(query)
  242.              textArea.append("Name       Price       Count\n")
  243.              
  244.              while (rs.next()) {
  245.                val name = rs.getString("name")
  246.                val price = rs.getInt("price")
  247.                val count = rs.getInt("count")
  248.                
  249.                textArea.append(name + "   "+ price+"     " + count + "\n")
  250.  
  251.              }
  252.           }
  253.           finally {
  254.             conn.close()
  255.           }
  256.         }
  257.        
  258.       })
  259.      
  260.       //Вывести категории товаров, которых меньше определённого количества
  261.       button5.addActionListener(new ActionListener {
  262.         override def actionPerformed(e: ActionEvent): Unit = {
  263.        
  264.           val url = "jdbc:mysql://localhost:3306/shop"
  265.           val username = "root"
  266.           val password = "adminadmin"
  267.          
  268.           Class.forName("com.mysql.jdbc.Driver")
  269.           val conn = DriverManager.getConnection(url, username, password)
  270.          
  271.           val minCount = 10
  272.           try {
  273.              val stmt = conn.createStatement()
  274.              
  275.              
  276.              val rs = stmt.executeQuery(s"SELECT * FROM products GROUP BY `count` HAVING `count` <= $minCount")
  277.              
  278.              debugTextArea.setText("")
  279.              
  280.              while (rs.next()) {
  281.                val name = rs.getString("name")
  282.                val price = rs.getInt("price")
  283.                val count = rs.getInt("count")
  284.                
  285.                debugTextArea.append(name + "   "+ price+"     " + count + "\n")
  286.  
  287.              }
  288.           }
  289.           finally {
  290.             conn.close()
  291.           }
  292.         }
  293.        
  294.       })
  295.      
  296.       //Вывести товары, которое содержат букву e
  297.       button6.addActionListener(new ActionListener {
  298.         override def actionPerformed(e: ActionEvent): Unit = {
  299.        
  300.           val url = "jdbc:mysql://localhost:3306/shop"
  301.           val username = "root"
  302.           val password = "adminadmin"
  303.      
  304.           Class.forName("com.mysql.jdbc.Driver")
  305.           val conn = DriverManager.getConnection(url, username, password)
  306.          
  307.           try {
  308.              val stmt = conn.createStatement()
  309.  
  310.              val rs = stmt.executeQuery("SELECT * FROM products WHERE LOCATE('e', name) > 0")
  311.              
  312.              debugTextArea.setText("")
  313.              
  314.              while (rs.next()) {
  315.                val name = rs.getString("name")
  316.                val price = rs.getInt("price")
  317.                val count = rs.getInt("count")
  318.                
  319.                debugTextArea.append(name + "   "+ price+"     " + count + "\n")
  320.  
  321.              }
  322.           }
  323.           finally {
  324.             conn.close()
  325.           }
  326.         }
  327.        
  328.       })
  329.  
  330.    
  331.       frame.setBackground(Color.BLUE)
  332.       frame.setLocationRelativeTo(null)
  333.       frame.setVisible(true)
  334.      
  335.       })
  336.   }
  337. }
  338.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement