- One cock sells 5 yuan, one hen 3 yuan, and one yuan for three chickens. If you want to buy 100 of them with 100 yuan, how should you buy them?
- From Wuhan to Beijing you can choose from 4 means of transportation, including driving a car, taking bus, train, or plane. Driving costs 500 yuan, bus 300 yuan, train 400 yuan, and plane 1000 yuan. With the budget of 500 yuan, which vehicles Xiaoming can choose to get there?
GitHub Repository: https://github.com/bacaljo/abstract_class_and_interface.git
- Create an
Animal
andChicken
abstract classes that will have age group and gender as attributes. - Create concrete classes for the three chickens by inheriting
Chicken
abstract class (Cock
,Hen
,Chick
). These classes will not expose any parameters in their constructors so that their age groups and genders will not be modifiable outside the class. - Create market-related classes and interfaces that will handle the listing and association of animals with their prices and quantities (
AnimalPrice
,AnimalMarket
,Animal Bundle
). - Create a concrete class for the chicken market by inheriting
AnimalMarket
interface. This class will contain the price list of the chickens. - Create an abstract class for our buyer object which will have money as attribute (
Buyer
). - Create an interface that will contain the method to choose all possible animal combination whose total matches the given quantity, base on an animal price list (
AnimalBuyer
). - Create a concrete class for the chicken buyer by inheriting
Buyer
abstract class. This will implement theAnimalBuyer
interface so that it can have the ability to choose animal bundles, specifically chickens, by providing its own implementation. - Update the logic of
ChickenBuyer.chooseAnimalBundleList()
to perform trial and error until it extracts all possible chicken combinations that satisfy the requirements.
- Create an abstract class for mode of transportation (
ModeOfTransportation
). It will have acost
attribute and atoString()
abstract method, which will return the display name of the subclass mode. It'scost
attribute will be private and will only have a getter, so we can only assign its value during instantiation in the constructor. - Create concrete classes for the four transportation modes by inheriting
ModeOfTransportation
abstract class (DrivingACar
,TakingABus
,TakingATrain
,TakingAPlane
). Since we want all instances of each implementations to have a fixed cost, we will assign the cost during thesuper()
call in the subclass constructors and not expose acost
parameter. - Create an interface that will have a
chooseAffordableModeOfTransportation()
method that accepts the list of availableModeOfTransportation
and returns the list of affordableModeOfTransportation
(ProblemSolver
). This will be implemented by our person object. - Create a person class that will have
name
andbudget
as attributes (Person
) with their respective getters and implement theProblemSolver
interface. For thechooseAffordableModeOfTransportation()
logic, we will return the list of affordableModeOfTransportation
by getting those from the list of availableModeOfTransportation
whosecost
is less than or equal to the person'sbudget
. - Create an executable class that will instantiate a person object given the details in the requirements, create a list that will contain all implementations of
ModeOfTransportation
then make the person choose which mode of transportation he/she can afford (TransportationProblem
).