Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторная работа 3 Объектная модель увц.rtf
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
872.35 Кб
Скачать

Пример тестовой программы

Использование рассмотренной выше упрошенной объектной модели магазина может быть продемонстрировано при помощи тестовой программы, которая:

  1. Активизирует сценарий использования магазина.

  2. Проверяет ожидания от состояния основных объектов модели после тестового воздействия.

  3. Распечатывает содержимое объектной модели на консоли.

test.cpp

//-----------------------------------------------------------------------------

#include "shop.hpp"

#include "order.hpp"

#include "item.hpp"

#include "product.hpp"

#include "money.hpp"

#include <cassert>

#include <cstdio>

//-----------------------------------------------------------------------------

void showShopStats ( const Shop & _shop )

{

printf( "Shop \"%s\" has %d products:\n",

_shop.getTitle().c_str(),

_shop.getProductsCount()

);

std::auto_ptr< Shop::ProductIterator > productIt = _shop.browseProducts();

while ( productIt->hasNext() )

{

Product const & product = productIt->getProduct();

printf( " - [%d] \"%s\", desription: \"%s\", price: %s\n",

product.getUniqueID(),

product.getName().c_str(),

product.getDescription().c_str(),

product.getCurrentPrice().toString().c_str()

);

productIt->next();

}

putchar( '\n' );

unsigned int nOrders = _shop.getOrdersCount();

printf( "Totally there were %d order(s) registered\n", nOrders );

putchar( '\n' );

for ( unsigned int i = 0; i < nOrders; i++ )

{

const Order & order = _shop.getOrder( i );

printf( "Order %d contains %d items with total cost of %s:\n",

order.getUniqueID(),

order.getItemsCount(),

order.getCost().toString().c_str()

);

unsigned int nItems = order.getItemsCount();

for ( unsigned int k = 0; k < nItems; k++ )

{

const Item & item = order.getItem( k );

printf( " Product = '%s', Quantity = %d, Price = %s, Cost = %s\n",

item.getProduct().getName().c_str(),

item.getQuantity(),

item.getPrice().toString().c_str(),

item.getCost().toString().c_str() );

}

putchar( '\n' );

}

putchar( '\n' );

}

//-----------------------------------------------------------------------------

void main ()

{

/////

const char * SHOP_TITLE = "MyShop";

Shop shop( SHOP_TITLE );

/////

assert( shop.getTitle() == SHOP_TITLE );

assert( shop.getOrdersCount() == 0 );

assert( shop.getProductsCount() == 0 );

/////

Product const & batteries = shop.addProduct(

"Batteries",

"A pack with 4 batteries",

Money::fromString( "12.00" )

);

Product const & dvdDisk = shop.addProduct(

"DVD disk",

"A blank DVD disk for recording",

Money::fromString( "3.00" )

);

/////

assert( shop.getProductsCount() == 2 );

assert( shop.findProduct( "Batteries" ) == & batteries );

assert( shop.findProduct( "DVD disk" ) == & dvdDisk );

assert( shop.findProduct( "other-product" ) == NULL );

/////

Order & order = shop.newOrder();

order.newItem( batteries, 1 );

order.newItem( dvdDisk, 3 );

/////

assert( order.getCost().toString() == "21.00" );

assert( order.getItemsCount() == 2 );

assert( order.getItem( 0 ).getQuantity() == 1 );

assert( order.getItem( 0 ).getCost() == batteries.getCurrentPrice() );

assert( order.getItem( 1 ).getQuantity() == 3 );

assert( order.getItem( 1 ).getCost() == ( dvdDisk.getCurrentPrice() * 3 ) );

/////

showShopStats( shop );

}

//-----------------------------------------------------------------------------