Download Programming Assignment #1 - Computer Science and Engineering

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Programming Assignment #1
CSE 452: Fall 2004
Due Date: 10/13/05
Instructions: This programming assignment is designed to develop your Python
programming skills and to illustrate how object oriented programming can be used for
web applications. This assignment is the first step towards our final system—an online
shopping system. In particular, the assignment will familiarize you with generating web
pages dynamically using Python. All files are to be handed-in by midnight on the due date
shown above.
Programming Exercises:
Explanation:
In this assignment, you should implement the GUI (Graphical User Interface) part of the
online—shopping system, E-SPARTY. You will be developing a Python program that will
generate the appropriate web page(s) for E-SPARTY.
1. The resulting web interface should:
 Display at least 3 categories of goods for purchase (the number should be
flexible and easily modifiable for future assignments).

Display a table of goods available for purchase for each category. Each table
should have the same basic appearance. The table should minimally list:
name and description of item, unit price, and quantity.

There should be some representation of the current status of their
purchases (e.g., the contents of their shopping cart). For now, you should
have the interface that would allow users to change the contents of their
shopping cart (e.g., remove items, change quantity.). Those operations
need not be functional for the current assignment, but will be needed in an
upcoming assignment.
2. Create a UML class diagram depicting the design of your Python system. Be sure to
include inheritance and polymorphism in your design. Describe your design,
including a data dictionary explaining all the elements of your class diagram (e.g.,
classes, attributes, and operations).
3. Be sure to document your code carefully and thoroughly.
4. Create a README file explaining how to run your Python program, how to use the
resulting web application, and any other special instructions needed to understand
your solution.
Below are some sample code snippets that illustrate some key features in Python that
might be useful in designing your solution.
(Hyperlink object): (HTMLObject is some generic HTML object, you can define it to have
minimal functionality.)
--------------------------------------------------class HTMLHyperlink(HTMLObject):
#The object's constructor
def __init__(self,theURL,theText):
self.LinkURL=theURL
self.LinkText=theText
def writeHTML(self):
print("<a href=" + self.LinkURL + ">" + self.LinkText +
"</a>\n")
This simple class is created for the sole purpose of creating a hyperlink. You can extend
it through inheritance. All the object does, when initialized, is store its link values
locally. Then when the parent object (say an HTML form or a table) calls its writeself
function, it returns the fully formed HTML code.
For example suppose we initialize an instance of
HTMLHyperlink:
MyLink=HTMLHyperlink (http://www.cse.msu.edu/, "Michigan
State University Department of Computer Science");
Calling Mylink.writeHTML() returns:
"<a href=http://www.cse.msu.edu/>Michigan
Science</a>\n"
State's
department
of
Computer
You can extend its functionality through inheritance to create an object that makes a
picture hyperlink. For example:
class HTMLPictureHyperlink(HTMLHyperlink):
#The object's constructor
def __init__(self,theURL,theImageURL):
HTMLHyperlink.init(self,theURL,"<imgsrc="+theImageURL+">")
As you can see, all this does is create a special HTMLHyperlink with an image tag in place
of the usual link text. When HTMLPictureHyperlink's writeHTML() function is called, it
will return a fully formed hyperlink with a picture displayed as a link instead of text!
Suppose you have a class HTMLMenu that also inherits HTMLObject's properties, but it
also has children of type HTMLHyperlink. These would be the different menu items
(perhaps arranged in a table, or listed individually on each line).
When you call the HTMLMenu's writeHTML() function, it might look something like:
def writeHTML(menuTitle,menuEntries) :
ReturnValue= " "
returnValue = menuTitle + "<p>";
for i in range(len(menuEntries) :
returnValue += menuEntries[i].writeHTML() + "<p>";
returnValue += "\n”
where menuTitle, and menuEntries are variables owned by HTMLMenu and they
contain the menu's title and their hyperlinks, respectively. menuEntries is a list of
HTMLPictureHyperlink objects or HTMLHyperlink objects.
Therefore, calling HTMLMenu's writeHTML() could produce code like this:
A Sample Menu<p>
<a href=http://www.cse.msu.edu/>Michigan State's department of Computer
Science</a>
<p><a
href=http://www.egr.msu.edu/>Michigan
State's
department
of
Engineering</a>
<p><a href=http://www.msu.edu/>
<img src=http://www.msu.edu/sparty.jpg</a> (an HTMLPictureHyperlink)
<p><a href=http://www.google.com/>Google</a>
<p>
With these basic techniques, you can create an entire hierarchy objects that collectively
work together (by calling the main node's writeHTML() function) to create an entire
page of HTML code ready to be sent back through the web server. In short, everything
within a web page is generated by Python objects that are used to print the HTML code.
Requirements:
While there is significant flexibility in designing your GUI, there are specific features that
must be available in the resulting web interface (see the above descriptions). Take a look
at Amazon.com or similar web pages to get a sense of what good and poor interfaces are,
Below is a simple prototype:
Welcome to Sparty!
CD
DVD
Book
PDA
Game
Car
Food
When you click CD, it goes to the following page:
Welcome to Sparty!
Name
Singer
Style
Price
Quantity
Rainy day
Roberb
R&B
18
0
I love you
CoCo
Pop
20
0
Run away
Micheal
Rock
22
0
Feel not bad
Dido
Pop
21
0
CD
DVD
Book
PDA
Game
Car
Food
Add
Then you can select items you want:
Welcome to Sparty!
CD
Name
Singer
Style
Price
Quantity
Reset
DVD
Rainy day
Roberb
R&B
18
1
I love you
CoCo
Pop
20
2
Run away
Micheal
Rock
22
6
Feel not bad
Dido
Pop
21
9
Book
PDA
Game
Car
Food
Add
Reset
Click Add, it will lead you to confirmation page:
Welcome to Sparty!
Here is your shopping cart:
Name
Price
Quantity
You Pay
Rainy day
18
1
18
I love you
20
2
40
Run away
22
6
132
Feel not bad
21
9
189
Total: 379
Confirm Cancel
If you click Cancel, it brings you back to the main page. When you click Confirm, it will
print the following information:
Thanks for shopping with us!
Web Programming Background:
If you want to do the project on your own machine, before you start, you should:
1.
Download
and
install
Apache
httpd
server
in
your
computer (http://httpd.apache.org/)
2.
Download and install Mod_python for enabling python in Apache httpd server.
(http://www.modpython.org/). When finished, find httpd.conf under some
directory where the Apache is installed/Apache Group/Apache 2/conf. Find lines
begin with LoadModule (there are couples of them), add this line:”LoadModule
python_module modules/mod_python.so” at the end.
3.
Let Apache handle your Python program:
Next, add the following lines:
<Directory "C:/Program Files/Apache Group/Apache2/htdocs/test">
AddHandler mod_python .py
PythonHandler index
PythonDebug On
It means that Apache Server will handle a python program named index.py that is under
directory specified by
C:/Program Files/Apache Group/Apache2/htdocs/test.
Quick-Start Guide to Python:
Here is a link for learning python:
Tutorial: http://www.python.org/
There are two books that are helpful:
1. ProgrammingPython ,2nd edition. By Mark Lutz
ISBN
: 0-596-00085-5
In Part III: Internet Scripting, you will find some useful examples.
2.
Python Cookbook
By Alex Martelli, David Ascher
Publisher
Pub Date
ISBN
: O'Reilly
: July 2002
: 0-596-00167-3