Jump to content

Java Question Confusion

tp95112

Currently working on a assignment and I cant seem to understand the coding given. I am to complete/add coding into prewriten .java classes. Here is the question copy pasted/main question. 

<

 

 

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:

implement the classes to support the application. The main class is PhoneCard with the following API:

  • public PhoneCard(long no, int passwd): 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 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 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".

The class 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: adds a card with the given number and password;
  • 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, orAFRICA);
  • charge no passwd callZone minutes: charges card no for a call of minutes to callZone if the password passwd is valid and the balance is sufficient.
  • 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:

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.

  • 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 exist, print "Error: card no no does not exist"; if the card's password is wrong, print "Error: password passwd incorrect";

     

  • 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 the card's balance is not sufficient to cover the call, print "Error: card no limit for zone callZone is XX minutes".

 

 

>

Here is what I have so far PhoneCard

<
import java.text.DecimalFormat;public abstract class PhoneCard { private long number; private int password; private double balance;  	//Constructor	public PhoneCard(long no, int passwd)	{ 		number = no;		password = passwd;					}			//Accessor return card number password balance 	public long getNumber(){		return number;	}		public int getPassword(){		return password;	}	public double getBalance(){		return balance;	}	//Accessor return card number password balance			public void setBalance(double bal){ //mutator to set the card balance		balance = bal;			}	public double costPerMin(CallZone zone){ //returns cost per minute of a call to the argument "zone"			return costPerMin(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		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; 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);		double callCharge = costPerMin(zone)*minutes;		double cost = getBalance() - callCharge;		if (cost>=0){			setBalance(callCharge);			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(){		DecimalFormat formatbalance = new DecimalFormat("#.##");// Format Balance to proper money value		return "card no " + number + "has a balance of " + formatbalance.format(balance);			}} 

>

Superphonecardinc.java
<
import java.util.Scanner;public class SuperPhoneCardInc{   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; //valid arguement	    long no = 0;            int passwd = 0;            	    if(inl.hasNextLong())	    {               no = inl.nextLong();            }            else	    {               invalidArgs = true; //invalid argument	    }	    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"))	 {  // YOU MUST FILL THIS PARTf            // OF THE DEFINITION		 		 		 		 		          }         else if(command.equals("charge"))         {  // YOU MUST FILL THIS PART            // OF THE DEFINITION        	         	         	         	         	         	          }	 else if(command.equals("deductWeeklyFee"))deducts the weekly fee from all cards         {            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"))  // prints the balance of all cards.         {            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;         }      }   }}
>
I have done the PhoneCard and not quiet sure if I did that correctly even though there are no errors. The problem I have is for the second part. I am getting confused with the if statements cause there are alot and I lose track of what means what
Link to comment
Share on other sites

Link to post
Share on other sites

Too many if statements/branching logic = a bad design. Have a think about breaking things up a bit more, it will simplify the issue.

I'm not going to spoon feed you your homework :P

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Too many if statements/branching logic = a bad design. Have a think about breaking things up a bit more, it will simplify the issue.

I'm not going to spoon feed you your homework :P

Wasnt expecting spoon feeding :D I really want to get the logic behind this cause its really mind boggling.. These main is not made by me

Link to comment
Share on other sites

Link to post
Share on other sites

Too many if statements/branching logic = a bad design. Have a think about breaking things up a bit more, it will simplify the issue.

I'm not going to spoon feed you your homework :P

So I worked on it today and taking it slowly and I got the first few if statements down until this one

if(!invalidArgs && inl.hasNext())	    {               cardType = inl.next();            }            else	    {               invalidArgs = true;	    }

Not sure if this means if it means. If they have no Invalidargs and they have the cardType eg Canada or Africa calling card

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×