私は価格を計算するために使用される4つのクラスProductとMultiBuyProduct(productの子)と、MultiBuyProductからの呼び出し関数が価格を取得してコンソールに領収書を出力する追加できるショッピングカートとAmountを持っていますは、2 つの int を受け取り、加算減算などの計算を行い、フォーマットされた価格を返すクラスです。私のmain.cppから私は呼び出します
MultiBuyProduct p2("Wine", Amount(10,0), 2, 10);
ShoppingCart SC;
SC.add(&p2 ,2, true);
以下にショッピングカートの追加方法を示します
void ShoppingCart::add(Product *p, int quantity, bool end)
{
mProduct = p;
//Sets member vairable value
mQuantity = quantity;
//Gets Name of product 'p'
mProductName = mProduct->getName();
//Gets price of product 'p'
mAmount = mProduct->getPrice();
//Gets total price of product 'p' based on quantity
mAmountTotal = mProduct->getPrice(quantity);
//Updates the GrandTotal
mGrandTotal.add(mProduct->getPrice(0));
}
以下は、MultiBuyProduct getPrice を示しています
Amount MultiBuyProduct::getPrice(int quantity)
{
if(quantity >= mMinDiscountedQuantity)
{
mPrice.setFullPence(mPrice.getFullPence() * quantity);
mPrice.setPounds(mPrice.getFullPence()/100);
mPrice.setPence(mPrice.getFullPence()%100);
int j = mPrice.getFullPence() / 100 * mDiscountedPercent;
saving += j;
int i = mPrice.getFullPence() - j;
mPrice.setFullPence(i);
mPrice.setPounds(i/100);
mPrice.setPence(i%100);
return Amount(mPrice.getFullPence()/100, mPrice.getFullPence()%100);
}
else
{
return Product::getPrice(quantity);
}
}
基本的に機能は機能しており、数量が 2 以上であるため 10% が割引されたことを示す正しい合計がコンソールに出力されます。
しかし、私のelseステートメントに到達しました(ショッピングカートで、アイテムの特異な価格を探すときにメソッドを追加します)
mAmount = mProduct->getPrice();
しかし、何も返されません。何らかの理由で製品に MultiBuyProduct が持つデータが含まれていないためだと思います。基本的に、製品に multiBuy 製品と同じデータを持たせてから価格を取得する必要があります。(基本的にJavaのように私は(else super.getPrice(quantity) <<<を呼び出しますが、C ++ではそれができないことを知っています)
編集:ここにクラス構造があります
製品:
Product::Product(std::string name, Amount price):aName(name), mPrice(price)
{
}
マルチバイ製品:
MultiBuyProduct::MultiBuyProduct(std::string aName, Amount price, int minDiscountedQuantity, int discountedPercent)
: mMinDiscountedQuantity(minDiscountedQuantity), mDiscountedPercent(discountedPercent),
mPrice(price), aName(aName)
{
mProduct = Product(mName,mPrice);
}