用Androidstudio做一个校园订餐系统
时间: 2024-01-30 22:01:43 浏览: 80
实现校园订餐系统需要以下几个步骤:
1. 设计数据库
首先需要设计数据库,至少需要包含以下几个表:用户表、商家表、菜品表、订单表。其中,用户表用于存储用户信息,商家表用于存储商家信息,菜品表用于存储菜品信息,订单表用于存储订单信息。
2. 实现用户登录和注册功能
在应用中创建登录和注册页面,用户输入账号和密码进行登录或注册。登录时需要验证用户输入的账号和密码是否正确,注册时需要将用户信息插入到用户表中。
3. 实现商家列表和菜品列表展示功能
在应用中创建商家列表和菜品列表页面,商家列表页面展示所有商家的信息,菜品列表页面展示某个商家的所有菜品信息。需要从数据库中查询商家和菜品信息,并将其展示在页面中。
4. 实现订单功能
在应用中创建订单页面,用户在该页面中可以选择商家和菜品,并下单购买。下单前需要验证用户是否已登录,如果未登录则需要先登录。下单后将订单信息插入到订单表中。
以下是一些示例代码,供参考:
1. 创建数据库:
```sql
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS shops (
shop_id INTEGER PRIMARY KEY AUTOINCREMENT,
shop_name TEXT NOT NULL,
shop_address TEXT NOT NULL,
shop_phone TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS dishes (
dish_id INTEGER PRIMARY KEY AUTOINCREMENT,
dish_name TEXT NOT NULL,
dish_price REAL NOT NULL,
shop_id INTEGER NOT NULL,
FOREIGN KEY (shop_id) REFERENCES shops(shop_id)
);
CREATE TABLE IF NOT EXISTS orders (
order_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
shop_id INTEGER NOT NULL,
dish_id INTEGER NOT NULL,
order_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (shop_id) REFERENCES shops(shop_id),
FOREIGN KEY (dish_id) REFERENCES dishes(dish_id)
);
```
2. 实现登录和注册功能:
```java
public boolean login(String username, String password) {
SQLiteDatabase db = getReadableDatabase();
String[] columns = {"user_id"};
String selection = "username = ? AND password = ?";
String[] selectionArgs = {username, password};
Cursor cursor = db.query("users", columns, selection, selectionArgs, null, null, null);
boolean success = cursor.moveToFirst();
cursor.close();
db.close();
return success;
}
public boolean register(String username, String password) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", username);
values.put("password", password);
long rowId = db.insert("users", null, values);
db.close();
return rowId != -1;
}
```
3. 实现商家和菜品列表展示功能:
```java
public List<Shop> getShops() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query("shops", null, null, null, null, null, null);
List<Shop> shops = new ArrayList<>();
while (cursor.moveToNext()) {
int shopId = cursor.getInt(cursor.getColumnIndex("shop_id"));
String shopName = cursor.getString(cursor.getColumnIndex("shop_name"));
String shopAddress = cursor.getString(cursor.getColumnIndex("shop_address"));
String shopPhone = cursor.getString(cursor.getColumnIndex("shop_phone"));
Shop shop = new Shop(shopId, shopName, shopAddress, shopPhone);
shops.add(shop);
}
cursor.close();
db.close();
return shops;
}
public List<Dish> getDishes(int shopId) {
SQLiteDatabase db = getReadableDatabase();
String[] columns = {"dish_id", "dish_name", "dish_price"};
String selection = "shop_id = ?";
String[] selectionArgs = {String.valueOf(shopId)};
Cursor cursor = db.query("dishes", columns, selection, selectionArgs, null, null, null);
List<Dish> dishes = new ArrayList<>();
while (cursor.moveToNext()) {
int dishId = cursor.getInt(cursor.getColumnIndex("dish_id"));
String dishName = cursor.getString(cursor.getColumnIndex("dish_name"));
double dishPrice = cursor.getDouble(cursor.getColumnIndex("dish_price"));
Dish dish = new Dish(dishId, dishName, dishPrice);
dishes.add(dish);
}
cursor.close();
db.close();
return dishes;
}
```
4. 实现订单功能:
```java
public boolean placeOrder(int userId, int shopId, int dishId) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("user_id", userId);
values.put("shop_id", shopId);
values.put("dish_id", dishId);
long rowId = db.insert("orders", null, values);
db.close();
return rowId != -1;
}
```
以上是一个简单的实现示例,具体实现方式可能因应用需求而有所不同。
阅读全文