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;
         }
       }
    }
}

Recommended Answers

All 7 Replies

Please post the compiler error messages (including the line numbers).
There's no way you have reached a limit on the number of nested if's etc, so it's not that.

I have fixed the else if statements. I just had to take some time to look over the opening and closing braces. And now theses are the errors I get.

12 errors found:
[line: 56]
Error: cannot find symbol
symbol: variable cardType
location: class SuperPhoneCardInc

[line: 69]
Error: PhoneCard is abstract; cannot be instantiated

line: 177]
Error: cannot find symbol
symbol: variable card
location: class SuperPhoneCardInc

[line: 180]
Error: cannot find symbol
symbol: variable card
location: class SuperPhoneCardInc

[line: 187]
Error: cannot find symbol
symbol: variable card
location: class SuperPhoneCardInc

[line: 191]
Error: cannot find symbol
symbol: variable card
location: class SuperPhoneCardInc

[line: 245]
Error: cannot find symbol
symbol: method allowed(java.lang.String)
location: variable card of type PhoneCard

[line: 248]
Error: method charge in class PhoneCard cannot be applied to given types;
required: int,CallZone
found: int,java.lang.String

[line: 250]
Error: cannot find symbol
symbol: variable df
location: class SuperPhoneCardInc

[line: 250]
Error: method costPerMin in class PhoneCard cannot be applied to given types;
required: CallZone
found: java.lang.String

[line: 250]
Error: cannot find symbol
symbol: variable df
location: class SuperPhoneCardInc

[line: 254]
Error: method getLimit in class PhoneCard cannot be applied to given types;
required: CallZone
found: java.lang.String

Member Avatar for hfx642

Keep in mind that when you define an object inside {}s,
they are NOT available outside of the {}s.

public void Print_This ()
{
   {
      String My_String = "Test String";
   }
   System.out.println (My_String);
}

will fail because the print command can NOT see My_String.

Keep in mind that when you define an object inside {}s,
they are NOT available outside of the {}s.

public void Print_This ()
{
   {
      String My_String = "Test String";
   }
   System.out.println (My_String);
}

will fail because the print command can NOT see My_String.

Thank you. I figured it out. Just needed to have some patience and take my time to review it.

Dude, can you please submit the fixed and working code ?

commented: do your own homework +0
commented: Dude, no -3

do you really think some kid asking to get their homework done for them is going to come back years later and give you their homework they never got done in the first place because nobody did it for them?

<insult snipped>, you don't deserve even a failing grade, you only deserve to be kicked out of school for attempted plagiarism.

Point of order: the original poster seemed to work it out for themselves, with a little help from DaniWeb, in this particular case. I agree about matercado20, although I think the language used is too harsh and out of order, but the OP posted code, asked for help and responded to questions before working out the answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.