variables - Create n objects specified by user in C# -
i have elevator program in user enters amount of people created.
once amount of people created, need 1 one request lift.
the code person object class is:
class person { public int currentfloor; public int destinationfloor; public bool requestmade; // user not request until inside lift public bool iswaiting; // manual public person(int currentfloor, int destinationfloor) { this.currentfloor = currentfloor; this.destinationfloor = destinationfloor; } // automatic public person() { var r = new random(); currentfloor = r.next(0, 4); // assign random floor current (5 lifts) assignrandomdestination(); // assign random floor destination cannot same current, else randomise destination again } private void assignrandomdestination() { var r = new random(); destinationfloor = r.next(0, 4); if (destinationfloor == currentfloor) { assignrandomdestination(); } } public void state(person person, bool waiting) { if (waiting == true) { person.iswaiting = true; } else { person.iswaiting = false; } } public bool request() { return requestmade; } } and how able call them individually?
you should make class elevator , add system.collections.generic.
list<person> passenger = new list<person>(); //make instance of person. person my1stperson = new person(); //access member dot operator. my1stperson.currentfloor = 13; //after setting fields; can add them list by. passenger.add(my1stperson); // if have elevator class currentlocation field on elevator. elevator.currentlocation = passenger[0].currentfloor; //<- works if field static. or use index. linq works list. enumerables etc. //if not static, instantiate elevator class. elevator myelevator = new elevator();
Comments
Post a Comment