Abstract hint or general direction RRS feed

  • Question

  • Good Morning/Afternoon,

    I'm working on a homework assignment and would appreciate just a hint or information about the specific situation using an abstract class.

    The overall project is to create an account class then a personal with specific criteria name, address, invoice date, due date. Then create a business account with a few similar properties as account as well as a few different ones. However, the instructions specify to create an abstract class to calculate the due date via personal +30 days from invoice date and business +60 days from invoice date.

    My thought process was to create a method for today's date via: string InvDate = DateTime.Now.ToString("MM/dd/yyyy");. Then create an abstract class that would calculate by adding 30 days or 60 days depending on personal or business. This can be done ( in theory ) by adding to the property in the main method when instantiating the class via:

    personal.DueDate = InvDate.add(30);

    as well as

    business.DueDate = InvDate.add(60);

    Then displaying the information to the console. I guess I just don't understand abstract and the purpose of using it in this situation. Perhaps I'm using it wrong because I'm reading an abstract class cannot be added to. Any hints would be great! Thanks for taking the time to read this and again this is homework and I would just appreciate some input~


    Sunday, June 24, 2018 2:30 PM

All replies

  • As the abstract BaseAccount class should do the calculation based on the current date, it should have a public read-only Date property. I would use two constructors. One parameterless setting the date property to DateTime.Now and a constructor with one DateTime parameter to set a custom value (e.g. for testing).

    As the due date depends on a due period specific to the concrete classes, it needs also a protected field storing the due period. The DueDate property should be only read-only and use the getter to calculate the correct value depending on the values of Date and DuePeriod. 

    The concrete types of BusinessAccount and PersonalAccount set the correct DuePeriod value in their constructors.

    Sunday, June 24, 2018 3:53 PM
  • In addtion to what Stefan wrote, let me just add that abstract classes cannot be instantiated. They have to be used as a base class that other classes inherit from.

    So your two classes (Business and Personal) would sub-class from (inherit from) the abstract Base class. What that means is that the method in the abstract Base class has all the code and does all the work ... the sub-classed Business and Personal classes just call the method.

    ~~Bonnie DeWitt [C# MVP]

    Sunday, June 24, 2018 5:06 PM