canteen_name = ['食堂1', '食堂2', '食堂3', '食堂4', '食堂5']还可以使用哪些高级功能来优化代码?
时间: 2023-10-23 09:11:22 浏览: 41
可以使用 Python 的列表推导式(List Comprehension)来优化代码,将上述代码简化为一行代码:
```
canteen_name = [f'食堂{i}' for i in range(1, 6)]
```
这行代码使用了 f-string 来动态生成字符串,避免了使用字符串拼接。同时使用了列表推导式来避免了使用循环语句。这样的代码更加简洁、清晰、易读。
相关问题
canteen_name = ['食堂1', '食堂2', '食堂3', '食堂4', '食堂5'] man_count = [canteen1, canteen2, canteen3, canteen4, canteen5]优化代码
你可以使用字典来存储食堂名和人数:
```
canteen_dict = {'食堂1': canteen1, '食堂2': canteen2, '食堂3': canteen3, '食堂4': canteen4, '食堂5': canteen5}
```
这样,你就可以通过食堂名来获取对应的人数,而不需要使用两个分开的列表。
用java编写一个简单的使用数据库储存信息的食堂管理系统
好的,下面是一个使用Java语言和MySQL数据库的简单食堂管理系统的示例代码:
1. 首先需要创建MySQL数据库和相应的数据表,例如创建一个名为canteen的数据库,其中有一个名为dishes的数据表,包含字段id、name、price和inventory,用于存储菜品的信息。可以使用以下SQL语句创建该数据表:
```
CREATE DATABASE canteen;
USE canteen;
CREATE TABLE dishes (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
price DECIMAL(8,2) NOT NULL,
inventory INT NOT NULL
);
```
2. 在Java程序中使用JDBC连接MySQL数据库,并实现菜品信息的增删改查功能。可以使用以下代码作为示例:
```
import java.sql.*;
public class CanteenManagementSystem {
// 数据库连接信息
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/canteen?useSSL=false&serverTimezone=UTC";
private static final String JDBC_USERNAME = "root";
private static final String JDBC_PASSWORD = "password";
// 菜品信息增删改查的SQL语句
private static final String INSERT_DISH = "INSERT INTO dishes(name, price, inventory) VALUES (?, ?, ?)";
private static final String DELETE_DISH = "DELETE FROM dishes WHERE id = ?";
private static final String UPDATE_DISH = "UPDATE dishes SET name = ?, price = ?, inventory = ? WHERE id = ?";
private static final String SELECT_DISH = "SELECT * FROM dishes WHERE id = ?";
public static void main(String[] args) {
// 建立数据库连接
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD)) {
// 添加一道新菜
addDish(conn, "红烧肉", 28.0, 50);
// 修改一道菜的信息
updateDish(conn, 1, "红烧排骨", 32.0, 30);
// 查询一道菜的信息
Dish dish = selectDish(conn, 1);
System.out.println(dish);
// 删除一道菜
deleteDish(conn, 1);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加一道新菜
private static void addDish(Connection conn, String name, double price, int inventory) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(INSERT_DISH, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, name);
stmt.setDouble(2, price);
stmt.setInt(3, inventory);
stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
System.out.println("新增菜品成功,ID为" + id);
}
rs.close();
stmt.close();
}
// 修改一道菜的信息
private static void updateDish(Connection conn, int id, String name, double price, int inventory) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(UPDATE_DISH);
stmt.setString(1, name);
stmt.setDouble(2, price);
stmt.setInt(3, inventory);
stmt.setInt(4, id);
int rows = stmt.executeUpdate();
if (rows > 0) {
System.out.println("修改菜品信息成功");
}
stmt.close();
}
// 查询一道菜的信息
private static Dish selectDish(Connection conn, int id) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(SELECT_DISH);
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
Dish dish = null;
if (rs.next()) {
String name = rs.getString("name");
double price = rs.getDouble("price");
int inventory = rs.getInt("inventory");
dish = new Dish(id, name, price, inventory);
}
rs.close();
stmt.close();
return dish;
}
// 删除一道菜
private static void deleteDish(Connection conn, int id) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(DELETE_DISH);
stmt.setInt(1, id);
int rows = stmt.executeUpdate();
if (rows > 0) {
System.out.println("删除菜品成功");
}
stmt.close();
}
// 菜品信息类
private static class Dish {
private int id;
private String name;
private double price;
private int inventory;
public Dish(int id, String name, double price, int inventory) {
this.id = id;
this.name = name;
this.price = price;
this.inventory = inventory;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getInventory() {
return inventory;
}
@Override
public String toString() {
return "菜品信息:ID=" + id + ", 名称=" + name + ", 价格=" + price + ", 库存=" + inventory;
}
}
}
```
以上代码实现了食堂管理系统中的菜品信息的增删改查功能。您可以根据需要进行修改和扩展。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![exr](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)