TypeScript 语言 实战项目 酒店管理系统

阿木 发布于 14 小时前 5 次阅读


酒店管理系统实战项目:TypeScript语言下的代码实现

随着旅游业的蓬勃发展,酒店行业也日益繁荣。为了提高酒店的管理效率和服务质量,开发一套完善的酒店管理系统显得尤为重要。本文将围绕TypeScript语言,详细介绍如何实现一个酒店管理系统,包括需求分析、技术选型、核心功能实现以及部署上线。

需求分析

在开始项目之前,我们需要明确酒店管理系统的需求。以下是一些基本的功能需求:

1. 用户管理:包括员工、客户等角色的注册、登录、权限管理。
2. 房间管理:包括房间类型、价格、状态等信息的维护。
3. 预订管理:包括预订查询、预订确认、订单管理等功能。
4. 报表统计:包括入住率、收入等数据的统计和分析。
5. 系统设置:包括系统参数、日志管理等。

技术选型

基于以上需求,我们选择以下技术栈:

- 前端:TypeScript + React
- 后端:Node.js + Express
- 数据库:MongoDB
- 版本控制:Git
- 构建工具:Webpack

环境搭建

1. 安装Node.js:从官网下载并安装Node.js,确保环境变量配置正确。
2. 安装MongoDB:下载并安装MongoDB,启动数据库服务。
3. 创建项目目录:在本地创建一个项目目录,例如`hotel-management-system`。
4. 初始化项目:使用npm初始化项目,并安装相关依赖。

bash
mkdir hotel-management-system
cd hotel-management-system
npm init -y
npm install express mongoose react react-dom react-router-dom

核心功能实现

用户管理

1. 创建用户模型:使用Mongoose连接MongoDB,并定义用户模型。

typescript
import mongoose from 'mongoose';

const userSchema = new mongoose.Schema({
username: { type: String, required: true },
password: { type: String, required: true },
role: { type: String, required: true }
});

export const User = mongoose.model('User', userSchema);

2. 注册、登录接口:使用Express创建注册、登录接口。

typescript
import express from 'express';
import User from './models/User';

const app = express();
app.use(express.json());

// 注册接口
app.post('/register', async (req, res) => {
const { username, password, role } = req.body;
const user = new User({ username, password, role });
await user.save();
res.send('注册成功');
});

// 登录接口
app.post('/login', async (req, res) => {
const { username, password } = req.body;
const user = await User.findOne({ username, password });
if (user) {
res.send('登录成功');
} else {
res.status(401).send('用户名或密码错误');
}
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

房间管理

1. 创建房间模型:使用Mongoose定义房间模型。

typescript
import mongoose from 'mongoose';

const roomSchema = new mongoose.Schema({
type: { type: String, required: true },
price: { type: Number, required: true },
status: { type: String, required: true }
});

export const Room = mongoose.model('Room', roomSchema);

2. 房间列表接口:使用Express创建房间列表接口。

typescript
import express from 'express';
import Room from './models/Room';

const app = express();
app.use(express.json());

// 房间列表接口
app.get('/rooms', async (req, res) => {
const rooms = await Room.find();
res.send(rooms);
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

预订管理

1. 创建预订模型:使用Mongoose定义预订模型。

typescript
import mongoose from 'mongoose';

const bookingSchema = new mongoose.Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
room: { type: mongoose.Schema.Types.ObjectId, ref: 'Room', required: true },
checkIn: { type: Date, required: true },
checkOut: { type: Date, required: true }
});

export const Booking = mongoose.model('Booking', bookingSchema);

2. 预订接口:使用Express创建预订接口。

typescript
import express from 'express';
import Booking from './models/Booking';

const app = express();
app.use(express.json());

// 预订接口
app.post('/bookings', async (req, res) => {
const { user, room, checkIn, checkOut } = req.body;
const booking = new Booking({ user, room, checkIn, checkOut });
await booking.save();
res.send('预订成功');
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

报表统计

1. 统计入住率:使用Mongoose查询入住的房间数量,并计算入住率。

typescript
import mongoose from 'mongoose';
import Room from './models/Room';
import Booking from './models/Booking';

const calculateOccupancyRate = async () => {
const occupiedRooms = await Booking.countDocuments({ checkOut: { $gte: new Date() } });
const totalRooms = await Room.countDocuments();
const occupancyRate = (occupiedRooms / totalRooms) 100;
console.log(`Occupancy rate: ${occupancyRate.toFixed(2)}%`);
};

calculateOccupancyRate();

2. 统计收入:使用Mongoose查询预订记录,并计算总收入。

typescript
import mongoose from 'mongoose';
import Booking from './models/Booking';

const calculateTotalRevenue = async () => {
const bookings = await Booking.find();
const totalRevenue = bookings.reduce((acc, booking) => {
const days = Math.floor((booking.checkOut.getTime() - booking.checkIn.getTime()) / (1000 3600 24));
return acc + booking.room.price days;
}, 0);
console.log(`Total revenue: $${totalRevenue.toFixed(2)}`);
};

calculateTotalRevenue();

部署上线

1. 打包项目:使用Webpack打包前端项目,生成静态文件。
2. 配置服务器:使用Node.js搭建服务器,并配置路由。
3. 数据库迁移:将本地数据库迁移到线上数据库。
4. 域名解析:将域名解析到服务器IP地址。

总结

本文以TypeScript语言为基础,详细介绍了如何实现一个酒店管理系统。通过使用React和Express框架,结合MongoDB数据库,我们成功实现了用户管理、房间管理、预订管理、报表统计等功能。在实际开发过程中,可以根据需求进行功能扩展和优化。希望本文能对您在TypeScript语言下的实战项目开发有所帮助。