skip to Main Content

class Hotel {
  static final Map<String, List<String>> mapInformation = {
    'Americana Hotel': [
      '4.3*', 
      '$4000 / Night', 
    ],
  };

  String getPrice(String favouriteElementsName) {
    return mapInformation[favouriteElementsName]?.elementAt(1) ??
        'It may need fixes';
  }

  String getRating(String favouriteElementsName) {
    return mapInformation[favouriteElementsName]?.elementAt(0) ??
        'It may need fixes';
  }
}

class Home {
  static final Map<String, List<String>> mapInformation = {
    'Beachside Resort': [
      '2.2*', 
      '$2200 / Night', 
    ],
  };

  String getPrice(String favouriteElementsName) {
    return mapInformation[favouriteElementsName]?.elementAt(1) ??
        'It may need fixes';
  }

  String getRating(String favouriteElementsName) {
    return mapInformation[favouriteElementsName]?.elementAt(0) ??
        'It may need fixes';
  }
}

class Favourite {
  final LinkedHashMap<String, dynamic> favouriteElementsInLinkedHashMap =
      LinkedHashMap();

  void changePrice(String favouriteElementKey, Object obj) {
    obj.mapInformation[favouriteElementKey]?[1] = '$5000 / Night';
    var str1 = obj.getPrice(favouriteElementKey);
    var str2 = obj.getRating(favouriteElementKey);
  }

  void main() {
    var fav = Favourite();
    favouriteElementsInLinkedHashMap['Americana Hotel'] = Hotel;
    favouriteElementsInLinkedHashMap['Beachside Resort'] = Home;

    for (var mapKey in favouriteElementsInLinkedHashMap.keys) {
      fav.changePrice(
          mapKey, favouriteElementsInLinkedHashMap[mapKey]);
    }
  }
}

There is method changePrice, it gives me an error when i use mapInformation, is there any way, how can i use mapInformation, getPrice, getRating in method changePrice. There are a lot of classes as Hotel and Home. So I can’t use if,else in method changePrice.

2

Answers


  1. Make an abstract class that defines those methods:

    abstract class Accommodation {
      String getPrice(String favouriteElementsName);
      String getRating(String favouriteElementsName);
    }
    

    Now both Hotel and Home should extend that abstract class, and annotate the overriding methods with @override:

    class Hotel extends Accommodation {
      @override
      String getPrice(String favouriteElementsName) {
        // ...
      }
    
      // Do the same for other overriding methods
    }
    
    class Home extends Accommodation {
      // Do the same as Hotel
    }
    

    Change the type of the obj parameter:

    void changePrice(String favouriteElementKey, Accommodation obj) {
      // ...
    }
    

    Please note that you can’t access mapInformation from obj, because mapInformation is a static field of the class. Preferably you should define another method inside the abstract class and implement it in the extending classes that modifies mapInformation.


    There are also other Dart class modifiers that might suit for your case, see Class modifiers.

    Login or Signup to reply.
  2. You could use interface that in terms of dart is an abstract class e.g.

    abstract class Establishment {
      Map<String, List<String>> get mapInformation;
    
      String getPrice(String favouriteElementsName);
    
      String getRating(String favouriteElementsName);
    }
    

    Then you could use it like this:

    class Hotel implements Establishment {
      @override
      Map<String, List<String>> get mapInformation => {
            'Americana Hotel': [
              '4.3*',
              '$4000 / Night',
            ],
          };
    
      @override
      String getPrice(String favouriteElementsName) =>
          mapInformation[favouriteElementsName]?.elementAt(1) ??
          'It may need fixes';
    
      @override
      String getRating(String favouriteElementsName) =>
          mapInformation[favouriteElementsName]?.elementAt(0) ??
          'It may need fixes';
    }
    
    class Home implements Establishment {
      @override
      Map<String, List<String>> get mapInformation => {
            'Beachside Resort': [
              '2.2*',
              '$2200 / Night',
            ],
          };
    
      @override
      String getPrice(String favouriteElementsName) =>
          mapInformation[favouriteElementsName]?.elementAt(1) ??
          'It may need fixes';
    
      @override
      String getRating(String favouriteElementsName) =>
          mapInformation[favouriteElementsName]?.elementAt(0) ??
          'It may need fixes';
    }
    

    Finally, in the method changePrice you could use mapInformation:

    class Favourite {
      final LinkedHashMap<String, dynamic> favouriteElementsInLinkedHashMap =
          LinkedHashMap();
    
      void changePrice(String favouriteElementKey, Establishment place) {
        place.mapInformation[favouriteElementKey]?[1] = '$5000 / Night';
        final str1 = place.getPrice(favouriteElementKey);
        final str2 = place.getRating(favouriteElementKey);
      }
    
      void main() {
        final fav = Favourite();
        favouriteElementsInLinkedHashMap['Americana Hotel'] = Hotel;
        favouriteElementsInLinkedHashMap['Beachside Resort'] = Home;
    
        for (final mapKey in favouriteElementsInLinkedHashMap.keys) {
          fav.changePrice(mapKey, favouriteElementsInLinkedHashMap[mapKey]);
        }
      }
    }
    

    More info about Class modifiers here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search