ITEC 1630 Object-Oriented Programming, Sections M, N, and O, Winter 2007
Assignment 1
Due date:
For all sections M, N, and O of ITEC 1630, the assignment must be
submitted by Monday February 19 at 7pm.
To submit:
Prepare a single zip file containing all the source files
(.java files, NOT .class files) and
testsOutput.txt. Submit this file as an attachment to an
email sent to: itec1630@atkinson.yorku.ca. Put in the subject
line your section (M, N, or O), name, student number, and the words
"Assignment 1". Please make sure that your section is entered
correctly. For example, if your name is Jane Doe, and your section is
M, your subject line will be "M Jane Doe 999999999 Assignment
1". Your section must be in capital case. Your submission will
not reach your TA and will therefore NOT be marked if a wrong section
code is used in the subject line. It is highly recommended that you
CC yourself on this email. You can email the program any time before
the deadline. A printout of the source files and
testsOutput.txt (no cover page is necessary but make sure the
identification section at the beginning of
SuperPhoneCardInc.java is filled out and appears on top) is
to be put in the drop box on the 3rd floor of TEL building before the
deadline.
Details
SuperPhoneCard Inc. sells phone cards for making cheap long distance
calls around the world. In this assignment, you will write a
(much simplified) Java program to manage their business.
SuperPhoneCard Inc. sells 3 types of phone cards: SuperNA10 cards,
which cost $10 and are good only for calls in Canada and the USA,
Global10 cards, which cost $10 and are good for overseas calls, and
Global25 cards, which cost $25 and are also good for overseas calls.
The per minute rates for each type of card and call zone are as follows:
| SuperNA10 | Global10 | Global25 |
Canada | $0.05 | $0.07 | $0.05 |
USA | $0.10 | $0.15 | $0.10 |
Europe | XXXXX | $0.30 | $0.20 |
Asia | XXXXX | $0.60 | $0.40 |
Australia & NZ | XXXXX | $0.45 | $0.30 |
Latin America | XXXXX | $0.45 | $0.30 |
Africa | XXXXX | $0.60 | $0.40 |
The initial balance on the cards and the weekly maintenance fee are
indicated below:
| SuperNA10 | Global10 | Global25 |
initial balance | $10.00 | $10.00 | $25.00 |
weekly fee | $0.50 | $1.00 | $1.00 |
Your main job in this assignement is to implement a hierarchy of
classes to represent the different types of cards. At the top of the
hierarchy, there is an abstract class PhoneCard with the following
API:
-
public PhoneCard(long no, int passwd, double bal): a constructor
(precondition: no and passwd must be positive);
-
public long getNumber(): an accessor returning the card number;
-
public int getPassword(): an accessor returning the card password;
-
public double getBalance(): an accessor returning the card balance;
-
public void setBalance(double bal): a mutator to set the card balance;
-
public abstract boolean allowed(CallZone zone):
a predicate that returns true if a call to the argument zone is allowed for
the card;
-
public abstract double costPerMin(CallZone zone):
returns the cost per minute of a call to the argument zone;
-
public int getLimit(CallZone zone):
returns the maximum number of minutes that can be charged
for a call to the argument zone given the card's balance
(truncated down to the next integer);
-
public boolean charge(int minutes, CallZone zone):
tries to charge a call to the given zone with the given number
of minutes to the card; if the balance is sufficient to cover it,
it returns true and if the balance is not sufficient,
it leaves it unchanged and returns false
(precondition: minutes must be positive);
-
public abstract void deductWeeklyFee():
deducts the appropriate weekly fees from the card's balance, leaving
it non-negative;
-
public String toString():
returns the string "card no no has a balance of
X.XX".
There are also 3 classes, SuperNA10Card,
Global10Card, and Global25Card that inherit from
PhoneCard, and model the properties of the associated type of
card as specified above, by defining the PhoneCard class's
abstract methods. These classes should use the supplied CallZone.java class to represent
call zones; it provides methods to convert strings representing the
call zones to CallZone objects and to check if such strings are valid.
Once you have defined these classes, you should then complete the
application that SuperPhoneCard Inc. will use to manage its business. This
application is implemented by the SuperPhoneCardInc class, that reads
and processes a number of commands from the standard input stream for
processing cards and calls, outputing the results on the standard
output stream. The commands are:
-
add no passwd cardType:
adds a card of the given type with the given number, password, and type
(a cardType is either SuperNA10, Global10,
or Global25);
-
getBalance no passwd: prints the balance of card
no if the password passwd is valid;
-
getLimit no passwd callZone:
prints the maximum number of
minutes that can be charged to card no for a call to callZone
given the card's balance if the password passwd is valid and calls
to callZone are allowed for this card (a callZone is either
CANADA, USA, EUROPE, ASIA, ANZ,
LATINAM, or AFRICA);
-
charge no passwd callZone minutes:
charges card no for a call of minutes to callZone
if the password passwd is valid, the balance
is sufficient, and calls to callZone are allowed for this card;
-
deductWeeklyFee: deducts the weekly fee from all cards;
-
printAll: prints the balance of all cards.
An incomplete definition for the SuperPhoneCardInc class
is available here in
SuperPhoneCardInc.java.
You must fill out the missing parts of the definition, i.e. the code
for handling the following commands:
-
getLimit no passwd callZone:
if the command can be executed successfully, print
"Result: card no limit for zone
callZone is XX minutes",
otherwise print error messages as follows: if the number of
arguments or their type is wrong, print "Error: invalid arguments for
getLimit command"; if the card does not exists, print
"Error: card no no does not exist";
if the card's password is wrong,
print "Error: password passwd incorrect";
and if calls to the given zone are not
allowed for this card, print
"Error: card no not allowed for zone callZone";
-
charge no passwd callZone minutes:
if the command can be
executed successfully, print
"Result: card no charged X.XX, new
balance is Y.YY", otherwise print error messages as follows: if the
number of arguments or their type is wrong, print "Error: invalid
arguments for charge command"; if the card does not exists, print
"Error: card no no does not exist";
if the card's password is wrong,
print "Error: password passwd incorrect";
and if calls to the given zone are not allowed for this card,
print "Error: card no not allowed
for zone callZone";
and if the card's balance is not sufficient to cover the call,
print "Error: card no limit for zone callZone is XX
minutes".
The SuperPhoneCardInc uses another class CardTable
to manage a table
of PhoneCards. An incomplete definition for the CardTable class is
available here in
CardTable.java.
You must also fill out the missing parts of the
definition of CardTable,
i.e. the public PhoneCard get(long no) method
that returns the first phone card in the table whose number matches
the argument, or null if there is no card with this number.
Make sure your code compiles and runs under the JDK available in the ITEC labs.
Test your program with assertion checking enabled, e.g. with
java -ea SuperPhoneCardInc (see Horstmann pp. 335-338 for details).
Once you have implemented and thoroughly tested all of these classes,
run your application on the test data file
testsInput.txt and save
your output in the file testsOutput.txt. Under Unix and Windows XP,
you can do this
by redirecting the standard input stream and standard output stream
with the command:
java -ea SuperPhoneCardInc < testsInput.txt > testsOutput.txt
Then submit a zipped archive with all your source code files and
testsOutput.txt by the deadline as specified above.