Thursday, December 4, 2014

cs Oct 16, 2014



two_cards.cpp

#include "two_cards.h"
#include <stdexcept>
TwoCards::TwoCards(CreditCard * cd1, CreditCard * cd2)
:c1(cd1),c2(cd2),totalbal(c1->getBalance()+c2->getBalance())
{
   if (not check_invariant())
 throw std::runtime_error("invariant violation");
}

 void TwoCards::withdraw1(unsigned int amt){
  c1->withdraw(amt);
 totalbal = c1->getBalance() + c2->getBalance();
}
 void TwoCards::transfer(unsigned int)
 void TwoCards::withdrawEither(unsigned int)
 void TwoCards::replaceCard1(CreditCard *)
{
  // check the three preconditions
}
 bool TwoCards::check_invariant()
{
   return c1 != c2  and totalbal == c1->getBalance() + c2->getBalance();
}

two_cards.h


lab4.cpp
#include <iostream>
#include <stdexcept>
using namespace std;
#include "test.h"
#include "credit_card.h"

{
  public :
  void run()
   {
/*{\subsection{testcases for TwoCards constructor - pass }}*/
      CreditCard *c1 = new CreditCard(1000);
       CreditCard *c2 = new CreditCard(2000);
      try {TwoCards tc(c1,c2); succeed_();
    test_(tc.totalBal == 0); // initially zero balance
} catch(std::runtime_error& e)
   { std::cerr << e.what() << std:: endl;
      fail_("no exception expected");
    }
  /*{\subsection{testcases for TwoCards constructor - fail }}*/

      try {TwoCards tc(c1,c1); fail_("should not allow 2 of same card");
} catch(std::runtime_error& e)
   { std::cerr << e.what() << std:: endl;
      succeed_();
    }
  TwoCards tc(c1,c2);
  tc.withdraw1(500);
test_(tc.totalbal == -500);
test_(c1->getBalance() == -500);
test(c2->getBalance() == 0);
  CreditCard *c3 = new CreditCard(1000);
 CreditCard *c4 = new CreditCard(5000);

   try {tc.replaceCard1(c1); fail_("should not allow  same card");
} catch(std::runtime_error& e)
   { std::cerr << "expected failure message"<<e.what() << std:: endl;
      succeed_();
    }

   try {tc.replaceCard1(c3); succeed_();
 test_(tc.totalbal == -500); // still owe the bank 500
} catch(std::runtime_error& e)
   { std::cerr << e.what() << std:: endl;
      fail_("no exception expected");
    }

 try {tc.replaceCard1(c4); fail_("should not allow  different limit");
} catch(std::runtime_error& e)
   { std::cerr << "expected failure message"<<e.what() << std:: endl;
      succeed_();
    }
 }
};

initialization ocurs in the order as declared in the .h file. not initializer list so sometimes there is a segmentation fault or the tests don't pass through because of that.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home