C# Polymorphism/Lists -


i've been having trouble this. i'll summarise briefly.

i have 4 classes. 1 "person" class. other 3 "rental", 2 classes inheriting that, "rentalbyday" , "rentalbykm".

within "person" class, there list of rental objects. issue i'm having i'm not sure how add list rental objects created.

class person {     private string _firstname;     private string _lastname;      public person(string lastname, string firstname)     {         _lastname = lastname;         _firstname = firstname;     }     public string lastname     {         { return _lastname; }     }     public string firstname     {         { return _firstname; }     }     public list<rental> _rentedlist = new list<rental>();     public readonlycollection<rental> rentedlist     {         { return _rentedlist.asreadonly();}     }       public void addrental(rental rental)     {         _rentedlist.add(rental);     }     public void printrentals()     {         foreach (rental d in _rentedlist)         {             console.writeline(d.person);         }     } }  class rental {      private double _rentduration;      person _person;      public double rentduration     {         { return _rentduration; }         set { _rentduration = value; }     }      public person person     {         { return _person; }         set { _person = value; }     }       public rental(person person, double rentduration)     {          _person = person;         _rentduration = rentduration;      }   }  class rentalbyday : rental {      public rentalbyday(person person, double rentbydays)         : base(person, rentbydays)     {          // add rental list here?      }  }  class rentalbykm : rental {     public rentalbykm(person person, double rentbykm)         : base(person, rentbykm)     {          // add rental list here?         }  }  class rentalagency {     static void main(string[] args)     {          person jane = new person("bloggs", "jane");         person joe = new person("bloggs", "joe");         person peter = new person("piper", "peter");         person penny = new person("piper", "penny");          new rentalbyday(jane, 5);          new rentalbyday(jane, 2);         jane.printrentals();          new rentalbyday(joe, 8);         new rentalbykm(joe, 15);         joe.printrentals();          new rentalbyday(peter, 1);         new rentalbykm(peter, 85);         peter.printrentals();          new rentalbyday(penny, 5);         new rentalbykm(penny, 42);         penny.printrentals();          console.writeline("quote {0}", new rentalbyday(null, 10));         console.writeline("quote {0}", new rentalbykm(null, 10));        } } 

the end result should when printrental called, of rentals person displayed.

any appreciated. feel obvious, whatever reason cannot figure out.

thanks!

you need create rental, , add rental user, instead of trying create rental, , have rental add user.

jane.addrental(new rentalbyday(jane, 5)); jane.addrental(new rentalbyday(jane, 2)); jane.printrentals();  joe.addrental(new rentalbyday(joe, 8)); joe.addrental(new rentalbykm(joe, 15)); joe.printrentals();  peter.addrental(new rentalbyday(peter, 1)); peter.addrental(new rentalbykm(peter, 85)); peter.printrentals();  penny.addrental(new rentalbyday(penny, 5)); penny.addrental( new rentalbykm(penny, 42)); penny.printrentals(); 

you could add rental person, however, creating objects new , not assigning them bit unusual , confusing.

public rental(person person, double rentduration) {     _person = person;     _rentduration = rentduration;     _person.addrental(this); } 

you may want add tostringoverride person class, when printed console, more "person".

public override string tostring() {     return string.format("{0}, {1} - {2} rentals.", lastname, firstname, _rentedlist.count); } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -