I basically wrote a program for a phone card company, using about four classes. Im not sure what im doing wrong, but my program is not compiling. Most of my errors occur in the fourth file (class superphonecardinc) where the else if(command.equals("getLimit")) begins.
Im thinking that I used too many if, else and else if statements, I could try the for loop. Any help will be apprecaited.
This is the question..., well a part of it
SuperPhoneCard Inc. sells Global25 phone cards which cost $25 each and are good for both domestic and overseas calls. The per minute rates are as follows:
Global25
Canada
$0.05
USA
$0.10
Europe
$0.20
Asia
$0.40
Australia & NZ
$0.30
Latin America
$0.30
Africa
$0.40
initial balance
$25.00
weekly fee
$1.00
This is my program
File one
public enum CallZone // represents the call zones
{ CANADA, USA, EUROPE, ASIA, ANZ, LATINAM, AFRICA;
public static boolean isValidZone(String zone)
{ if(CANADA.toString().equals(zone) ||
USA.toString().equals(zone) ||
EUROPE.toString().equals(zone) ||
ASIA.toString().equals(zone) ||
ANZ.toString().equals(zone) ||
LATINAM.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(ANZ.toString().equals(zone))
{ return ANZ;
}
else if(LATINAM.toString().equals(zone))
{ return LATINAM;
}
else
{ assert AFRICA.toString().equals(zone);
return AFRICA;
}
}
}
File two
public class CardTable // this class manages the table of Phone Cards
{
public CardTable()
{ ct = new PhoneCard[TABLE_LENGTH];
ctSize = 0;
current = 0;
}
public boolean add(PhoneCard card)
{ if(ctSize == TABLE_LENGTH) return false;
if(get(card.getNumber()) !=null) return false;
ct[ctSize] = card;
ctSize++;
return true;
}
public PhoneCard get(long no)
{ for(int i = 0; i < ctSize; ++i) {
if(no == ct[i].getNumber())
return ct[i];
}
return null;
}
public PhoneCard first()
{ if(ctSize == 0)
{ return null;
}
else
{ current = 0;
return ct[current];
}
}
public PhoneCard next()
{ if(current + 1 == ctSize)
{ return null;
}
else
{ current++;
return ct[current];
}
}
private PhoneCard[] ct;
private int ctSize;
private int current;
private static int TABLE_LENGTH = 20;
}
the third file
import java.text.DecimalFormat;
public abstract class PhoneCard{ // this is the main class
long number;
int password;
double balance;
public PhoneCard(long no, int passwd, double bal) { // a constructor (precondition: no and passwd must be positive)
number = no;
password = passwd;
balance = bal;
}
public long getNumber() { //an accessor returning the card number
return number;
}
public int getPassword() {// an accessor returning the card password
return password;
}
public double getBalance() {// an accessor returning the card balance
return balance;
}
public void setBalance(double bal) {// a mutator to set the card balance
balance = bal;
}
public 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
return (int)(getBalance()/costPerMin(zone));
}
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
double cst = costPerMin(zone)*minutes;
double diff = getBalance() - cst;
if (diff >= 0) {
setBalance(diff);
return true;
}else return false;
}
public 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".
DecimalFormat df = new DecimalFormat("0.00");
return "card no " + number + " has a balance of " + df.format(balance);
}
}
The fourth file
import java.util.Scanner;
public class SuperPhoneCardInc
{ // this is the application that SuperPhoneCardInc will use to manage it businesses
public static void main(String[] args)
{
CardTable ct = new CardTable();
Scanner in = new Scanner(System.in);
String line = null;
boolean done = false;
if(!in.hasNextLine())
{
done = true;
}
else
{
line = in.nextLine();
}
if(!done && line.length() >= 4 && line.substring(0,4).equals("quit"))
{
done = true;
}
while(!done)
{
System.out.println("Input: " + line);
Scanner inl = new Scanner(line);
String command = "";
if(inl.hasNext())
{
command = inl.next();
}
if(command.equals("add"))
{
boolean invalidArgs = false;
long no = 0;
int passwd = 0;
if(inl.hasNextLong())
{
no = inl.nextLong();
}
else
{
invalidArgs = true;
}
if(!invalidArgs && inl.hasNextInt())
{
passwd = inl.nextInt();
}
else
{
invalidArgs = true;
}
if(!invalidArgs && inl.hasNext())
{
cardType = inl.next();
}
else
{
invalidArgs = true;
}
if(!invalidArgs && (no <= 0 || passwd <= 0))
{
invalidArgs = true;
}
PhoneCard card = null;
if(!invalidArgs)
{
card = new PhoneCard(no,passwd);
}
else
{
invalidArgs = true;
}
if(invalidArgs)
{
System.out.println("Error: invalid arguments for add command");
}
else if(ct.get(no) != null)
{
System.out.println("Error: card no " + no + " already exists");
}
else if(!ct.add(card))
{
System.out.println("Error: card table full");
}
else
{
System.out.println("Result: added card " + no);
}
}
else if(command.equals("getBalance"))
{
boolean invalidArgs = false;
long no = 0;
int passwd = 0;
if(inl.hasNextLong())
{
no = inl.nextLong();
}
else
{
invalidArgs = true;
}
if(!invalidArgs && inl.hasNextInt())
{
passwd = inl.nextInt();
}
else
{
invalidArgs = true;
}
if(!invalidArgs && (no <= 0 || passwd <= 0))
{
invalidArgs = true;
}
if(invalidArgs)
{
System.out.println("Error: invalid arguments for getBalance command");
}
else
{
PhoneCard card = ct.get(no);
if(card == null)
{
System.out.println("Error: card no " + no + " does not exist");
}
else if(card.getPassword() != passwd)
{
System.out.println("Error: password " + passwd + " incorrect");
}
else
{
System.out.printf("Result: card %d balance is %.2f%n",
no, card.getBalance());
}
}
}
else if(command.equals("getLimit"))
{
boolean invalidArgs = false;
long no = 0;
int passwd = 0;
if(inl.hasNextLong())
{ no = inl.nextLong();
}
else
{ invalidArgs = true;
}
if(!invalidArgs && inl.hasNextInt())
{ passwd = inl.nextInt();
}
else
{ invalidArgs = true;
}
if(!invalidArgs && (no <= 0 || passwd <= 0))
{ invalidArgs = true;
}
String cardZone = null;
if(!invalidArgs && inl.hasNext())
{ cardZone = inl.next();
}
else
{ invalidArgs = true;
}
if(invalidArgs)
{ System.out.println("Error: invalid arguments for getLimit command");
}
else
{
PhoneCard card = ct.get(no);
}
if(card == null)
{ System.out.println("Error: card no " + no + " does not exist");
}
else if(card.getPassword() != passwd)
{ System.out.println("Error: password " + passwd + " incorrect");
}
else if (!CallZone.isValidZone(cardZone))
{
System.out.println("Error: invalid arguments for getLimit command");
}
else if (!card.allowed(cardZone))
{ System.out.println("Error: card " + no + " not allowed for zone " + cardZone);
}
else
{ System.out.println("Result: card " + no + " limit for zone " + cardZone + " is " + card.getLimit(cardZone) + " minutes");
}
}
else if(command.equals("charge"))
{ boolean invalidArgs = false;
long no = 0;
int passwd = 0;
int min = 0;
if(inl.hasNextLong())
{ no = inl.nextLong();
}
else
{ invalidArgs = true;
}
if(!invalidArgs && inl.hasNextInt())
{ passwd = inl.nextInt();
}
else
{ invalidArgs = true;
}
String cardZone = null;
if(!invalidArgs && inl.hasNext())
{ cardZone = inl.next();
}
else
{ invalidArgs = true;
}
if(!invalidArgs && inl.hasNextInt())
{ min = inl.nextInt();
}
else
{ invalidArgs = true;
}
if(!invalidArgs && (no <= 0 || passwd <= 0 || min < 0))
{ invalidArgs = true;
}
if(invalidArgs)
{ System.out.println("Error: invalid arguments for charge command");
}
else
{
PhoneCard card = ct.get(no);
if(card == null)
{ System.out.println("Error: card no " + no + " does not exist");
}
else if(card.getPassword() != passwd)
{ System.out.println("Error: password " + passwd + " incorrect");
}
else if (!CallZone.isValidZone(cardZone))
{
System.out.println("Error: invalid arguments for charge command");
}
else if (!card.allowed(cardZone))
{ System.out.println("Error: card " + no + " not allowed for zone " + cardZone);
}
else if (card.charge(min, cardZone))
{
System.out.println("Result: card " + no + " charged " + df.format(card.costPerMin(cardZone)*min) +", new balance is " + df.format(card.getBalance()));
}
else
{
System.out.println("Error: card " + no + " limit for zone " + cardZone + " is "+ card.getLimit(cardZone) + " minutes");
}
}
}
else if(command.equals("deductWeeklyFee"))
{
PhoneCard card = ct.first();
while(card != null)
{
card.deductWeeklyFee();
System.out.printf("Result: card %d charged weekly fee%n", card.getNumber());
card = ct.next();
}
System.out.println("Result: weekly fees deducted");
}
else if(command.equals("printAll"))
{
PhoneCard card = ct.first();
while(card != null)
{
System.out.printf("Result: %s%n", card);
card = ct.next();
}
System.out.println("Result: all cards printed");
}
else
{
System.out.println("Error: command invalid");
}
if(!in.hasNextLine())
{
done = true;
}
else
{
line = in.nextLine();
}
if(!done && line.length() >= 4 && line.substring(0,4).equals("quit"))
{
done = true;
}
}
}
}