C/C++教程

酒店管理系统(c++课程设计)

本文主要是介绍酒店管理系统(c++课程设计),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

这是一个用c++编写的一个课程设计的代码:酒店管理系统

大概实现了一下操作:对订单进行增删查改,对订单文件保存,对订单数据保存进入ordering.dat,对订单文件的销毁。

如有不足,请指正~~

Ordering.h

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;

class Ordering
{
    public:
        Ordering();
        Ordering(string , string , string , string , string);
        virtual ~Ordering();

        string GetId() { return id; }
        string GetName() { return name; }
        friend ostream& operator<<(ostream& , const Ordering& );
        friend istream& operator>>(istream& , Ordering& );


    private:
        string name;//姓名
        string tel;//电话号码
        string time;//订餐时间
        string remarks;//备注
        string id;//房间号
};

Hotel.h

#include "Ordering.h"
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;

class Hotel 
{
public:
    Hotel();
    ~Hotel();
    bool AddOrder(Ordering);
    bool DeleteOrder(string);
    bool FindOrder(string);
    void DispAll();
    void SaveToFile();
    void ReadFromFile();
    void DestroyOrder();
    int cnt;//人数

private:
    Ordering addr[50];
   
};

Hotel.cpp

#include"Hotel.h"
#include<iostream>
#include<string>
#include<cstring>
#include <fstream>
#include<istream>
#include<vector>
using namespace std;
int N = 50;
Hotel::Hotel()
{
    cnt = 0;
}

Hotel::~Hotel()
{
    
}

bool Hotel::AddOrder(Ordering ord) {
    int i;
    for (i = 0; i < cnt; i++) {
        if (addr[i].GetId()==ord.GetId()) {
            cout << "这个已经存在,无法加入!" << endl;
            return false;
        }
    }
    if (cnt < N && i == cnt){
        addr[cnt] = ord;
        cnt++;
        cout << "订单添加成功." << endl;
        return true;
    }
    else  return false;
}

void Hotel::DispAll() {
    Ordering ord;
    cout << " 房间号" << "\t姓名" << "\t\t电话号码" << "\t\t预定时间" << "\t备注" << endl;
    for (int i = 1; i <= 54; i++)
        cout << "=";
    cout << endl;
    for (int i = 0; i < cnt; i++) {
        ord= addr[i];
        cout << ord;
    }
}

bool Hotel::DeleteOrder(string id) {
    int i;
    for (i = 0; i < cnt; i++) {
        if (addr[i].GetId()==id) {
            int j = i;
            for (; j < cnt; j++) {
                addr[j] = addr[j + 1];
            }
            cout << "这个订单的信息已经删除!" << endl;
            cnt--;
            return true;
        }
    }
    if (i == cnt) {
        cout << "这个订单没有找到,无法删除" << endl;
        return false;
    }
    return false;
}

bool Hotel::FindOrder(string id) {
    int i;
    Ordering ord;
    for (i = 0; i < cnt; i++) {
        ord = addr[i];
        if (ord.GetId()==id) {
            cout << "订单信息找到!" << endl;
            cout << " 房间号" << "\t姓名" << "\t\t电话号码" << "\t\t预定时间" << "\t备注" << endl;
            for (int i = 1; i <= 54; i++)
                cout << "=";
            cout << endl;
            //cout << sta.id << '\t' << sta.name << '\t' << sta.department << '\t' << sta.wage << '\t' << sta.position << endl;
            cout << ord;
            return true;
        }
    }
    if (i == cnt) {
        cout << "订单信息没有找到!" << endl;
        return false;
    }
    return false;
}

void Hotel::SaveToFile()
{
    ofstream ofs("ordering.dat", ios::binary | ios::out | ios::trunc);
    if (!ofs)
    {
        cerr << "Write file error." << endl;
        return;
    }
    for (int i = 0; i < cnt; i++)
        ofs.write((char*)&addr[i], sizeof(addr[i]));
    ofs.close();
    cout << "文件保存完成!" << endl;
}

void Hotel::ReadFromFile() {
    ifstream ifs("odering.dat", ios::binary | ios::in);
    if (!ifs) {
        cerr << "Open the file error." << endl;
        cout << "请创建文件:ordering.dat,开始创建..." << endl;
        ofstream ofs("odering.dat", ios::binary | ios::trunc);
        ofs.close();
        cout << "空文件创建完成!" << endl;
    }
    else {
        ifs.seekg(0, ios::end);
        long filelen = ifs.tellg();
        cnt = filelen / sizeof(Ordering);
        cout << "文件已打开,请注意修改后及时保存!!" << endl;
        ifs.seekg(0, ios::beg);
        for (int i = 0; i < cnt; i++)
            ifs.read((char*)&addr[i], sizeof(addr[i]));
        ifs.close();
    }
}

void Hotel::DestroyOrder()
{
    char ch;
    cout << "危险!!你确认要清空说有订单信息?(y/n):";

    while ((ch = getchar()) != '\n')
        continue;
    cin.get(ch);
    if (ch == 'y' || ch == 'Y')
    {
        ofstream ofs("ordering.dat", ios::binary | ios::trunc);
        ofs.close();
        cout << "订单信息已清空!" << endl;
        cnt = 0;
    }
}

Ordering.cpp

#include<iostream>
#include<string>
#include<cstring>
#include"Ordering.h"
#include<vector>

Ordering::Ordering()
{
	this->id = "";
	this->name = "";
	this->remarks = "";
	this->tel = "";
	this->time = "";
}
Ordering::Ordering(string name, string tel, string time, string remarks, string room)
{
	this->id = id;
	this->name = name;
	this->remarks = remarks;
	this->tel = tel;
	this->time = time;
}
Ordering::~Ordering(){}

ostream& operator<<(ostream& o, const Ordering& ord) {
	o << ord.id << '\t' << ord.name << "\t\t" << ord.tel << '\t' << ord.time << '\t' << ord.remarks << endl;
	return o;
}
istream& operator>>(istream& in, Ordering& ord) {
	cout << "请输入房间号:";
	in >> ord.id;
	cout << "请输入姓名:";
	in >> ord.name;
	cout << "请输入电话号码:";
	in >> ord.tel;
	cout << "请输入订餐时间:";
	in >> ord.time;
	cout << "请输入订餐备注:";
	in >> ord.remarks;
	return in;
}

main.cpp

#include"Hotel.h"
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
const int NUM = 30;
const char password[] = "wslxc";
Hotel *hol = new Hotel();

void login() {
    int c = 3, count = 1;
    char key[NUM];
    cout << "请输入登录密码(您有三次机会):";
    cin >> key;
    while (strcmp(key, password) != 0) {
        if (count < 3)
            cout << "密码错误,您还有" << --c << "次机会,请重新输入:";
        if (count >= 3)
        {
            cout << "**抱歉,输入错误三次,退出系统**\n";
            exit(0);
        }
        cin >> key;
        count++;
    }
    cout << "密码正确,欢迎使用!\n\n";
    system("pause");
}

void ShowMainMenu()
{
    system("cls");
    cout << "请选择您的操作:" << endl;
    cout << "****************************" << endl;
    cout << "     1 打开订餐信息表" << endl;
    cout << "     2 登记订餐信息" << endl;
    cout << "     3 查找订餐信息" << endl;
    cout << "     4 显示所有订餐信息" << endl;
    cout << "     5 删除订餐信息" << endl;
    cout << "     6 保存订餐信息" << endl;
    cout << "     7 清空订餐信息表" << endl;
    cout << "     0 退出" << endl;
    cout << "****************************" << endl;
}


void Welcome()
{
    cout << endl;
    cout << "     $-------------------------------------------------$" << endl;
    cout << "     |                                                 |" << endl;
    cout << "     |      **欢迎进入酒店订餐信息管理程序 **          | " << endl;
    cout << "     |                                                 |" << endl;
    cout << "     @-------------------------------------------------@" << endl;
}


void SelectFun(int sel) {
    Ordering *order=new Ordering();
    string id;
    switch (sel) {
    case 1:
        hol->ReadFromFile();
        system("pause");
        break;
    case 2:
        cout << "请输入要添加的订餐信息:" << endl;
        cin >> *order;
        hol->AddOrder(*order);
        system("pause");
        break;
    case 3:
        cout << "请输入要查找的房间号:";
        cin >> id;
        hol->FindOrder(id);
        system("pause");
        break;
    case 4:
        cout << "所有的订餐信息如下:" << endl;
        hol->DispAll();
        system("pause");
        break;
    case 5:
        cout << "请输入要删除的房间号:";
        cin >> id;
        hol->DeleteOrder(id);
        system("pause");
        break;
    case 6:
        hol->SaveToFile();
        system("pause");
        break;
    case 7:
        hol->DestroyOrder();
        system("pause");
        break;
    case 0:
        break;
    default:
    {
        cout << "输入错误,请重新输入!" << endl;
        system("pause");
    }
    }
}


int main()
{
    int sel = 1;
    Welcome();
    login();
    while (sel)
    {
        ShowMainMenu();
        cout << "订餐总人数:" << hol->cnt << ",请输入选择功能(0-7):";
        cin >> sel;
        cout << endl;
        SelectFun(sel);
    }
    delete hol;
    return 0;

}

这篇关于酒店管理系统(c++课程设计)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!