[Class 2017 Object-Oriented Programming] Assignment 2

PO elevator

  • A struct to store the request and a struct to store the passenger information.
  • The process is as follows:
    • Check for requests.
    • Move to request.
    • Customer service.
    • see a visitor out.
    • Check if all passengers have been delivered, if not, go back to the first step to check if there is a new request.

OO elevator

  • A class to store time, a class to store passengers, a class to store elevators, a struct to store requests, and a main function.
  • The process is as follows:
    • Preprocessing: Read the passenger request, define an elevator and a bunch of passengers, and initialize the time class.
    • run:
      • Call the passenger class method to check if there is a request, and put it into the request queue. ,
      • Call the passenger method to check if there is access to the elevator.
      • Call the elevator method to select the appropriate target.
      • Move in the appropriate target direction.
      • Check if all passengers have been delivered, if not, go back to the first step to check if there is a new request.
    • Tail processing: None at the moment.

Compared

  • Personally, I think OO elevators have no great advantages compared with PO elevators under the current conditions.
  • Personally, I think OOP is like besieging a target and wrapping it all up in the last net, while PO is more like hitting a point until it hits the target.
  • Personally, I think OO's code scalability, security, and readability are better.

Elevator

class Elevator
{
public:
  Elevator() {
    liftfloor = 1;
    direction = 0;
    isopen = 0;
  }
  ~Elevator();
//输出当前楼层
  int getLiftfloor() {
    return liftfloor;
  }
//开门
  void open() {
    isopen = 1;
  }
//关门
  void close() {
    isopen = 0;
  }
//运行
  void run() {
    liftfloor += direction;
  }
//运行到目标
  void runTo(int goalfloor) {
    if (goalfloor != liftfloor) {
      direction = (goalfloor - liftfloor) / abs(goalfloor - liftfloor);
    }
    for (int i = liftfloor; i < goalfloor; i++) {
      run();
    }
  }
private:
  int liftfloor; //储存当前楼层
  int direction;//储存运行方向
  bool isopen;//储存门是否打开
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325374973&siteId=291194637