It's 2020. The mentors of Codecool Phnom Pen (capital of Cambodia) are fed up, that despite the strict house rules, no one cares about collecting the waste in a selective way in the school. They decided to make an automated dustbin, which can detect different types of garbage, and can put them to different containers automatically.
Mentors at Codecool usually have a whole bunch of things to do, and they aren't exceptions either, so they don't have time to implement the dustbin's software. Luckily it's internal computer is capable of running Java programs...
Have you found out yet??? YES, it's your job to implement it in an Object-Oriented way!
You're required to implement the following classes
GarbagePlasticGarbagePaperGarbageDustbinProgram(this one is special, read on)
and make the tests pass!
Run the program with dotnet run and the tests with dotnet test.
Note: don't touch the test files!
In the Program class you need to write code the serves as a "movie script" for a little story.
Think of the contents of the main method as series of things (statements) that happen over time in the life of an ordinary dustbin.
A short script could be ...
Workers in the cantine
They leave three rotten tomatoes and a plastic milk jug on the floor beside the dustbin.
They leave the scene.
Cleaning lady arrives
She strolls into the cantine and she's furious as she sees this mess.
She throws the three tomatoes -- one-by-one -- into the dustbin.
Picks up the plastic jug.
She checks if it cleans (it's not obviously!).
She cleans it, then throws that too into the bin.
She clears her throat, empties the dustbin.
Finally she leaves the scene.
A possible translation of this to code could look like as follows.
Note: We only need to create objects that necessary for us for the purposes of the assignment (no need for a cleaning lady class or anything like that).
// We create the garbage objects that appear in the script.
Garbage[] rottenTomatoes = new Garbage[3];
for (int i = 0; i < 3; i++)
{
rottenTomatoes[i] = new Garbage("rotten tomato nr." + (i + 1));
}
// Then we create the plastic milk jug.
PlasticGarbage milkJug = new PlasticGarbage("plastic milk jug", false);
/*
Note that on the leftside the type is Garbage, but on the right it's PlasticGarbage.
We can do this, because PlasticGarbage extends Garbage, which in simple terms
means that every plastic garbage is garbage, but not every garbage is plastic garbage.
*/
// We create the dustbin where the garbages will be thrown.
Dustbin dustbin = new Dustbin("Jenny's handsome");
// Showing the contents of the dustbin for the sake of seeing something on the terminal :)
dustbin.DisplayContents();
// Then the cleaning lady comes and does her thing.
for (int i = 0; i < 3; i++)
{
/*
She throws every piece of rotten tomato in the dustbin.
This doesn't mean the tomato Garbage instance will be destroyed or anything,
they are just now inside of the Dustbin object.
*/
dustbin.ThrowOutGarbage(rottenTomatoes[i]);
}
// Then she cleans the milk jug.
if (!milkJug.Cleaned)
{
milkJug.Clean();
}
// Throws out the milk jug.
dustbin.ThrowOutGarbage(milkJug);
// Empties the contents.
dustbin.EmptyContents();
// Displaying what's in there.
dustbin.DisplayContents();
// Aaaaaaand the scene fades out!
Your goal is to play around and create something like this. Two rules apply
- create lots of instances, pass them around, see what happens, see what compiles, what doesn't,
- whatever you create it should compile and you should be able to run it :)
This is the file containing a regular garbage's logic.
string Name: stores the custom name of the garbage object (e.g."rotten tomatoes")
Garbage(string name): the class has a single constructor that takes thenameof the garbage to be created.
This is the file containing the logic of a garbage made of paper.
The PaperGarbage class inherits the logic of the Garbage class.
string Name: stores the name of the garbage (should be inherited from theGarbage)bool Squeezed: stores if the garbage is squeezed (true) or not (false)
PaperGarbage(string name, boolean squeezed): the class has a single constructor that takes two parametersname(the name of the paper garbage andsqueezed(trueif the garbage is squeezed from the start orfalseotherwise).
- when called it sets the object's
Squeezedfield totrue
This is the file containing the logic of a garbage made of plastic.
PlasticGarbage class inherits the logic of the Garbage class.
string Name: stores the name of the garbage (should be inherited from theGarbage)bool Cleaned: stores if the garbage is clean (true) or not (false)
PlasticGarbage(string name, bool cleaned): the class has a single constructor that takes two parametersname(the name of the plastic garbage andcleaned(trueif the garbage is clean from the start orfalseotherwise).
- when called, it sets the object's
Cleanedattribute totrue
This file should contain all the logic, what our automated dustbin can do.
PaperGarbage[] paperContent: an array, storesPaperGarbageinstancesPlasticGarbage[] plasticContent: an array, storesPlasticGarbageinstancesGarbage[] houseWasteContent: an array, storesGarbageinstances
string Color: stores the dustbin's colorPaperCount: read-only, returns the number ofPaperGarbageinstances in the dustbinPlasticCount: read-only, returns the number ofPlasticGarbageinstances in the dustbinHouseWasteCount: read-only, returns the number ofGarbageinstances in the dustbin
If it's called it prints all the contents of the dustbin in the following format
Red Dustbin!
House waste content: 2 item(s)
Rotten tomato
Wooden leg
Paper content: 0 item(s)
Plastic content: 1 item(s)
Milk jug
The first line is the dustbin's color + " Dustbin!".
- Receives an argument.
- If the argument is an instance of the
PlasticGarbageclass, and it's clean, then it puts that into thePlasticContentarray. - If the
PlasticGarbageinstance is not clean, it raises aDustbinContentException. - If the argument is an intance of the
PaperGarbageclass, and it's squeezed, then it puts that into thePaperContentarray. - If the
PaperGarbageinstance is not squeezed, it raises aDustbinContentException. - If the argument is an instance of the
Garbageclass (but not aPaperGarbageor aPlasticGarbage), then it puts that into theHouseWasteContentarray. - If the argument is not an instance of the classes above, it raises a
DustbinContentException.
- If it's called,
PlasticContent,PaperContentand theHouseWasteContentarray gets emptied.