Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. i) How to create an interface login system for the supermarket management system (Java Swing) in java eclipse for the following codes? ii) I
1. i) How to create an interface login system for the supermarket management system (Java Swing) in java eclipse for the following codes? ii) I need step by step procedure and screenshots for the login system how they work
Supermarket Shopping Management System
Connect to the database:
Database:
gg Home Study tools My courses My books My folder Career Life involves the operation of the database. package com.javayihao.top.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** *@date 2019-12-9 * @Description Connection database tool class * @author com.javayihao.top */ public class DbUtil // Several strings used to connect to the database are defined as constants, which need not be created every time private static final String USER = "root"://Database user name private static final String UPWD = "root"://Database password // Local database shop private static final String URL = "jdbc:mysql://localhost:3306/shop"; //drive private static final String DRIVER = "com.mysql.jdbc.Driver"; //Registration driven static try Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(): } // Get the function of database Connection object Connection public static Connection getConnection() throws SQLException { return DriverManager.getConnection(URL, USER, UPWD): } // Close connected and executed open resources public static void close(Connection connection, Statement statement) { if (statement != null) { try statement.close(); } catch (SQLException e) { e.printStackTrace(); } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } // Close all open resources public static void close(Connection connection, Statement statement, ResultSet rs) { if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } if (connection != null) { try connection.close(); } catch (SQLException e) { e.printStackTrace(); } } if (rs != null) { try rs.close(); } catch (SQLException e) { e.printStackTrace(); } test package com.javayihao.top.test; import java.sql.Connection; import java.sql.SQLException; import com.javayihao.top.utils.DbUtil; *@date 2019-12-9 * @Description Test database connection class @author com.javayihao.top */ public class DbUtilTest{ public static void main(String[] args) throws SQLException { Connection con = DbUtil.getConnection(); System.out.println(con); } The database connection is successful as follows V: create entity class package com.javayihao.top.pojo; /** *@date 2019-12-9 * @Description Commodity entity * @author com.javayihao.top */ public class Good // Commodity number private int id; // Trade name private String name; //Commodity price (the price may involve a small number, and float is used here. Of course, the real large shopping platform will not use float, and interested friends can learn about it online) private float price; // Stock private int num; //Empty parameter structure public Good0 { superO; // Printing method @Override public String toString({ return "Good [id=" + id =" name=" + name + price=" + price +", num=" + num + "\": } //Parametric construction, easy to initialize objects public Good(int id, String name, float price, int num) { superO; this.id = id; this.name = name; this.price = price; this.num = num; } // set get method public int getido { return id; } public void setidint id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice) { return price; } public void setPrice(float price) { this.price = price; } public int getNum) { return num; } public void setNum(int num) { this.num = num; 3 1 egg Home Study tools My courses My books My folder Career Life CREATE TABLE t_good ( id int(5) NOT NULL, 'name' varchar(25) NOT NULL, "price float(10,2) NOT NULL, "num int(5) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; VII. Core business package com.javayihao.top.service; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet: import java.sql.SQLException; import java.util.ArrayList; import java.util.Scanner; import com.javayihao.top.pojo.Good; import com.javayihao.top.utils.DbUtil; * @date 2019-12-9 @Description System main interface * @author com.javayihao.top */ public class ShopView { // Get keyboard input object Scanner input = new Scanner(System.in); /* * System operation method */ public void ShopStart() { System.out.println("=================Welcome to the supermarket shopping management system================='); //Flag quantity whether to continue, default is String is Go="y": dol //Call the function shown in the menu showMenu(); System.out.println("Please enter the action to be performed"); //Accept the input from the keyboard. Use String here to process the number and character input at one time. It is not recommended to use int type numbers String select =input.next(); //Execute the corresponding method according to the input selection switch (select) { // Execute goods warehousing method case "1": insertGood(); break; //Execute commodity query method case 2: System.out.println("Enter the item number to query"); int goodid = input.nextInt(); // Call the method to query the product, Good good = searchGoodById(goodld); //existence if(goodl=null) System.out.println("Commodity number:"+goodld+" Trade name:"-good.getName() +" commodity price:"+good.getPrice()+" Quantity of commodities:"+good.getNum(); }else{ System.out.println("This product does not exist"); 3 break; //Execute product list method case 3: getGoodList(); break; //Execute commodity purchase method case 4": buyGood(); break; // Execute commodity purchase method case "5": System.out.println("Enter item number to delete"); int id = input.nextinto; //Call the method to query the product, if(searchGoodByld(id)!=null){ deleteGood(id); Jelsel System.out.println("There is no such product"); break; case "6": updateGoodo; break; //Exit system case "0" System.out.println("* *Welcome to the next use bye!*************"); //Termination procedure System.exit(0); default: System.err.println("Wrong input, please input again!"); continue; } System.out.println("input y Continue/Otherwise quit"); isGo = input.next(); }while(isGo.equals(y); System.out.println(" **Welcome to the next use bye!**** "); /** Update product action * 1.First query whether the current product inventory to be updated does not exist * 2.If there is an update, there is no prompt */ private void updateGood() { System.out.println("Enter the item to modify id"); int gid = input.nextInto: Good good = searchGoodByld(gid); System.out.println("The product information is as follows"); if good!=null) System.out.println("Commodity number:"+gid+" Trade name:"+good.getName() +" commodity price:"+good.getPrice(+" Quantity of commodities: +good.getNum(); System.out.println("Modify product name"); String name = input.next(: System.out.println("Modify commodity unit price"); float price = input.nextFloat(): System.out.println("Modify commodity inventory); int num = input.nextInto: String sql="update t_good set name=?,price=?,num=? where id=?"; try // Create an object to operate the database Connection con = DbUtil.getConnection(); // Create an execution object PreparedStatement to execute sal PreparedStatement pst = con.prepareStatement(sql): // Set values for placeholders pst.setString(1, name); pst.setFloat(2, price); pst.setInt(3, num); pst.setInt(4, gid); //execute() returns true if it is a query or false if it is an update or insert if(!pst.execute() System.out.println("Update success"); } //Close connection DbUtil.close(con, pst); } catch (Exception e) { e.printStackTrace(): System.out.println("Update exception+e.getMessage(); Jelse{ System.out.println("This product does not exist"); ) } // Method for displaying system interface menu private void showMenu() { System.out.println("1.Goods warehousing"); System.out.println("2Query products according to product number"); System.out.println("3. List of commodities"); System.out.println("4.Purchase goods"); System.out.println("5.Delete merchandise"); System.out.println("6.Update commodity"); System.out.println("O.Exit system"); /* Delete merchandise * 1. First of all, we have to judge the existence of the commodity * 2.Delete according to item id */ private void deleteGood(int id) { String sql = "delete from t_good where id=?"; try { // Create an object to operate the database Connection con=DbUtil.getConnection(); //Create an execution object PreparedStatement to execute sal PreparedStatement pst = con.prepare Statement(sql); // Set values for placeholders pst.setInt(1, id); //execute() returns true if it is a query or false if it is an update or insert if(!pst.execute(X System.out.println("Delete successful"); //Close connection DbUtil.close(con, pst): } catch (Exception e) { e.printStackTrace(: System.out.println("Delete exception"+e.getMessage(); 3 /* * Goods warehousing * Here we only deal with the logic that the number cannot be repeated, * As for whether the input number is a number, there is no judgment here. Interested friends can try it */ private void insertGood // Commodity number int id=0; System.out.println("Enter item number"); while(true) id=input.nextInt(); // Judge whether the number entered is duplicate or not, and re-enter it repeatedly if(searchGoodById(id)==null) break; } System.err.println("Duplicate number, please re-enter product number"); } System.out.println("Enter product name"); String name = input.next(); System.out.println("Enter unit price of goods"); float price = input.nextFloat(); System.out.println("Enter item quantity"); int num = input.nextInt(); //To execute the sql statement, use placeholders here to prevent sql intrusion String sql = "insert into t_good values(?,?,?,?)"; try { // Create an object to operate the database Connection con = DbUtil.getConnection(: //Create an execution object PreparedStatement to execute sal PreparedStatement pst = con.prepareStatement(sql); // Set values for placeholders pst.setInt(1, id); pst.setString(2, name); pst.setFloat(3, price); pst.setInt(4, num): // Execute sql statement if(!pst.execute() System.out.println("Warehousing success"); } // Close connection DbUtil.close(con, pst); } catch (Exception e) { e.printStackTrace(): System.out.println("Abnormal storage"+e.getMessage()); ) } /*Commodity inquiry * The returned object is a product object. If there is no such product, null will be returned */ private Good searchGoodById(int id) { // Executed sql statement String sql="select id, name, price,num from t_good where id=?"; try { //Create an object to operate the database Connection con = DbUtil.getConnection0; // Create an object PreparedStatement to execute sql PreparedStatement pst = con.prepareStatement(sql); pst.setInt(1, id); ResultSet rs = pst.executeQueryo: if(rs.nextOX/As a result, the queried data is encapsulated into a commodity object through the constructor Good good = new Good(rs.getInt("id"), rs.getString("name"), rs.getFloat("price"), rs.getInt("num")); return good; //Close connection DbUtil.close(con, pst); } catch (SQLException e) { e.printStackTrace(); } return null; } // List of commodities private void getGoodList0{ // Executed sql statement String sql="select id, name,price,num from t_good"; try // Create an object to operate the database Connection con = DbUtil.getConnection(; // Create an object PreparedStatement to execute sal PreparedStatement pst = con.prepare Statement(sql); ResultSet rs = pst.executeQuery(); System.out.println("number\t"+"Name\t"+"Unit Price\t"+"Number\t"): if(rs.wasNullOX System.out.println("No goods"); }else{ while(rs.next(X //Results, printing // Through rs.getxxx("yy") method parameter is the database column name System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+ rs.getFloat("price")+"\t+rs.getInt("num")+"\t"); } //Close connection DbUtil.close(con, pst); } catch (SOLException e) { e.printStackTrace(): } //Purchase goods public void buyGood() { // Collection used to store purchased items ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started