0

This is the Registration Class stored as Registration.java.

public class Registration {
    int registrationId;
    double fees;
    int marks;
public void setRegistrationId(int registrationId){
    this.registrationId=registrationId;
}
public int getRegistrationId(){
    return registrationId;
}
public void setFees(double fees){
    this.fees=fees;
}
public double getFees(){
    return fees;
}
public void calculateFees(int marks){
    int discount=0;
    if(marks>=85 && marks<=100)
        discount = 12;
    else if(marks>=75 && marks<=84)
        discount = 7;
    else if(marks>=65 && marks<=74)
        discount = 0;
    else
        System.out.println("You have not passed!");
    fees = fees-(fees*(discount/100.0));
    System.out.println("The discount availed is "+discount+" % and the fees after discount is: "+fees);
}
}

Another class called DemoReg1.java which hosts the starter method.

public class DemoReg1 {

  public static void main(String[] args) {
    int branchList[]={1001,1002,1003,1004,1005};
    double fees[]= {25575,15500,33750,8350,20500};      
    Registration reg = new Registration();
    reg.setRegistrationId(2001);
    int branchId=1002;      
    double fees1=0;
    for(int i=0;i<branchList.length;i++)
    {
        if(branchList[i]==branchId)
        { 
            fees1=fees[i];
            reg.setFees(fees1);
            System.out.println("The Registration ID is:"+reg.getRegistrationId());
            System.out.println("The fees before discount is:"+reg.getFees());
            reg.calculateFees(79);
            break;
        } 
       }
      }
    }

The output which I get is:

The Registration ID is:2001
The fees before discount is:15500.0
The discount availed is 7 % and the fees after discount is: 14415.0

I want to add an 'else' condition which prints out the following statement when the branchId provided(local variable) is say 1006.

System.out.println("Invalid branch ID");

Where in the code should I add the above statement? Thanks!

3 Answers 3

6

Introduce a boolean variable that is set to true if you found the branch, and check the value of this boolean variable after the loop:

boolean branchFound = false;
for(int i=0;i<branchList.length;i++)
{
    if(branchList[i]==branchId)
    { 
        branchFound = true;
        fees1=fees[i];
        reg.setFees(fees1);
        System.out.println("The Registration ID is:"+reg.getRegistrationId());
        System.out.println("The fees before discount is:"+reg.getFees());
        reg.calculateFees(79);
        break;
    } 
   }
  }
}
if (!branchFound) {
    System.out.println("Invalid branch ID");
}

You should probably introduce a class (BranchIdAndFee) that holds the branchId and the associated fee, instead of using parallel arrays. And you should also use a Map<Integer, BranchIdAndFee> to be able to get the information from a branch ID in constant time rather than having to loop.

Sign up to request clarification or add additional context in comments.

Comments

3

Instead of using an else clause, simply set a boolean flag, and check it's value after the loop. If it is true, then you know that a valid branch was found. No need for an error message. Otherwise, you've got to output the error message.

boolean isValidBranch = false;
// Create a flag.    

for(int i=0;i<branchList.length;i++)
{
    if(branchList[i]==branchId)
    { 
        isValidBranch = true;
        fees1=fees[i];
        reg.setFees(fees1);
        System.out.println("The Registration ID is:"+reg.getRegistrationId());
        System.out.println("The fees before discount is:"+reg.getFees());
        reg.calculateFees(79);
        break;
    } 
   }

Then simply check if the flag has been set to true:

 if(!isValidBranch)
 {
     // Print out your error message.
 }

2 Comments

A pleasure! Mark one of the answers as correct please. Looks like his was a little better than mine :)
Yours worked too! So, that great! :-) Thanks for all the help.
0

After the for loop, you can test whether or not the branch ID supplied was valid, for example:

int ii;

for (ii = 0 ; ii < branchList.length; ii++)
{
  if (branchList[ii] == branchId)
  {
    // do your calculations
    break;
  }
}

if (ii == branchList.length)
{
  printf("Error: supplied branch ID %d was invalid\n", branchId);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.