Qt中的UML图解释

Qt中的UML图解释,qt,uml,Qt,Uml,问题 我试图使用Qt解释这个UML图: 我关心的是如何定义QList类以及如何实现聚合和组合关系。我知道继承是由…实现的。。。。派生类:公共基指令 我已经在下面尝试了。 任何人都可以帮忙。谢谢 #ifndef PRODUCT_H #define PRODUCT_H #include <Transaction.h> #include <QString> #include <QList> #include <QDate> #include <

问题

我试图使用Qt解释这个UML图: 我关心的是如何定义QList类以及如何实现聚合和组合关系。我知道继承是由…实现的。。。。派生类:公共基指令

我已经在下面尝试了。 任何人都可以帮忙。谢谢

#ifndef PRODUCT_H
#define PRODUCT_H

#include <Transaction.h>
#include <QString>
#include <QList>
#include <QDate>
#include <QDebug>

//define product class
class Product {
public:
  Product(QString name, int num, double seprice, double suprice, QString sc );
  virtual void sell(int n);
  void restock(int n);
  QString getSupplierCode();
  void setProductCode(QString c );
  QString getProductCode();
  QList<Transaction*>getTransactions();
  QString toString();
  void removeAll();
  bool isExpired();

private:
  QString m_Name;
  int m_NumOfItems;
  QString m_ProductCode;
  double m_SellingPrice;
  double m_SupplierPrice;
  QString m_SupplierCode;
  QList<Transaction*>m_Transactions;

};
//end of product class
#endif //PRODUCT_H

#ifndef FOODPRODUCT_H
#define FOODPRODUCT_H

//begining of FoodProduct class
class FoodProduct : public Product {
public:
  FoodProduct(QString name, int num, double seprice, double suprice, QString sc, QDate sbd);
  void sell(int n);
  QString toString();
  bool isExpired();

private:
  QDate m_SellByDate;

};
//end of FoodProduct Class

#endif // FOODPRODUCT_H

#ifndef PRODUCTLIST_H
#define PRODUCTLIST_H

//begining of ProductList Class
class ProductList : public QList<Product*> {
public:
  QString add(QString name, int num, double seprice, double suprice, QString sc, QDate sbd );
  void sell(QString pc, int n );
  void restock(QString pc, int n );
  QString toString();
  void removeStock();
  QString getTransactions(QString pc) const;

private:
  int m_NextCode;
};
//end of ProductList Class
#endif //PRODUCTLIST_H

#include <Product.h>
#ifndef TRANSACTION_H
#define TRANSACTION_H

//begining of Transaction Class
class Transaction
{
public:
  Transaction(QString type, QDate date, int num, double price );
  QString toString();

private:
  QString m_Type;
  QDate m_Date;
  int m_NoOfItems;
  double m_PricePerItem;

};
//end of Transaction class
#endif // TRANSACTION_H

implementations

#include <Product.h>

Product::Product(QString name, int num, double seprice, double suprice, QString sc)
{
    m_Name = name;
    m_NumOfItems = num;
    m_SellingPrice = seprice;
    m_SupplierPrice = suprice;
    m_SupplierCode = sc;
}

void Product::sell(int n)
{
  if(m_NumOfItems == 0)
  {
    qDebug() << "Out of stock";
  }
  else
    {
        m_NumOfItems -= n;
        m_Transaction.append(Transaction("Sale", QDate::currentDate(),n, m_SellingPrice));
    }
}

void Product::restock(int n)
{
    m_NumOfItems += n;
    m_Transaction.append(Transaction("Purchase", QDate::currentDate(),n, m_SupplierPrice));
}

QString Product::getSupplierCode()
{
    return m_SupplierCode;
}

void Product::setProductCode(QString c)
{
    m_ProductCode = c;
}

QString Product::getProductCode()
{
    return m_ProductCode;
}


QString Product::toString()
{
    return QString("Product Name: %1\nProduct Code: %2\nSupplier Price: %3\nSelling Price: %4\nSupplier Code: %5")
                   .arg(m_Name).arg(m_ProductCode).arg(m_SupplierPrice).arg(m_SellingPrice).arg(m_SupplierCode);
}

void Product::removeAll()
{
    m_NumOfItems = 0;
}

-------------------------

#include <Transaction.h>


Transaction::Transaction(QString type, QDate date, int num, double price)
{
    m_Type = type;
    m_Date = date;
    m_NoOfItems = num;
    m_PricePerItem = price;
}

QString Transaction::toString()
{
   QString date1;
   date1 = m_Date.toString();
   return QString("Transaction Type: %1\nDate: %2\nNumber of Items: %3\nPrice: R%4\n")
        .arg(m_Type).arg(date1).arg(m_NoOfItems).arg(m_PricePerItem*m_NoOfItems);
}

--------------------------

FoodProduct::FoodProduct(QString name,int num, double seprice, double suprice, QString sc, Qdate sbd)
{
    FoodProduct = Product();
    m_SellByDate = sbd;
}

void FoodProduct::sell(int n)
{
    if (isExpired())
        qDebug << "Product has expired!";
    else Product::sell(n);
}

bool FoodProduct::isExpired()
{
    return(m_SellByDate < QDate::currentDate());
}

QString FoodProduct::toString()
{
    Product::toString();
}


--------------------

#include ProductList.h

QString ProductList::add(QString name, int num, double seprice, double suprice, QString sc, QDate sbd)
:int m_NextCode(1001);
{
     ProductList = Product();
     m_SellByDate = sbd;
     m_NextCode++;

}

ProductList::~ProductList()
{

}

void ProductList:sell(Qstring pc, int n)
{
     foreach(Product* pc, ProductList)
    {
        ProductList::m_NumberOfItems -= n;

    }    

}

void ProductList:restock(Qstring pc, int n)
{
     foreach(Product* pc, ProductList)
    {
        ProductList::m_NumberOfItems += n;

    }    

}

void ProductList::removeStock()
{
     foreach(Product* pc, ProductList)
    {
        if (ProductList::isExpired())

        ProductList::m_NumberOfItems = 0;

    } 

}

QString ProductList::toString()
{
     return QString("Product Name: %1\nProduct Code: %2\nNumber of Items: %3")
        .arg(ProductList::m_Name).arg(ProductList::m_ProductCode).arg(ProductList::m_NumberofItems);

}

QString ProductList::getTransactions(QString pc)
{

}
\ifndef产品
#定义产品
#包括
#包括
#包括
#包括
#包括
//定义产品类别
类产品{
公众:
产品(QString名称、整数、双精度、双精度、QString sc);
虚拟无效销售(int n);
空置补货(整数n);
QString getSupplierCode();
void setProductCode(QString c);
QString getProductCode();
QListgetTransactions();
QString-toString();
void removeAll();
bool isExpired();
私人:
QString m_名称;
国际货币基金组织;
QString m_产品代码;
双m_销售价格;
双m_供应商价格;
QString m_供应商代码;
QListm_交易;
};
//产品类别结束
#endif//PRODUCT\u H
#ifndef食品产品
#定义食品产品
//FoodProduct类的开始
食品类产品:公共产品{
公众:
食品产品(QString名称、整数、双seprice、双suprice、QString sc、QDate sbd);
无效出售(int n);
QString-toString();
bool isExpired();
私人:
QDate m_SellByDate;
};
//FoodProduct类结束
#endif//FOODPRODUCT\u H
#ifndef产品列表
#定义产品列表
//ProductList类的开始
类ProductList:公共QList{
公众:
QString add(QString name,int num,double seprice,double supprice,QString sc,QDate sbd);
无效销售(QString pc,int n);
无效补货(QString pc,int n);
QString-toString();
void removeStock();
QString getTransactions(QString pc)常量;
私人:
int m_NextCode;
};
//ProductList类结束
#endif//PRODUCTLIST\u H
#包括
#ifndef交易记录
#定义事务
//事务类的开始
类事务
{
公众:
交易(QString类型、QDate日期、整数、双倍价格);
QString-toString();
私人:
QString m_型;
QDate m_日期;
int m_NoOfItems;
双m_;
};
//事务类结束
#endif//TRANSACTION\u H
实现
#包括
产品::产品(QString名称、整数、双精度、双精度、QString sc)
{
m_Name=名称;
m_NumOfItems=num;
m_SellingPrice=seprice;
m_供应商价格=供应价格;
m_供应商代码=sc;
}
无效产品::销售(整数n)
{
如果(m_NumOfItems==0)
{

qDebug()QList
不能作为自身实现。它是一个泛型类,
ProductList
确实实现了它。它显示子类
ProductList
返回一个
Product
类的列表

关于聚合/合成,您在如何实现它方面相当自由。最简单的方法是使用数组。您也可以使用链表或任何合适的方法。当您在聚合类中解除对象锁定时,聚合将使对象继续存在。相反,合成将在解除锁定时杀死对象。存在实际上不需要这样做。这是建模者对程序员的一个提示。因此,如果您自己有一些垃圾收集,您可能会以不同的方式处理它


当查看
ProductList
时,感觉它使用了一个组合是错误的。我想
Product
可以独立生存。因此聚合似乎更合适。YMMV。

请问UML在哪里?我不会读上面那么长的文本。谢谢我可以通过电子邮件发送它,我看不到如何在这里附加图像?它告诉我需要10个声誉才能发布图片。将其放置在公共服务器(例如imageshack.us)上并发布链接。一旦获得声誉(很快),您可以直接发布图片。通过dropboxOk共享。那么您担心什么?