看了简单工厂模式后,心里总是痒痒的,想要做个小东西来练习一下,看到书上是以c#做了一个简单的商场收银小系统为例子,于是,我便用qt和c++把这个小系统改写了一遍,也算是把这个简单工厂模式练习了一下。
以下是代码:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
double totalprice;
double firstmoney;
double price;
int num;
QString record;
double oldmoney;
double returnmonry;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
class Cash //收费基类
{
public:
virtual void setdis(double dis){ return; } //打折 折扣赋值虚函数
virtual void setmoney(double oldmon, double retmon){ return; } //满减 返利赋值虚函数
virtual double acceptcash(double money) //总计算法虚函数
{
return money;
}
};
class Normal : public Cash //正常收费
{
public:
double acceptcash(double money) //正常收费算法
{
return money;
}
};
class Discount : public Cash //打折收费
{
double m_dis; //折扣
public:
void setdis(double dis) //折扣赋值
{
m_dis = dis;
}
double acceptcash(double money) //打折算法收费
{
return money * m_dis;
}
};
class Returnmoney : public Cash //满减收费 例满300减50
{
double oldmoney; //满的值 此值为300
double returnmoney; //返的值 此值为50
public:
void setmoney(double oldmon, double retmon) //两个值赋值
{
oldmoney = oldmon;
returnmoney = retmon;
}
double acceptcash(double money) //满减算法
{
money = money - money * returnmoney / oldmoney;
return money;
}
};
class CashFoctory //收费算法 工厂类
{
Cash *cash;
public:
Cash* cashcreate(double sec)
{
if(sec == 1.0) //正常收费
{
cash = new Normal;
}
else if(sec == 2.0) //满减收费
{
cash = new Returnmoney;
}
else //打折收费
{
cash = new Discount;
cash->setdis(sec);
}
return cash;
}
};
double my_discount(QString dis)
{
if(dis == "正常收费")
{
return 1.0;
}
if(dis == "打9折")
{
return 0.9;
}
if(dis == "打8折")
{
return 0.8;
}
if(dis == "打7折")
{
return 0.7;
}
if(dis == "打6折")
{
return 0.6;
}
if(dis == "打5折")
{
return 0.5;
}
if(dis == "打4折")
{
return 0.4;
}
if(dis == "打3折")
{
return 0.3;
}
if(dis == "打2折")
{
return 0.2;
}
if(dis == "打1折")
{
return 0.1;
}
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked() //重置按钮槽函数
{
ui->priceedit->clear();
ui->numedit->clear();
}
void Widget::on_pushButton_2_clicked() //确定按钮槽函数
{
price = ui->priceedit->text().toDouble(); //获取单价
num = ui->numedit->text().toInt(); //获取数量
QString dis;
dis = ui->discountbox->currentText(); //获取折扣
double Dis;
Dis = my_discount(dis); //将对应的复选框中的文字转化为折扣
if((ui->oldmoney->text() != "") && (ui->returnmoney->text() != "")) //满减有值的时候
{
oldmoney = ui->oldmoney->text().toDouble();
returnmonry = ui->returnmoney->text().toDouble();
Dis = 2.0;
}
CashFoctory F; //定义工厂类实例
Cash* cash;
cash = F.cashcreate(Dis);
cash->setmoney(oldmoney, returnmonry);
firstmoney = price * num;
totalprice = totalprice + cash->acceptcash(firstmoney); //计算合计
QFont ft;
ft.setPointSize(32);
ft.setBold(true);
ui->totalprice->setFont(ft);
ui->totalprice->setText(tr("%1").arg(totalprice));
record = record + QString("单价:%1 元 数量: %2 个 合计:%3 元\n").arg(price).arg(num).arg(totalprice);
ui->textBrowser->setText(record);
}
#include <QtGui/QApplication>
#include "widget.h"
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
Widget w;
w.show();
return a.exec();
}