In: Computer Science
repare a single compressed file in .zip format containing all the source files (.java files, NOT .class files) and the output file generated by running your code (i.e., testsOutput.txt) and submit it through Moodle. To be more precise, your submitted .zip file should contain the following files: PhoneCard.java, SuperNA10Card, Global10Card, Global25Card, SuperPhoneCardInc.java, CardTable.java, CallZone.java, and testsOutput.txt.
Note:
(1) Your assignment will be given a zero mark if only the compiled files (.class files) are submitted. No exceptions. Please make sure to submit the source files (.java files).
(2) Please make sure your code compiles under the command line (i.e., without an IDE). All Java classes should be put under the default package. Do not put any package statement at the beginning of your source file. If you are using an IDE, this is especially important because some IDEs put your code under a particular package for your project. Any code that does not compile under the command line can only receive 20/100. (See below for the marking scheme).
(3) Finish only the parts as indicated; do not attempt to change other parts of the code.
(4) Please pay attention to variable naming. JavaDoc comments are required for both the code you write and the code already provided.
(5) Please pay close attention to the formatting of the output. For example, when asked to print charged X.XX, make sure only two digits are printed after the decimal point. (Hint: Use DecimalFormat or System.out.printf().)
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:
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 check if the strings representing the call zones 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:
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:
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 Java SE Development Kit 8 or above. 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. 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 (containing PhoneCard.java, SuperNA10Card, Global10Card, Global25Card, SuperPhoneCardInc.java, CardTable.java, and CallZone.java) and testsOutput.txt by the deadline as specified above. The test output file corresponding to the given testsInput.txt is here. Note that we are going to use a different input file to test your program.
Hi,
Please find the classes below:
//////////////////
import java.text.DecimalFormat;
//PhoneCard.java
public abstract class PhoneCard {
protected long number;
protected int password;
protected double balance;
protected double costPerMinute;
//PhoneCard constructor
public PhoneCard (long no, int passwd, double
bal){
//(precondition: no and passwd must
be positive);
assert no >= 0;
assert passwd >=0;
this.number=no;
this.password=passwd;
this.balance=bal;
}
//an accessor returning the card number;
public long getNumber(){
return number;
}
//an accessor returning the card password;
public int getPassword(){
return password;
}
//an accessor returning the card balance;
public double getBalance(){
return balance;
}
// a mutator to set the card balance;
public void setBalance(double bal){
this.balance = bal;
}
// returns true if a call is allowed for the
card;
public abstract boolean allowed(String zone);
//returns the cost per minute
public abstract double costPerMin(String zone);
//returns the maximum number of minutes
public int getLimit(String zone) {
double maximumMinutes =
balance/costPerMin(zone);
return
(int)maximumMinutes;
}
// returns the cost of the call with the given
number of minutes to the card
public boolean charge(int minutes, String zone)
{
double charge =
costPerMin(zone)*minutes;
if(charge< balance){
balance=balance-charge;
return true;
}
else
return false;
}
// deducts the appropriate weekly fees from the card's
balance
public abstract void deductWeeklyFee();
//toString() method
public String toString(){
DecimalFormat df = new
DecimalFormat();
df.setMaximumFractionDigits(2);
return "Card Number ="+ number + "
, Balance= " + df.format(balance);
}
}
///////////////////
//SuperNA10Card.java
public class SuperNA10Card extends PhoneCard {
protected final double costPerMinuteCan =
0.05;
protected final double costPerMinuteUsa = 0.10;
protected final double weeklyFee=0.50;
public SuperNA10Card(long no, int passwd) {
super(no, passwd, 10.0);
}
@Override
public boolean allowed(String zone) {
if(zone.equals(CallZone.CANADA.toString()) ||
zone.equals(CallZone.USA.toString())){
return
true;
}
else {
return
false;
}
}
@Override
public double costPerMin(String zone) {
double costPerMinute = 0.0;
if(zone.equals(CallZone.CANADA)){
costPerMinute =
costPerMinuteCan;
}
if
(zone.equals(CallZone.USA)){
costPerMinute =
costPerMinuteUsa;
}
return costPerMinute;
}
@Override
public void deductWeeklyFee() {
balance = balance-weeklyFee;
if(weeklyFee < balance){
balance =
balance-weeklyFee;
}
else{
balance =
0.00;
}
}
}
/////////////////////////
//CallZone.java
public enum CallZone
{ CANADA, USA, EUROPE, ASIA, AUSTRALIANZ, LATINAMERICA, AFRICA;
public static boolean isValidZone(String
zone)
{ if(CANADA.toString().equals(zone) ||
USA.toString().equals(zone) ||
EUROPE.toString().equals(zone) ||
ASIA.toString().equals(zone) ||
AUSTRALIANZ.toString().equals(zone) ||
LATINAMERICA.toString().equals(zone) ||
AFRICA.toString().equals(zone))
{ return true;
}
else
{ return false;
}
}
public static CallZone convertToZone(String
zone)
{ if(CANADA.toString().equals(zone))
{ return CANADA;
}
else if(USA.toString().equals(zone))
{ return USA;
}
else if(EUROPE.toString().equals(zone))
{ return EUROPE;
}
else if(ASIA.toString().equals(zone))
{ return ASIA;
}
else if(AUSTRALIANZ.toString().equals(zone))
{ return AUSTRALIANZ;
}
else if(LATINAMERICA.toString().equals(zone))
{ return LATINAMERICA;
}
else
{ assert AFRICA.toString().equals(zone);
return AFRICA;
}
}
}
//////////////////////////
//Global10Card.java
public class Global10Card extends PhoneCard {
protected final double costPerMinuteCan =
0.07;
protected final double costPerMinuteUsa = 0.15;
protected final double costPerMinuteEuro = 0.30;
protected final double costPerMinuteAsia = 0.60;
protected final double costPerMinuteAfrica =
0.60;
protected final double costPerMinuteAusNz =
0.45;
protected final double costPerMinuteLatin =
0.45;
protected final double weeklyFee=1.00;
public Global10Card(long no, int passwd, double
bal) {
super(no, passwd, 10.0);
}
@Override
public boolean allowed(String zone) {
if(zone==CallZone.CANADA.toString()
|| zone==CallZone.USA.toString() ||
zone==CallZone.EUROPE.toString() ||
zone==CallZone.ASIA.toString() ||
zone==CallZone.AFRICA.toString() ||
zone==CallZone.LATINAMERICA.toString()
|| zone==CallZone.AUSTRALIANZ.toString()){
return
true;
}
else {
return
false;
}
}
@Override
public double costPerMin(String zone) {
if(zone.equals(CallZone.CANADA)){
costPerMinute =
costPerMinuteCan;
}
if
(zone.equals(CallZone.USA)){
costPerMinute =
costPerMinuteUsa;
}
if
(zone.equals(CallZone.EUROPE)){
costPerMinute=costPerMinuteEuro;
}
if
(zone.equals(CallZone.ASIA)){
costPerMinute =
costPerMinuteAsia;
}
if
(zone.equals(CallZone.AFRICA)){
costPerMinute =
costPerMinuteAfrica;
}
if
(zone.equals(CallZone.LATINAMERICA)){
costPerMinute =
costPerMinuteLatin;
}
if
(zone.equals(CallZone.AUSTRALIANZ)){
costPerMinute =
costPerMinuteAusNz;
}
return costPerMinute;
}
@Override
public void deductWeeklyFee() {
balance = balance-weeklyFee;
if(weeklyFee < balance){
balance =
balance-weeklyFee;
}
else{
balance =
0.00;
}
}
}
////////////////////////
Screenshot:
------------------------------------------------------------------
Let me know if you need more help on this.
Hope this helps.