Banking Problem

Discussion in 'C++' started by ashu, Oct 17, 2005.

  1. ashu

    ashu Guest

    hi to all,
    Please read the following ques. first.

    assume that a bank maintains 2 kinds of accounts for
    customers one called as
    savings accounts & other as current account.
    the savings account provides simple interest &
    withdraw facilities but no check book facilities.
    the current account provides check book facilities but
    no interest.
    current account holders should also maintain a minimum
    balance and if the minimum balance falls below this
    level a service charge is to be imposed.

    create a class account that stores customers name,
    account no & type of account
    from this class derive the classes current acc &
    savings acc. to make them more
    specific to their environments. include necessary
    member functions in order to achieve following tasks:
    1. accept the deposit from the customer and compute
    the balance
    2. display the balance
    3. compute and deposit the interest
    4. permit the withdrawl and update the balance
    5. check the minimun balance, impose penalty if
    necessary &update the balance

    -->>DO NOT USE CONSTRUCTORS AND USE MEMBER FUNCTIONS
    TO INITIALIZE THE CLASS MEMBERS

    (NOTE: #MAKE AN EXTRA FIELD DATE (INCLUDE DOS.H TO
    ACCESS DATE STRUCTURE)
    #MAKE THIS PROGRAM BY USING LINK LIST )


    The problem i am facing is that when i have created two derived class
    as directed, i have to create two linklist to each derived class. So at
    the time of modifing, depositing or any other operation, i have to
    search both the linklist, which will be very time consuming & if no
    such account exist, it would be the worst of the worst case. Then i
    think another solution, i make another derived class by taking (saving
    & current class) early derived class as base class & create a single
    linklist. Now, the problem in that is, when my account is saving type,
    my current class in totally untouched(wasted) & vice-versa.
    I want to make a single linklist by merging two linklist. Would anyone
    suggest me the solution. The program should be Menu Driven.

    Thank You
    ashu, Oct 17, 2005
    #1
    1. Advertising

  2. ashu

    mlimber Guest

    ashu wrote:
    > hi to all,
    > Please read the following ques. first.
    >
    > assume that a bank maintains 2 kinds of accounts for
    > customers one called as
    > savings accounts & other as current account.
    > the savings account provides simple interest &
    > withdraw facilities but no check book facilities.
    > the current account provides check book facilities but
    > no interest.
    > current account holders should also maintain a minimum
    > balance and if the minimum balance falls below this
    > level a service charge is to be imposed.
    >
    > create a class account that stores customers name,
    > account no & type of account
    > from this class derive the classes current acc &
    > savings acc. to make them more
    > specific to their environments. include necessary
    > member functions in order to achieve following tasks:
    > 1. accept the deposit from the customer and compute
    > the balance
    > 2. display the balance
    > 3. compute and deposit the interest
    > 4. permit the withdrawl and update the balance
    > 5. check the minimun balance, impose penalty if
    > necessary &update the balance
    >
    > -->>DO NOT USE CONSTRUCTORS AND USE MEMBER FUNCTIONS
    > TO INITIALIZE THE CLASS MEMBERS
    >
    > (NOTE: #MAKE AN EXTRA FIELD DATE (INCLUDE DOS.H TO
    > ACCESS DATE STRUCTURE)
    > #MAKE THIS PROGRAM BY USING LINK LIST )
    >
    >
    > The problem i am facing is that when i have created two derived class
    > as directed, i have to create two linklist to each derived class. So at
    > the time of modifing, depositing or any other operation, i have to
    > search both the linklist, which will be very time consuming & if no
    > such account exist, it would be the worst of the worst case. Then i
    > think another solution, i make another derived class by taking (saving
    > & current class) early derived class as base class & create a single
    > linklist. Now, the problem in that is, when my account is saving type,
    > my current class in totally untouched(wasted) & vice-versa.
    > I want to make a single linklist by merging two linklist. Would anyone
    > suggest me the solution. The program should be Menu Driven.
    >
    > Thank You


    See this FAQ:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2

    This newsgroup is for C++ language questions. If you have a specific
    language-related question, we will be happy to help you. Otherwise, you
    might want to try in comp.programming or
    comp.please.do.my.homework.for.me.

    Cheer! --M
    mlimber, Oct 17, 2005
    #2
    1. Advertising

  3. ashu

    Kai-Uwe Bux Guest

    mlimber wrote:

    > ashu wrote:

    [snip]
    >> The problem i am facing is that when i have created two derived class
    >> as directed, i have to create two linklist to each derived class. So at
    >> the time of modifing, depositing or any other operation, i have to
    >> search both the linklist, which will be very time consuming & if no
    >> such account exist, it would be the worst of the worst case. Then i
    >> think another solution, i make another derived class by taking (saving
    >> & current class) early derived class as base class & create a single
    >> linklist. Now, the problem in that is, when my account is saving type,
    >> my current class in totally untouched(wasted) & vice-versa.
    >> I want to make a single linklist by merging two linklist. Would anyone
    >> suggest me the solution. The program should be Menu Driven.

    [snip]
    > This newsgroup is for C++ language questions. If you have a specific
    > language-related question, we will be happy to help you. Otherwise, you
    > might want to try in comp.programming or
    > comp.please.do.my.homework.for.me.


    Don't be so eager to dismiss this as off topic. Actually, there is a
    language question in there: given two classes, deriving from a common base,
    does C++ have a mechanism to have objects of both types in the same
    container. If so, what is the recommended was to go about it?

    And a possible answer is: you can use a container of pointers to the base
    class. Better even, use a container of reference counting smart_pointers to
    the base class so that memory management is still automatic.

    To the OP: this should get you started. Post again when you have a draft of
    your code and you are running into more serious problems. Also, please do
    not roll your own code for the linked list. Use std::list<> instead. It
    will save you serious headache.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 17, 2005
    #3
  4. ashu

    ashu Guest

    Thank you for considering my problem & guiding me so well. As i'm new
    to the Oops, i not know much about Oops. So would you please tell me
    what are containers of pointers, container of reference counting
    smart_pointers???.

    And "please do not roll your own code for the linked list. Use
    std::list<> instead" ????? what does this line means. please tell me.
    ashu, Oct 17, 2005
    #4
  5. ashu

    Kristo Guest

    ashu wrote:
    > Thank you for considering my problem & guiding me so well. As i'm new
    > to the Oops, i not know much about Oops. So would you please tell me
    > what are containers of pointers, container of reference counting
    > smart_pointers???.


    Rather than have one of us explain it, I suggest picking up a good C++
    book (or two) and reading it. IIRC, the FAQ has several good
    recommendations.

    > And "please do not roll your own code for the linked list. Use
    > std::list<> instead" ????? what does this line means. please tell me.


    It means that you should use the list class provided by the Standard
    Library instead of writing your own.

    Kristo
    Kristo, Oct 17, 2005
    #5
  6. ashu

    osmium Guest

    "ashu" writes:

    > assume that a bank maintains 2 kinds of accounts for
    > customers one called as
    > savings accounts & other as current account.
    > the savings account provides simple interest &
    > withdraw facilities but no check book facilities.
    > the current account provides check book facilities but
    > no interest.
    > current account holders should also maintain a minimum
    > balance and if the minimum balance falls below this
    > level a service charge is to be imposed.
    >
    > create a class account that stores customers name,
    > account no & type of account
    > from this class derive the classes current acc &
    > savings acc. to make them more
    > specific to their environments. include necessary
    > member functions in order to achieve following tasks:
    > 1. accept the deposit from the customer and compute
    > the balance
    > 2. display the balance
    > 3. compute and deposit the interest
    > 4. permit the withdrawl and update the balance
    > 5. check the minimun balance, impose penalty if
    > necessary &update the balance
    >
    > -->>DO NOT USE CONSTRUCTORS AND USE MEMBER FUNCTIONS
    > TO INITIALIZE THE CLASS MEMBERS
    >
    > (NOTE: #MAKE AN EXTRA FIELD DATE (INCLUDE DOS.H TO
    > ACCESS DATE STRUCTURE)
    > #MAKE THIS PROGRAM BY USING LINK LIST )
    >
    >
    > The problem i am facing is that when i have created two derived class
    > as directed, i have to create two linklist to each derived class. So at
    > the time of modifing, depositing or any other operation, i have to
    > search both the linklist, which will be very time consuming & if no
    > such account exist, it would be the worst of the worst case. Then i
    > think another solution, i make another derived class by taking (saving
    > & current class) early derived class as base class & create a single
    > linklist. Now, the problem in that is, when my account is saving type,
    > my current class in totally untouched(wasted) & vice-versa.
    > I want to make a single linklist by merging two linklist. Would anyone
    > suggest me the solution. The program should be Menu Driven.


    First of all, a linked list is a very bad data structure for this problem,
    it will be terribly slow and no reasonable amount of cleverness is going to
    change that. That *might* be the point your instructor is trying to make.
    He specifies you are not to use a meaningful constructor, and this is a
    prelude to an obvious array solution, using hashing. Then, for a subsequent
    assignment he has you already motivated for a better data structure.

    I don't really understand everything you said, I suspect there is an English
    problem. You seem to focus on the worst case, a customer who claims to have
    an account in fact doesn't have one. Are you proposing three lists? And
    some way to get the "customer" to answer some question or other truthfully?
    osmium, Oct 18, 2005
    #6
  7. "osmium" <> schrieb im Newsbeitrag
    news:...
    >
    > That *might* be the point your instructor is trying to make.


    I think the point his instructor is trying to make is an exercise
    on how to use a polymorphic class in a container (plus an exercise
    in constructing a linked list, if he has to code the list by hand).

    Never forget: This are class room assignments, not real world
    applications.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 18, 2005
    #7
  8. ashu

    Jim Langston Guest

    "ashu" <> wrote in message
    news:...

    > The problem i am facing is that when i have created two derived class
    > as directed, i have to create two linklist to each derived class. So at
    > the time of modifing, depositing or any other operation, i have to
    > search both the linklist, which will be very time consuming & if no
    > such account exist, it would be the worst of the worst case. Then i
    > think another solution, i make another derived class by taking (saving
    > & current class) early derived class as base class & create a single
    > linklist. Now, the problem in that is, when my account is saving type,
    > my current class in totally untouched(wasted) & vice-versa.
    > I want to make a single linklist by merging two linklist. Would anyone
    > suggest me the solution. The program should be Menu Driven.


    Maybe this will get you started:

    class Base
    {
    public:
    virtual ~Base() {};
    };

    class Derived1 : Base
    {
    public:
    ~Derived1(){};
    };

    class Derived2 : Base
    {
    public:
    ~Derived2 () {};
    };

    int main()
    {
    Base* baseP1;
    Base* baseP2;
    baseP2 = new Derived1;
    baseP2 = new Derived2;
    };

    What I'm showing here is that a base pointer can point to a derived object.
    So a linked list of base objects can point to any derived objects of base.

    Read your textbook on this.
    Jim Langston, Oct 19, 2005
    #8
  9. ashu

    ashu Guest

    here, the object of base class can't access the member data & functions
    of derived class, as inheritance not work in opposite direction, so
    can't help.
    ashu, Oct 19, 2005
    #9
  10. ashu wrote:
    >
    > here, the object of base class can't access the member data & functions
    > of derived class, as inheritance not work in opposite direction, so
    > can't help.


    Right. But the base class should not access all of this.

    All your base class needs to do is:
    make available a couple of virtual functions, which are
    implemented in the derived classes.

    Eg. The base class introduces functions for handling
    of check books. In the 'current account' class, these
    functions are fully implemented, while in the 'savings account'
    class they are do-nothing-functions (or not implemented at all).

    Virtual functions, paired with inheritance is the key to your
    assignment!

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 19, 2005
    #10
  11. ashu

    Jim Langston Guest

    "ashu" <> wrote in message
    news:...
    > here, the object of base class can't access the member data & functions
    > of derived class, as inheritance not work in opposite direction, so
    > can't help.
    >


    Actually, it can. Here is a working program showing how using Run Time Type
    Checking (RTTI). Polymorphism would be worthless otherwise.

    #include <vector>
    #include <iostream>
    class Base
    {
    public:
    virtual ~Base() {};
    virtual void CommonFunc() = 0;
    };

    class Derived1 : Base
    {
    public:
    ~Derived1(){};
    void D1Func() { std::cout << "Derived1" << std::endl; };
    void CommonFunc() { std::cout << "Derived1 Common" << std::endl; };
    };

    class Derived2 : Base
    {
    public:
    ~Derived2 () {};
    void D2Func() { std::cout << "Derived2" << std::endl; };
    void CommonFunc() { std::cout << "Derived2 Common" << std::endl; };
    };

    int main()
    {
    std::vector<Base*> MyVector;
    MyVector.push_back( (Base*) new Derived1 );
    MyVector.push_back( (Base*) new Derived2 );
    std::vector<Base*>::iterator it;
    for ( it = MyVector.begin(); it != MyVector.end(); ++it )
    {
    if ( dynamic_cast<Derived1*> (*it) != NULL )
    dynamic_cast<Derived1*>(*it)->D1Func();
    if ( dynamic_cast<Derived2*> (*it) != NULL )
    dynamic_cast<Derived2*>(*it)->D2Func();
    (*it)->CommonFunc();
    }
    char wait;
    std::cin >> wait;
    };

    Output is:
    Derived1
    Derived1 Common
    Derived2
    Derived2 Common
    Jim Langston, Oct 20, 2005
    #11
  12. ashu

    Jim Langston Guest

    "Jim Langston" <> wrote in message
    news:eek:vO5f.557$...
    > "ashu" <> wrote in message
    > news:...
    >> here, the object of base class can't access the member data & functions
    >> of derived class, as inheritance not work in opposite direction, so
    >> can't help.
    >>

    >
    > Actually, it can. Here is a working program showing how using Run Time
    > Type Checking (RTTI). Polymorphism would be worthless otherwise.


    In addition to what I showed you, you should note that the
    (*it).CommonFunc() didn't need to dynamic cast the object, because it would
    call the function for whichever object was instatized since it was contained
    in the base.

    Basically you only have to do a dynamic_cast if you want to call a method
    that only exists in a derived object, not the base. If the method exists in
    the base just call it and it will use the derived's overriden method (if
    there is one).

    So if you didn't to use dynamic_casts you could enter "stubs" for all
    methods that simply do nothing. Such as:

    class Account
    {
    public:
    virtual void CalcInterest() { }; // Default is do nothing
    };

    class Savings: Account
    {
    void CalcInterest() { Balace += Balance * 0.014; };
    };

    class Checking: Account
    {
    // Checking doesn't override CalcInterest()
    };

    So now when call:
    (*it)->CalcInterest();

    if the actual instance is Savings, nothing will happen as the base method
    will be used which does nothing. If the instance is Checking the Balance
    will be updated. Not dynamic_cast needed since the method CalcInterest()
    exists in the base class.
    Jim Langston, Oct 20, 2005
    #12
  13. ashu

    mlimber Guest

    Jim Langston wrote:
    > "ashu" <> wrote in message
    > news:...
    > > here, the object of base class can't access the member data & functions
    > > of derived class, as inheritance not work in opposite direction, so
    > > can't help.
    > >

    >
    > Actually, it can. Here is a working program showing how using Run Time Type
    > Checking (RTTI). Polymorphism would be worthless otherwise.


    I wouldn't say worthless. In fact, in my current project I use
    polymorphism extensively but have RTTI disabled via a compiler switch
    because my classes follow the more standard method of design described
    by Karl above and don't need it.

    >
    > #include <vector>
    > #include <iostream>
    > class Base
    > {
    > public:
    > virtual ~Base() {};
    > virtual void CommonFunc() = 0;
    > };
    >
    > class Derived1 : Base
    > {
    > public:
    > ~Derived1(){};
    > void D1Func() { std::cout << "Derived1" << std::endl; };
    > void CommonFunc() { std::cout << "Derived1 Common" << std::endl; };
    > };
    >
    > class Derived2 : Base
    > {
    > public:
    > ~Derived2 () {};
    > void D2Func() { std::cout << "Derived2" << std::endl; };
    > void CommonFunc() { std::cout << "Derived2 Common" << std::endl; };
    > };
    >
    > int main()
    > {
    > std::vector<Base*> MyVector;
    > MyVector.push_back( (Base*) new Derived1 );
    > MyVector.push_back( (Base*) new Derived2 );
    > std::vector<Base*>::iterator it;
    > for ( it = MyVector.begin(); it != MyVector.end(); ++it )
    > {
    > if ( dynamic_cast<Derived1*> (*it) != NULL )
    > dynamic_cast<Derived1*>(*it)->D1Func();


    Better would be:

    if( Derived1* p = dynamic_cast<Derived1*>(*it) )
    {
    p->D1Func();
    }

    Then, at least there's only one call to the non-trivial dynamic_cast.

    > if ( dynamic_cast<Derived2*> (*it) != NULL )
    > dynamic_cast<Derived2*>(*it)->D2Func();
    > (*it)->CommonFunc();
    > }
    > char wait;
    > std::cin >> wait;
    > };
    >
    > Output is:
    > Derived1
    > Derived1 Common
    > Derived2
    > Derived2 Common


    Cheers! --M
    mlimber, Oct 20, 2005
    #13
  14. ashu

    Jim Langston Guest

    "mlimber" <> wrote in message
    news:...
    > Jim Langston wrote:
    >> "ashu" <> wrote in message
    >> news:...
    >> > here, the object of base class can't access the member data & functions
    >> > of derived class, as inheritance not work in opposite direction, so
    >> > can't help.
    >> >

    >>
    >> Actually, it can. Here is a working program showing how using Run Time
    >> Type
    >> Checking (RTTI). Polymorphism would be worthless otherwise.

    >
    > I wouldn't say worthless. In fact, in my current project I use
    > polymorphism extensively but have RTTI disabled via a compiler switch
    > because my classes follow the more standard method of design described
    > by Karl above and don't need it.


    Actually, my "Polymorphism would be worthless otherwise" was refering to the
    fact that the member data & functions can be assess through a base pointer,
    not to RTII. I had added the last sentance as an afterthough. Switch
    sentances 2 and 3 around and it what I meant to say :D (IOW, I agree with
    you)


    >> if ( dynamic_cast<Derived1*> (*it) != NULL )
    >> dynamic_cast<Derived1*>(*it)->D1Func();

    >
    > Better would be:
    >
    > if( Derived1* p = dynamic_cast<Derived1*>(*it) )
    > {
    > p->D1Func();
    > }
    >
    > Then, at least there's only one call to the non-trivial dynamic_cast.


    Agreed.
    Jim Langston, Oct 21, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    854
    Andrew Thompson
    Nov 21, 2005
  2. Replies:
    3
    Views:
    763
    IchBin
    Mar 11, 2006
  3. Replies:
    0
    Views:
    287
  4. Replies:
    0
    Views:
    276
  5. rgt

    Student Banking Problem

    rgt, Apr 11, 2008, in forum: Java
    Replies:
    10
    Views:
    665
    Roedy Green
    Apr 24, 2008
Loading...

Share This Page