Lab 9
An Object Oriented Logistics System
(Sheep Based)
Introduction
============
Kudos to you lucky few who have survived your first glimpse at
Computer Science. Usually the response to all the code and all the
rest of what dwells beyond the silicon curtain is similar to the
response to R'lyah. (To look upon it is madness). Take a moment to
reflect on the wide range of problems we have solved. We have
explored the laws of physics, learned about fundamental triangle
relationships (and the loyalty between pupils and teachers),
explored ancient number systems, learned how to stuff a sheep into a
computer, and (recently) harnessed the raw power of dynamic memory.
We have done all this, and yet we have only scratched the surface of
what lies ahead in computer science. There is a wonderfully rich
field stretched out in front of you, and you have taken the first
steps in exploring it! I hope you enjoy this final lab, as well as
the rest of your studies.
Now! Down to business! While it is a lot of fun to explore simple
computing problems, eventually every computer scientist has to come
down from the ivory towers and write an actual computer
application. That's where this lab comes in. You are going to use
the objects we developed in lab 8, as well as the ones from the
guided lab in class, to build a full blown application. This sort
of application programming is what OOP is really used for as it
allows us to abstract away a lot of the application and offload the
actual business logic to a series of objects. This lab will involve
a fair bit of code, but you've already written most of what you'll
need. There will be plenty of places where you can improve upon
this application, and as always, I will award extra credit for any
nice touches you place on this application.
This program, while still set in the ancient world, is a common type
of application in use today. We will be writing a logistics
system. A logistics system is a system which manages the coming and
going of merchandise within a company. Generally these interface
with inventory control systems so as to allow the company to know
what it has on hand, what is out in transit, and what shipments are
expected to arrive. Logistics is actually a very rich area of
study, and these systems can become quite complex very quickly.
This is why we need the abstraction of objects, otherwise the
complexity would be overwhelming and we could not achieve even a
simple logistics system. The system which you will be building is,
of course, a very simple system. However, it will give you a feel
for how a larger application system is built. For those of you who
are going to be coding for The Man, this will also give you a feel
for the sorts of applications you are going to be writing in the
very near future!
Oh, and as always, this lab contains a bit of very odd humor, though
it is a little subtle. If you read up on your Roman history,
however, it is worth at least a chuckle. If you really know your
Roman history, then there is a point of inaccuracy which goes beyond
the idea of the ancient Etruscans using computers.
Lab 9.1
=======
You will need the linked list template class we developed in class.
If you haven't already completed this, scanned images of my code are
available. You will need to have completed the ListNode and List
classes. When you submit your complete lab, you will get credit for
this as a guided lab.
Lab 9.2
=======
Emboldened by the invention of object oriented programming, the
Etruscan Software Development company decided to produce something
of real value. Something that would save the company gobs of money
by eliminating dozens of jobs. They had a plan, and they met with
Stan one night to show it to him.
"Yes lads, I understand you have something to show to me?"
"Indeed we do!" called the programmers in unison.
"But why are meeting in the dead of night?"
"Because," said Reg (who had taken up programming), "we don't want
the logistics clerks to see it."
"But why?" asked Stan.
"Because once we have this, they'll be redundant. We'll have to
send them all to the dole queue!"
"Ah! I like the sound of that!" replied Stan, rubbing his hands
together greedily. He could already smell the Corinthian leather in
his new chariot if this went through. "Tell me then, how does it
work?"
"Well," said Reg, "Currently our clerks make a listing of all of our
orders, then they pass this on to the logistics staff who then divvy
up the sheep between ships, right?" "Right" nodded Stan. "Well, we
could replace them with a program that we hand the sheep orders to.
The first screen would look like this..." Reg bent down and sketched
the first screen in the sand:
-- Begin Main Menu --
Welcome to the Shepherd Sheep Shipping Logistics Program!
MAIN MENU
1.) Add a Sheep to Ship
2.) Print number of needed ships.
3.) Print number of ships for each destination.
4.) Print Manifests
5.) Clear Sheep List
6.) Quit
Choice: 1
-- End Main Menu --
"Then, when we enter a new sheep, the system will work out how many
ships we need and then the total value of each of those ships. We
can give the more valuable shipments to our more reliable sea
captains." Reg then sketched out the interface for entering the
sheep:
-- Begin Sheep Entry --
Sheep Entry
Sex (M/F): M
Origin [(T)uscany, (J)udea, (G)reece]: J
Destination: Gaul
Cost: 30
Has the sheep been sheared (Y/N)? N
Color: Plaid
-- End Sheep Entry --
"Oh, I like the look of that!" exclaimed Stan. So then what
happens? "Well we run reports. For instance, the next thing we
need to know is how many ships to order." "Right!" "As you know,
that is a complex problem. Each ship can only hold 10 sheep, and
can only sail to one destination on a single outing. So the program
will take all that into account and tell us the total number to
hire."
-- Begin Ship Count Report --
You will need 4 ships.
-- End Ship Count Report --
"How quickly can it work that out?" Stan asked in an astonished
tone. "Almost instantly!" Bragged Stev (one of the programmers).
"Good God! It takes Augustus over a day to work that out! I guess
he'll have to go back into politics." "I guess so" replied Stan,
"What harm could it do?"
"Next, we want a break down of how many ships are going to each
country. That way we can alert the docks as to how many to expect.
Also, we want to know how many sheep and how much they cost. This
will be useful to our marketing department. They will know the
wealthy regions as well as the regions which need a little more
development. Their report will look like this:"
-- Begin Destination Report --
Ships per Destination
---------------------
Destination Ships Sheep Total Value
----------- ----- ----- -----------
Gaul 1 2 40
Anglesey 2 15 170
Sparta 1 4 60
-- End Destination Report --
"The final report is a listing of what sheep are on which ship.
This is automatically worked out by the program. A short version of
this report is as follows: "
-- Begin Shipping Manifest --
Shipping Manifests
-------------------
Ship #1 Bound for Gaul
Sex Origin Sheared Color Cost
--- ------ ------- ----- -----
M Judea N Plaid 30
M Tuscany Y Black 10
=====================================
2 Sheep Total Value: 40
... and so on for the other ships ...
-- End Shipping Manifest --
"That's brilliant! Quick! Cover it! Augustus is coming!" They all
nervously scratched out what they had written. Augustus hardly
noticed them, I mean, why would he suspect they were all plotting
against him. Besides, he was excited because his adoptive father
was addressing the senate the following day. His family was really
going places! Nothing bad was about to happen to him or his dad.
Nothing bad at all....
"Ok, now the best part. You don't have to do all your work in one
run of the program. We'll store the list of sheep in a file, and
when you resume the program, it will remember what was there."
"Ah, so that's why you've included a clear function?"
"Yes, when you call clear, it will empty out the list of sheep like
so:"
-- Begin Clear --
Clear Sheep
-----------
Are you sure you want to clear this shipping list (Y/N)? Y
Sheep list cleared
-- End Clear --
"So once the ships ship the sheep we simply clear the sheep ship
data of the shipped sheep?"
"Right!"
"Ok! This looks great Reg. Start on it right away!"
Procedure
=========
You heard the man! Get coding! Note that the entire interface is
laid out for you. Also, you already have most of the classes you
need to get started. Here's the order that I would tackle this in.
Note that this is just a recommended way to compartmentalize the
program. If you work with just one part at a time this really won't
be so bad. Force yourself to stay with each item until it is 100%
complete, then move on to the next.
1. Create the menu system.
2. Create the entry for one sheep.
3. Create a global variable which will store a list of sheep. You
will want to sort the sheep by destination. This means that you
will need to implement overloaded operators for the comparison
operators for the sheep class and use addSorted when adding to
the list.
4. Implement a class for counting the sheep going to each
destination. I did this with a struct containing two elements.
Whenever I ran the reports which show the number of ships per
destination and/or the shipping manifest, I called a function
which implemented the following psuedocode (note that you will
have to adjust to how you implemented your sheep class in lab
8.):
Struct Destination
string name
int count
int value
Destination d
List<Destination> dl
for each sheep s in the list
if d.name != s.destination
if d.count > 0
dl.add(d)
d.name = s.destination
d.count = 1
d.value = s.cost
else
d.count++
d.value += s.cost
end for
dl.add(d)
After you run the above, you can scan through the destination
list and print out the number of sheep. To compute the number
of ships, you'll need the ceiling of the sheep count divided by
ten. So:
ships = (int) ceil(d.count / 10)
Note that ceil is in the cmath library. Make sure you read over
and understand the above psuedocode and this math. I have just
given you a sketch of how to do it. The actual context of this
is completely up to you.
5. After you get that working, work out how to print out your ship
manifest list. This is going to be similar to what we did to
figure out how many ships would be at each element. You can do
this with dynamic arrays of ships as we know how many ships we
need at each point. Just make an array with the total number of
ships in it. You can work this out using the formulas listed in
step 4. Populate the arrays, and then use the
ship's print functions to print out the manifest. Note that
you'll need to modify the sheep print function in order to make
things print the way we want. Do the print formatting last.
6. Get the file storage mechanism to work. Everything is driven by
the sorted sheep list, so that is the only thing you need to
write to the file. I just put each sheep field on a line in the
file. This is very easy to read and write with. This is super
easy to write if you build sheep insertion and extraction
operators.
Basically the sequence of events should work like this:
Program Start
Load the sheep file
Do all the menu items the user wants
Save the file when the user selects to exit
7. Dress it up a bit. Maybe you could make the input be done in
Roman Numerals or perhaps you could have ships that have more
complex paths. Perhaps you could have a ship visit multiple
locations, but you'd have to work out the itinerary for each
ship. The more extras you add, the more points I'll give you on
your final exam. If you have big plans, let me know and maybe
we can make this program count as your final exam as well!
Closing Thoughts
================
You all have worked very hard this semester. I have really enjoyed
being your teacher, and I hope you will get many years of enjoyment
out of computer science. I know at this juncture it can feel very
frustrating and overwhelming (especially given how hard I've worked
you this semester), however if you stay with it a little more,
you'll find that you will gain mastery of the subject in no time!
Also, consider this. Our world works on computers. Computers are
the pinnacle of human achievement in which we have finally harnessed
the promise of mathematics and fused it with logic. Math,
philosophy, art, language, poetry, and more; computers touch on
virtually everything our species has accomplished in the past two
and a half millenia. If you keep at it, you'll get to explore all of
that. A computer scientist walks with the ancients while pulling
the world into the future.
I can't put into words just how vast and fascinating our field is.
Suffice it to say that all the pains you take to learn how to code,
how to prove algorithms, how to build computers, and how to conceive
of universal computation is well worth it. A world of unspeakable
beauty and complexity is stretched out before you. It's your
birthright as a human, and I hope you will step up to take the
challenge and enjoy yourself while doing it!
Now, all that being said, you must write programs and think of
algorithms as much as possible in the coming weeks. Please, don't
put this down until the next semester, especially if you have a
non-cs course in between this and your next bought with
programming. To that end, I have some recommendations for you:
* Free Online Books & Resources
- Structure and Interpretation of Computer Programs
http://mitpress.mit.edu/sicp/
This book has been used for many years as the introductory text
in computer science at MIT. This book will teach you to program
in Scheme, a dialect of LISP. Honestly, if I had my choice, I
would have either taught you LISP or C, not C++. The reason is
that C++ has gobs of syntax, LISP has practically none. Because
the language is simple, this book quickly goes into detail about
algorithms, parsers, data structures, and more. You'll learn
LISP organically as you get a survey of the computing you'll be
doing in the future. I read this book when I was a freshman in
college, and it really put me ahead of the rest of my
classmates. My school used C++, as yours does, but the ideas
where equally applicable. Read this book, and you'll be better
at all programming languages.
This book will show you what computer science really is.
Reading this book is one of the things that has led me to love
computer science so much!
- Mathematics for Computer Science by Albert R Meyer
http://archive.org/details/ost-computer-science-mcs
This is a recent book released under creative commons. This
details the mathematics of theoretical computer science. It is
a very nice and very complete treatment of the matter. Read
this over, and you'll ace your discrete structure and theory of
computation courses. Also, I believe this will be a good
introduction to higher mathematics for you. There is nothing
quite so exhilarating as studying infinity with nothing more
than a pencil and a legal pad in your hand.
- Meno by Plato
http://www.gutenberg.org/ebooks/1643
While not strictly a computer science book, there are still many
things in this classical work. In particular, we see Socrates's
theory of forms and inborn knowledge. The discussions in this
short dialog sound an awful like object oriented programming.
Meditating on Socrates can make you a better programmer, and
also can make you more interesting to talk to. The world needs
more people that have read Meno, and fewer that have watched
walking dead. Read Meno. Be wise.
- Project Euler
http://projecteuler.net/
This is a great place to test out your programming chops. This
site gives you problems to solve, and then you solve them and
post the correct solution. Once you get it right, you gain
access to a forum where you can discuss your solutions.
I often use project Euler as a problem source for when I am
learning new programming languages. Lately, I've been writing a
lot of go-lang code for these problems. This will give you a
way to keep your brain thinking in C++, LISP, or whatever
language you decide to love.
- TopCoder Programming Contests
http://www.topcoder.com/tc
This is kind of like mad max for geeks (except everyone
survives). There are a lot of modes of competition here,
ranging from algorithm writing to marathon complex problems.
It's challenging and fun for programmers at all levels. Come on
in and pit your coding against others!
* Start your CS library!
If you want to be a serious scholar in any field, you must build a
personal library in it. In Computer Science this can be tricky
because there a lot of books about programming, but few on
computer science itself. Here's my recommendations for starting
your library.
- The C Programming Language 2nd Edition by Brian Kernighan and
Dennis Ritchie. Every programmer has a copy of this one. You
should too.
- The C++ Programming Language by Bjarne Stroustrup - A standard
text for anyone who has to use C++. This book is a great
reference. If you read it and understand it all, you'll be a
C++ wizard.
- The Art of Computer Programming by Donald Knuth
This is quite possibly the only comprehensive introduction to
pure computer science that has ever been written. I got my copy
when I was a junior in high school. It was so intriguing, and
confusing, that I decided to major in computer science just so I
could understand the concepts in these books. (It's a 4 volume
set so far. More volumes are on the way!) If you buy these
books now, they will provide you with hours of enjoyment all the
way through your BS up until your PhD.
Another thing you may try is reading through the source code of some
open source applications. Look at the Linux source (it has a
document in the source which guides you through it). Look at
firefox, gnome, something. Just make sure you think about computing
this summer. I'm sure you will.
Enjoy!