返回

Alibaba Interview Question: Implement an EatMan

前端

Introduction
During technical interviews, candidates often encounter challenging questions that test their problem-solving abilities and understanding of fundamental programming concepts. One such question, posed by Alibaba, requires the implementation of an EatMan class. This seemingly simple task demands a deep grasp of object-oriented programming principles and the ability to apply them creatively. In this article, we will embark on a journey to understand the requirements of the EatMan class and develop a comprehensive solution that showcases our programming proficiency.

Problem Statement
The EatMan class is a unique entity that represents a being capable of consuming food. The class should possess the following characteristics:

  • It should be able to eat different types of food, represented by various food classes.
  • The EatMan should have a stomach capacity, limiting the amount of food it can consume at a time.
  • Consuming food should increase the EatMan's energy levels.
  • Different types of food provide varying amounts of energy.
  • The EatMan should be able to report its current energy level.

Solution
To effectively tackle this problem, we will employ object-oriented programming principles such as inheritance, polymorphism, and encapsulation.

1. Inheritance:
We begin by defining a base Food class that serves as the blueprint for all types of food items. The Food class will have properties such as name, calories, and a method to consume the food.

public abstract class Food {
    private String name;
    private int calories;

    public Food(String name, int calories) {
        this.name = name;
        this.calories = calories;
    }

    public String getName() {
        return name;
    }

    public int getCalories() {
        return calories;
    }

    public abstract void consume();
}

2. Polymorphism:
Next, we define specific food classes that inherit from the Food class. Each food class represents a different type of food with unique properties. For example, we can have classes like Apple, Pizza, and Steak.

public class Apple extends Food {
    public Apple() {
        super("Apple", 100);
    }

    @Override
    public void consume() {
        System.out.println("Eating an apple...");
    }
}

public class Pizza extends Food {
    public Pizza() {
        super("Pizza", 500);
    }

    @Override
    public void consume() {
        System.out.println("Eating a pizza...");
    }
}

public class Steak extends Food {
    public Steak() {
        super("Steak", 300);
    }

    @Override
    public void consume() {
        System.out.println("Eating a steak...");
    }
}

3. Encapsulation:
We create the EatMan class, which encapsulates the behavior and state of an EatMan object. The EatMan class has a stomach capacity, current energy level, and methods to eat food and report its energy level.

public class EatMan {
    private int stomachCapacity;
    private int currentEnergyLevel;

    public EatMan(int stomachCapacity) {
        this.stomachCapacity = stomachCapacity;
        this.currentEnergyLevel = 0;
    }

    public void eat(Food food) {
        if (currentEnergyLevel + food.getCalories() <= stomachCapacity) {
            currentEnergyLevel += food.getCalories();
            food.consume();
        } else {
            System.out.println("Stomach is full, cannot eat more!");
        }
    }

    public int getCurrentEnergyLevel() {
        return currentEnergyLevel;
    }
}

4. Unit Testing:
To ensure the correctness of our implementation, we employ unit testing. We write test cases to verify that the EatMan class functions as expected.

public class EatManTest {
    @Test
    public void testEatMan() {
        EatMan eatMan = new EatMan(1000);

        Apple apple = new Apple();
        Pizza pizza = new Pizza();
        Steak steak = new Steak();

        eatMan.eat(apple);
        assertEquals(100, eatMan.getCurrentEnergyLevel());

        eatMan.eat(pizza);
        assertEquals(600, eatMan.getCurrentEnergyLevel());

        eatMan.eat(steak);
        assertEquals(900, eatMan.getCurrentEnergyLevel());

        eatMan.eat(steak);
        assertEquals(900, eatMan.getCurrentEnergyLevel()); // Stomach is full
    }
}

Conclusion
In this article, we embarked on a journey to solve the Alibaba interview question of implementing an EatMan class. We employed object-oriented programming principles such as inheritance, polymorphism, and encapsulation to create a robust and flexible solution. Furthermore, we discussed the importance of unit testing in ensuring the correctness of our code. By thoroughly understanding the problem requirements and applying our programming knowledge, we demonstrated our ability to tackle challenging technical questions effectively. This experience not only enhances our programming skills but also prepares us for future technical interviews and real-world software development scenarios.