dr. loop
dr. loop

Reputation: 3

how can I compare two values in inheritance in java?

I am trying to create a program that checks which account have lowest cost, but I don't know how to implement this in my program.

I want to select the output of each class separately to compare (dayTimeCost)

This is what I think it may be implemted but I don't know how

if (BronceDayTimeCost < SilverDayTimeCost){ System.out.print("\nThe Bronze Account is cheapest.");}

if (SilverDayTimeCost< BronceDayTimeCost ){ System.out.print("\nThe Silver Account is cheapest.");}

Bronze Class

    public  class Bronze  {

    // ----------------- Atributes -----------------------

    

    public int dayMinutes;      // daytime telphone minutes used

    public double dayTimeCost;  //Total daytime calls cost

    // ------------- CONSTRUCTORS (inputs) ---------------

    public Bronze(int theDayMinutes ) {  
    dayMinutes = theDayMinutes; 
    }
    // ------------------ METHODS ------------------------
    // Calculate Total daytime calls cost
    public double calcDayTimeCost(double costDay) {

        dayTimeCost = dayMinutes * costDay;
        System.out.print("\nCost of daytime calls = "  + costDay + "/min"+
        "\n\nTotal daytime calls cost = " + dayTimeCost + 
        "\n");

        return dayTimeCost;
    }

    //toString method to override that in Object
    public String toString(){
    return(
 "\n" 
         );
    }

    //Returns the type of account
    public String type(){
    return "Bronze";
    }
}

Silver Class

   public class Silver extends Bronze {

    private static final double costDay = 0.22;

    public Silver(int theDayMinutes ) {
    super(theDayMinutes );

  }
    //Returns the type of account
    public String type(){
    return "Silver";
    }

}

Main Class

    import java.util.Scanner;

public class AccountUser {

    // ------------------- FIELDS ------------------------    

    // Create instance of Scanner class
    public static Scanner input = new Scanner(System.in);
    // variables
    public static Bronze bron;
    public static Silver silv;

    public static int dayMinutes;

    // ------------------ METHODS ------------------------  

    public static void main(String [] args) {

        // Input dayMinutes (with error message)
        do{
        System.out.print("Please daytime telphone minutes used --> ");
        dayMinutes  = input.nextInt();
        if  ( dayMinutes <= 0){System.out.print("\n" + "Input value outside the range!!!" + "\n");}
        }while( dayMinutes <= 0);


        // Create new Bronze instance
        bron = new Bronze(dayMinutes);
        silv = new Silver(dayMinutes);
        // Calculate scheme1, scheme2
        bron.calcDayTimeCost(0.12);
        silv.calcDayTimeCost(0.22);


    System.out.println(bron);
    System.out.println(silv);
    }
}

Upvotes: 0

Views: 143

Answers (1)

Vasu
Vasu

Reputation: 22432

You can collect the return type of calcDayTimeCost method into a double variable as shown below and then check which cost is lower using an if and else conditions.

double bronCost = bron.calcDayTimeCost(0.12);
double silverCost = silv.calcDayTimeCost(0.22);
if(bronCost == silverCost) {
    System.out.print("\nThe Silver and Bronze Accounts are same cost ");
} else if (bronCost < silverCost){ 
     System.out.print("\nThe Bronze Account is cheapest.");
} else {
     System.out.print("\nThe Silver Account is cheapest.");
}

Although your code works, it is not inline with Object Oriented Programming i.e., in general, we create classes inline with the real world objects, but Bronze and Silver two different elements i.e., they don't directly relate/inherit each other.

In other words, when you say ClassA extends ClassB, you are saying that ClassA IS-A ClassB

So, the alternative approach to this is as follows:

  public abstract class Metal {

        //add fields applicable for all metals  

        public double calcDayTimeCost() {
             //add code here
        }
    }

  public class Bronze extends Metal {

       //Bronze specific details
  }

  public class Silver extends Metal {

       //Silver specific details
  }

Upvotes: 1

Related Questions