6 Replies - 261 Views - Last Post: 28 January 2018 - 02:06 PM Rate Topic: -----

#1 KittyPurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-January 18

Overloaded Method Simplification

Posted 26 January 2018 - 06:43 PM

Hello everyone,

I have created a program (below) that accepts a user-defined number of input values (1 to 4) and uses these to compute a total price after quantity, sales tax, coupon, and discount values are applied.

If user chooses to input one parameter - it is just the price of the book and all other values run from default

If user chooses to input two parameters - it is the price of the book and quantity and all other values run from default

If user chooses to input three parameters - it is the price of the book, quantity, and coupon value and other value runs from default

If user chooses to input four parameters - it is the price of the book, quantity, coupon value, and weekly discount value (no defaults used)

So the program works (exceptions are not handled but that is a battle for another day I expect) and math appears to check out, however, I would like to find a way to simplify the computeBill() methods. It seems to me that I should be able to reference the prior input computeBill() methods when stating additional computeBill() methods. Any help on how to make this work, on other ways in which I can simply this program, OR any other advice would be greatly appreciated.

//Jake Ihlenfeldt
/* This program will accept user input of 1, 2, 3, or 4 parameters for computing the 
 * total cost of the purchase at a photo bookstore*/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Billing
  
{
    //define variables
    static double bookCost = 1, bookCoupon = 1, weeklyDiscount = 1, salesTax = 1.085, bookQuantity = 1, totalCost;
    static int parameterCount;
    static DecimalFormat  df = new DecimalFormat( "00.00" );
    
    //Four parameter input method
    public double computeBill(double cost, double quantity, double coupon, double discount)
    {
      
      bookCost = cost;
      bookQuantity = quantity;
      bookCoupon = coupon;
      weeklyDiscount = discount;
      
    //Compute total
    totalCost = ((((bookCost * bookQuantity) * bookCoupon) * weeklyDiscount) * salesTax);
     
    return totalCost;
     
    }
    
    //Three parameter input method
    public double computeBill(double cost, double quantity, double coupon)
    {
      
      bookCost = cost;
      bookQuantity = quantity;
      bookCoupon = coupon;
      
     //Compute total
     totalCost = ((((bookCost * bookQuantity) * bookCoupon) * weeklyDiscount) * salesTax);
     
     return totalCost;
     
    }
    
    //Two parameter input method
    public double computeBill(double cost, double quantity)
    {
      
      bookCost = cost;
      bookQuantity = quantity;
      
     //Compute total
     totalCost = ((((bookCost * bookQuantity) * bookCoupon) * weeklyDiscount) * salesTax);
     
     
     return totalCost;
     
    }
    
    //One parameter input method
    public double computeBill(double cost)
    {
      
      bookCost = cost;
     
     //Compute total
     totalCost = ((((bookCost * bookQuantity) * bookCoupon) * weeklyDiscount) * salesTax);
     
     
     return totalCost;
      
    }
    
    //gather user input book price
    public double userBookPriceInput()
      
    {
      
     String bookPriceString;
      
     bookPriceString = JOptionPane.showInputDialog(null,
                                  "What is the price of the book?",
                                  "Book Price",
                                  JOptionPane.QUESTION_MESSAGE);
     
     bookCost = (Double.parseDouble(bookPriceString));
      
     return bookCost;
      
    }
    
    //gather user input quantity of books
    public double userBookQuantityInput()
      
    {
      
     String bookQuantityString;
      
     bookQuantityString = JOptionPane.showInputDialog(null,
                                  "How many books?",
                                  "Book Quantity",
                                  JOptionPane.QUESTION_MESSAGE);
     
     bookQuantity = (Double.parseDouble(bookQuantityString));
      
     return bookQuantity;
     
    }
    
    //gather user input value of coupon
    public double userCouponValueInput()
      
    {
      
     String couponValueString;
      
     couponValueString = JOptionPane.showInputDialog(null,
                                  "What is the value of your coupon? (For 10% off enter .9 / for 25% off enter .75)",
                                  "Coupon Value",
                                  JOptionPane.QUESTION_MESSAGE);
     
     bookCoupon = (Double.parseDouble(couponValueString));
      
     return bookCoupon;
     
    }
    
    //gather user input value of weekly discount
    public double userWeeklyDiscountInput()
      
    {
      
     String weeklyDiscountString;
      
     weeklyDiscountString = JOptionPane.showInputDialog(null,
                                  "What is the value of the weekly discount? (For 10% off enter .9 / for 25% off enter .75)",
                                  "Weekly Discount Value",
                                  JOptionPane.QUESTION_MESSAGE);
     
     weeklyDiscount = (Double.parseDouble(weeklyDiscountString));
      
     return weeklyDiscount;
     
    }
    
    //main program method
     public static void main(String[] args)
    {
       
      int userDecision = 0;
      String[] options = new String[] {"1", "2", "3", "4"};
      String[] repeatOptions = new String[] {"For Sure", "No way, bud"};
      
       while (userDecision == 0)
       {
      
      //gather user input on number of parameters
      parameterCount = JOptionPane.showOptionDialog(null,
                                   "How many parameters would you like to input?",
                                   "Parameter Count",
                                   JOptionPane.DEFAULT_OPTION,
                                   JOptionPane.QUESTION_MESSAGE,
                                   null,
                                   options,
                                   options[0]);
        
        //create user input instances
        Billing bookPrice = new Billing();
        Billing bookQuantities = new Billing();
        Billing bookCoupVal = new Billing();
        Billing bookWeeklyDiscount = new Billing();
       
       //if statement for single parameter option
       if (parameterCount == 0)
       {
      
       bookPrice.userBookPriceInput();
         
       Billing one = new Billing();
       one.computeBill(bookCost);
       
       }
       
       //if statement for double parameter option
       if (parameterCount == 1)
       {
         
       bookPrice.userBookPriceInput();
       bookQuantities.userBookQuantityInput();
         
       Billing two = new Billing();
       two.computeBill(bookCost, bookQuantity);
       
       }
       
       //if statement for triple parameter option
       if (parameterCount == 2)
       {  
         
       bookPrice.userBookPriceInput();
       bookQuantities.userBookQuantityInput();
       bookCoupVal.userCouponValueInput();
       
       Billing three = new Billing();
       three.computeBill(bookCost, bookQuantity, bookCoupon);
       
       }
       
       //if statement for quadrupal parameter option
       if (parameterCount == 3)
       {  
         
       bookPrice.userBookPriceInput();
       bookQuantities.userBookQuantityInput();
       bookCoupVal.userCouponValueInput();
       bookWeeklyDiscount.userWeeklyDiscountInput();
       
       Billing four = new Billing();
       four.computeBill(bookCost, bookQuantity, bookCoupon, weeklyDiscount);
       
       }
       
       
      //inform user of calculated total
      JOptionPane.showMessageDialog(null,
                                  "The total cost of the book is " + "$" + df.format(totalCost) + ".",
                                  "Calculated Total Display",
                                  JOptionPane.INFORMATION_MESSAGE);
      
      //request user input decision on whether to repeat program execution
      userDecision = JOptionPane.showOptionDialog(null,
                                   "Would you like to try again?",
                                   "Repeat?",
                                   JOptionPane.YES_NO_OPTION,
                                   JOptionPane.QUESTION_MESSAGE,
                                   null,
                                   repeatOptions,
                                   options[0]);
      
    }
    
   
    }
}
  
       
      



Is This A Good Question/Topic? 0
  • +

Replies To: Overloaded Method Simplification

#2 Heathersmithx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 07-November 16

Re: Overloaded Method Simplification

Posted 26 January 2018 - 11:16 PM

I don't know if there is a way to do what you are saying.
But what I was thinking when I was looking at this is
why don't you just create different methods, such as

public double cost(double cost, double quantity){
return total = cost*quantity;
}

public double discountsORcoupons(double discount, double coupon){
//math goes here
//returns cost after discounts or coupons. 
}

public double addTaxes(){
//math goes here
//returns cost after taxes
}


Also I noticed that you didn't set a new object declaration method.... in order to save private variable information in your billing class....

such as
private double price;
private int quantity;

public Billing(double price, int quantity)
{
this.price = price;
this.quantity = quantity;
}
//or maybe just do an empty constructor so you can just call the methods to set the 
//private data fields

public Billing(){
}

Billing customer = new Billing();
customer.setPrice(2.00);
customer.setQuantity(4);
customer.setDiscount(.10);
new double total = customer.getTotal();




Just some ideas I was thinking.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7063
  • View blog
  • Posts: 14,736
  • Joined: 16-October 07

Re: Overloaded Method Simplification

Posted 27 January 2018 - 07:18 AM

Try to do object oriented programming or not: try to do both, you get squished, just like grape.

Your odd fusion of static and instances is not only confusing me, it's confusing you. Here, your only real instance should be Billing.

This is your code, I just refactored it with extreme prejudiced:
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Billing {

    // perhaps the only static var you need
    private final static DecimalFormat df = new DecimalFormat("00.00");
    
    //not sure you need any of these
    // static double bookCost = 1, bookCoupon = 1, weeklyDiscount = 1, salesTax = 1.085, bookQuantity = 1, totalCost;
    // static int parameterCount;
    // hmm, looks like you need salesTax
    private final double SALES_TAX = 1.085;
    
    
    

    //Four parameter input method
    // no static
    private double computeBill(double cost, double quantity, double coupon, double discount) {
        // no
        // bookCost = cost;
        // bookQuantity = quantity;
        // bookCoupon = coupon;
        // weeklyDiscount = discount;

        //Compute total
        return ((((cost * quantity) * coupon) * discount) * SALES_TAX);
    }
    
    // let's start layering those defaults
    private double computeBill(double cost, double quantity, double coupon) {
        return computeBill(cost, quantity, coupon, 1);
    }
    private double computeBill(double cost, double quantity) {
        return computeBill(cost, quantity, 1);
    }
    private double computeBill(double cost) {
        return computeBill(cost, 1);
    }
    
    // now we're ready to do some work
    
    // you do this a lot, time for a helper
    private double getDouble(String msg, String title) {
        String s = JOptionPane.showInputDialog(null, msg,
            title,
            JOptionPane.QUESTION_MESSAGE);

        return (Double.parseDouble(s));
    }

    //gather user input book price
    // public double userBookPriceInput() {
    private double getUserBookCost() {
        return getDouble(
            "What is the price of the book?",
            "Book Price");
    }

    //gather user input quantity of books
    private double getUserBookQuantity() {
    // public double userBookQuantityInput() {
        return getDouble(
            "How many books?",
            "Book Quantity");
    }

    //gather user input value of coupon
    // public double userCouponValueInput() {
    private double getUserBookCoupon() {
        return getDouble(
            "What is the value of your coupon? (For 10% off enter .9 / for 25% off enter .75)",
            "Coupon Value");
    }

    //gather user input value of weekly discount
    // public double userWeeklyDiscountInput() {
    private double getUserWeeklyDiscount() {
        return getDouble(
                "What is the value of the weekly discount? (For 10% off enter .9 / for 25% off enter .75)",
                "Weekly Discount Value");
    }
    
    private int getUserDecision() {
        String[] options = new String[]{"1", "2", "3", "4"};
        return JOptionPane.showOptionDialog(null,
            "How many parameters would you like to input?",
            "Parameter Count",
            JOptionPane.DEFAULT_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[0]);
    }
    
    private void showCost(double totalCost) {
        JOptionPane.showMessageDialog(null,
            "The total cost of the book is " + "$" + df.format(totalCost) + ".",
            "Calculated Total Display",
            JOptionPane.INFORMATION_MESSAGE);
    }
    
    private boolean tryAgain() {
        String[] repeatOptions = new String[]{"For Sure", "No way, bud"};
        return 0 == JOptionPane.showOptionDialog(null,
                    "Would you like to try again?",
                    "Repeat?",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    repeatOptions,
                    // wrong options[0]);
                    repeatOptions[0]);
        
    }
    
    public void run() {
        boolean running = true;
        while (running) {
            final int parameterCount = getUserDecision();

            //create user input instances
            // wtf, no, bad
            // Billing bookPrice = new Billing();
            // Billing bookQuantities = new Billing();
            // Billing bookCoupVal = new Billing();
            // Billing bookWeeklyDiscount = new Billing();

            //if statement for single parameter option
            if (parameterCount == 0) {
                showCost(computeBill(getUserBookCost()));
            } else if (parameterCount == 1) { // important, else if, don't check twice
                showCost(computeBill(getUserBookCost(), getUserBookQuantity()));
            } else if (parameterCount == 2) {
                showCost(computeBill(
                        getUserBookCost(), 
                        getUserBookQuantity(),
                        getUserBookCoupon()));
            } else if (parameterCount == 3) {
                showCost(computeBill(
                        getUserBookCost(), 
                        getUserBookQuantity(),
                        getUserBookCoupon(),
                        getUserWeeklyDiscount()));
            } else {
                JOptionPane.showMessageDialog(null,
                        "Something went wrong, we shouldn't be here",
                        "Oops",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            running = tryAgain();
        }
        
    }

    //main program method
    public static void main(String[] args) {
        Billing app = new Billing();
        app.run();

    }
}



Now, if you really wanted some kind of object instance thing, having a Book lends itself to that. Maybe something like:
class Book {
    public final double SALES_TAX = 1.085;
    public final double cost, quantity, coupon, discount;
    public Book(double cost, double quantity, double coupon, double discount) {
        this.cost = cost;
        this.quantity = quantity;
        this.coupon = coupon;
        this.discount = discount;
    }
    public double getTotalCost() {
        return cost * quantity * coupon * discount * SALES_TAX;
    }
}



Now your code could generate a Book and you could have something in Billing like showBook(Book book).

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 KittyPurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-January 18

Re: Overloaded Method Simplification

Posted 27 January 2018 - 03:13 PM

Wow, talk about an overhaul.

Thanks for all the input baavgai.

Made all of the changes you suggested and added a few I had been working on since posting the code before. There is still some work that could be done to make the program a bit more versatile and user-friendly, however, I'm happy with it for now.

//Jake Ihlenfeldt
/* This program will accept user input of 1, 2, 3, or 4 parameters for computing the 
 * total cost of the purchase at a photo bookstore*/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;


public class Billing
  
{
    //define variables
    private final static double salesTax = 1.085;
    private final static DecimalFormat  df = new DecimalFormat( "00.00" );
    
    
    //Four parameter input method
    private double computeBill(double cost, double quantity, double coupon, double discount)
    {
    //Compute total
    return ((((cost * quantity) * coupon) * discount) * salesTax);
    }
    
    //Three parameter input method
    private double computeBill(double cost, double quantity, double coupon)
    {
     return computeBill(cost, quantity, coupon, 1);
    }
    
    //Two parameter input method
    private double computeBill(double cost, double quantity)
    {
     return computeBill(cost, quantity, 1);
    }
    
    //One parameter input method
    private double computeBill(double cost)
    {
     return computeBill(cost, 1);
    }
    
    
    //gather user input(JOptionpane)
    private double getDouble(String msg, String title)
    {
       
     String s = JOptionPane.showInputDialog(null,
                                  msg,
                                  title,
                                  JOptionPane.QUESTION_MESSAGE);
      double x = 0;
     
     try
     { 
       x = Double.parseDouble(s);
       
     }
     
     catch(NumberFormatException e)
     {
       JOptionPane.showMessageDialog(null, "Rechoose your parameter quanitity and then, when prompted, enter only numbers into all subsequent prompts",
                                     "Invalid input",
                                     JOptionPane.QUESTION_MESSAGE);
     
       run();
       
     }
     
     return (Double.parseDouble(s));
     
      }
      
    
    
    //gather user input book price
    private double getUserBookCost()
    {
      return getDouble(
                       "What is the price of the book",
                       "Book Price");
    }
    
    //gather user input quantity of books
    private double getUserBookQuantity()
    {
      return getDouble(
                       "How many books?",
                       "Book Quantity");
    }
    
    //gather user input value of coupon
    private double getUserBookCoupon()
    {
      
     return getDouble(
                      "What is the value of your coupon? (For 10% off enter .9 / for 25% off enter .75)",
                      "Coupon value");
     
    }
    
    //gather user input value of weekly discount
    private double userWeeklyDiscountInput()  
    {
      
     return getDouble(
                      "What is the value of the weekly discount? (For 10% off enter .9 / for 25% off enter .75)",
                      "Weekly Discount Value");
    }
    
    //gather user input on number of parameters
    private int getUserDecision()
    {
    String[] options = new String[] {"1", "2", "3", "4"};
    
    return JOptionPane.showOptionDialog(null,
                                   "How many parameters would you like to input?",
                                   "Parameter Count",
                                   JOptionPane.DEFAULT_OPTION,
                                   JOptionPane.QUESTION_MESSAGE,
                                   null,
                                   options,
                                   options[0]);
    }
    
    //inform user of calculated total
    private void showCost(double totalCost)
    {
      
      JOptionPane.showMessageDialog(null,
                                  "The total cost of the book is " + "$" + df.format(totalCost) + ".",
                                  "Calculated Total Display",
                                  JOptionPane.INFORMATION_MESSAGE);
    }
    
    //request user input decision on whether to repeat program execution
    private boolean tryAgain()
    {
      String[] repeatOptions = new String[]{"For Sure", "No way, bud"};
      
      return 0 == JOptionPane.showOptionDialog(null,
                                   "Would you like to try again?",
                                   "Repeat?",
                                   JOptionPane.YES_NO_OPTION,
                                   JOptionPane.QUESTION_MESSAGE,
                                   null,
                                   repeatOptions,
                                   repeatOptions[0]);
}
    private void informUser()
    {
      
      String[] informText= new String[]{"This program is used to calculate the total cost of a book given a number of parameters (price, quantity, coupon value, and discount value"};
      
       JOptionPane.showMessageDialog(null,
                                               informText,
                                               "Inform User",
                                               JOptionPane.INFORMATION_MESSAGE);
                                               
    }                                       
    
    public void run()
    {
      
      boolean running = true;
      
      while (running)
      {
       
        final int parameterCount = getUserDecision();
        
       
       
       //if statement for single parameter option
       if (parameterCount == 0)
       {
         
         
      showCost(computeBill(getUserBookCost()));
       }
 
       //if statement for double parameter option
       else if (parameterCount == 1)
       {
       
         showCost(computeBill(getUserBookCost(),
                              getUserBookQuantity()));
       }
       
       //if statement for triple parameter option
       else if (parameterCount == 2)
       {  
         
         showCost(computeBill(getUserBookCost(),
                              getUserBookQuantity(),
                              getUserBookCoupon()));
       }
       
       //if statement for triple parameter option
       else if (parameterCount == 3)
       {
         
         showCost(computeBill(getUserBookCost(), 
                              getUserBookQuantity(), 
                              getUserBookCoupon(),
                              userWeeklyDiscountInput()));
       }
       //if statement for any other, illegal, user input  
       else
       {
         
        JOptionPane.showMessageDialog(null,
                                       "Something went wrong",
                                       "Oops",
                                       JOptionPane.INFORMATION_MESSAGE);
       }
       
       running = tryAgain();
       
       
      }
      
    }
         
    //main program method
     public static void main(String[] args)
    {
   Billing inform = new Billing();
   inform.informUser();
   
   Billing app = new Billing();
   app.run();
   
    }
}
  
       
      


Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7063
  • View blog
  • Posts: 14,736
  • Joined: 16-October 07

Re: Overloaded Method Simplification

Posted 27 January 2018 - 03:38 PM

Nice idea, using the try/catch in getDouble. However, calling run() on the exception is all kinds of bad: you're starting over and invoking recursion without need. Instead, figure out how to stay getDouble until you get a double. You should be using a loop. Give it shot.
Was This Post Helpful? 0
  • +
  • -

#6 KittyPurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-January 18

Re: Overloaded Method Simplification

Posted 28 January 2018 - 01:27 PM

@ baavgai: Challenge accepted.

It took me an embarrassing amount of time to get all the bugs out of the code I added to obtain a durable program, yet, success has been achieved.

I doubt I will put much more time into this particular program but I welcome comments on what might benefit from improvement for future reference.

//Jake Ihlenfeldt
/* This program will accept user input of 1, 2, 3, or 4 parameters for computing the 
 * total cost of the purchase at a photo bookstore*/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;


public class Billing
  
{
    //define variables
    private final static double salesTax = 1.085;
    private final static DecimalFormat  df = new DecimalFormat( "00.00" );
    
    
    //Four parameter input method
    private double computeBill(double cost, double quantity, double coupon, double discount)
    {
    //Compute total
    return ((((cost * quantity) * coupon) * discount) * salesTax);
    }
    
    //Three parameter input method
    private double computeBill(double cost, double quantity, double coupon)
    {
     return computeBill(cost, quantity, coupon, 1);
    }
    
    //Two parameter input method
    private double computeBill(double cost, double quantity)
    {
     return computeBill(cost, quantity, 1);
    }
    
    //One parameter input method
    private double computeBill(double cost)
    {
     return computeBill(cost, 1);
    }
    
    
    //gather user input(JOptionpane)
    private double getDouble(String msg, String title)
    {
     
      int loopTest = 0;
      String s ="whatever";
      
      while(loopTest == 0)
      {
      
      s = JOptionPane.showInputDialog(null,
                                  msg,
                                  title,
                                  JOptionPane.QUESTION_MESSAGE);
      
      double x = 0;
     
     try
     { 
       
       x = Double.parseDouble(s);
       loopTest++;
     }
     
     catch(NumberFormatException e)
     {
       
      loopTest = inputTest();
     }
     
      }
      
      return (Double.parseDouble(s));
    }
      
    private int inputTest()
    {
      
      String[] stay = new String[]{"Alrighty", "Nah, I'm Out"};
                          
    return   JOptionPane.showOptionDialog(null, "Rechoose your parameter quanitity and then, when prompted, enter only numbers into all subsequent prompts",
                                     "Invalid input",
                                     JOptionPane.YES_NO_OPTION,
                                     JOptionPane.ERROR_MESSAGE,
                                     null,
                                     stay,
                                     stay[0]);
    }
    
    
    //gather user input book price
    private double getUserBookCost()
    {
      return getDouble(
                       "What is the price of the book",
                       "Book Price");
    }
    
    //gather user input quantity of books
    private double getUserBookQuantity()
    {
      return getDouble(
                       "How many books?",
                       "Book Quantity");
    }
    
    //gather user input value of coupon
    private double getUserBookCoupon()
    {
      
     return getDouble(
                      "What is the value of your coupon? (For 10% off enter .9 / for 25% off enter .75)",
                      "Coupon value");
     
    }
    
    //gather user input value of weekly discount
    private double userWeeklyDiscountInput()  
    {
      
     return getDouble(
                      "What is the value of the weekly discount? (For 10% off enter .9 / for 25% off enter .75)",
                      "Weekly Discount Value");
    }
    
    //gather user input on number of parameters
    private int getUserDecision()
    {
    String[] options = new String[] {"1", "2", "3", "4"};
    
    return JOptionPane.showOptionDialog(null,
                                   "How many parameters would you like to input?",
                                   "Parameter Count",
                                   JOptionPane.DEFAULT_OPTION,
                                   JOptionPane.QUESTION_MESSAGE,
                                   null,
                                   options,
                                   options[0]);
    }
    
    //inform user of calculated total
    private void showCost(double totalCost)
    {
      
      JOptionPane.showMessageDialog(null,
                                  "The total cost of the book is " + "$" + df.format(totalCost) + ".",
                                  "Calculated Total Display",
                                  JOptionPane.INFORMATION_MESSAGE);
    }
    
    //request user input decision on whether to repeat program execution
    private boolean tryAgain()
    {
      String[] repeatOptions = new String[]{"For Sure", "No way, bud"};
      
      return 0 == JOptionPane.showOptionDialog(null,
                                   "Would you like to try again?",
                                   "Repeat?",
                                   JOptionPane.YES_NO_OPTION,
                                   JOptionPane.QUESTION_MESSAGE,
                                   null,
                                   repeatOptions,
                                   repeatOptions[0]);
}
    private void informUser()
    {
      
      String[] informText= new String[]{"This program is used to calculate the total cost of a book given a number of parameters (price, quantity, coupon value, and discount value)."};
      
       JOptionPane.showMessageDialog(null,
                                               informText,
                                               "Inform User",
                                               JOptionPane.INFORMATION_MESSAGE);
                                               
    }                                       
    
    public void run()
    {
      
      boolean running = true;
      
      while (running)
      {
       
        final int parameterCount = getUserDecision();
        
       
       
       //if statement for single parameter option
       if (parameterCount == 0)
       {
         
         
      showCost(computeBill(getUserBookCost()));
       }
 
       //if statement for double parameter option
       else if (parameterCount == 1)
       {
       
         showCost(computeBill(getUserBookCost(),
                              getUserBookQuantity()));
       }
       
       //if statement for triple parameter option
       else if (parameterCount == 2)
       {  
         
         showCost(computeBill(getUserBookCost(),
                              getUserBookQuantity(),
                              getUserBookCoupon()));
       }
       
       //if statement for triple parameter option
       else if (parameterCount == 3)
       {
         
         showCost(computeBill(getUserBookCost(), 
                              getUserBookQuantity(), 
                              getUserBookCoupon(),
                              userWeeklyDiscountInput()));
       }
       //if statement for any other, illegal, user input  
       else
       {
         
        JOptionPane.showMessageDialog(null,
                                       "Something went wrong",
                                       "Oops",
                                       JOptionPane.INFORMATION_MESSAGE);
       }
       
       running = tryAgain();
      
      }
      
    }
         
    //main program method
     public static void main(String[] args)
    {
   Billing inform = new Billing();
   inform.informUser();
   
   Billing app = new Billing();
   app.run();
   
    }
}  
      


Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7063
  • View blog
  • Posts: 14,736
  • Joined: 16-October 07

Re: Overloaded Method Simplification

Posted 28 January 2018 - 02:06 PM

Nice. Though you seem to be storing more than you need.

Here's the way I'd attack it:
private double getDouble(String msg, String title) {
    // why isn't this a bool?
    // in any case, you don't need it
    // int loopTest = 0;
    // nor this, you certainly don't need this dummy out here
    // String s = "whatever";

    // we're just going to loop forever
    // the body will have to escape on it's own
    while (true) {
        String s = JOptionPane.showInputDialog(null,
                msg,
                title,
                JOptionPane.QUESTION_MESSAGE);
        // why here double x = 0;
        try {
            // never mind, we don't need a var, really
            // double x = Double.parseDouble(s);
            return Double.parseDouble(s);
        } catch (NumberFormatException e) {
            // if we get here, we have to go again
            // why do we even care about the result from this?
            inputTest();
        }
    }
}



Alternately, some folks really, really like to return at the end of a method, so:
private double getDouble(String msg, String title) {
    // this is a bit of a trick
    // the Double class can be null and degrades to double
    java.lang.Double result = null;
    while (result==null) {
        try {
            result = Double.parseDouble(
                    JOptionPane.showInputDialog(null,
                            msg,
                            title,
                            JOptionPane.QUESTION_MESSAGE)
            );
        } catch (NumberFormatException e) {
            inputTest();
        }
    }
    return result;
}



Sounds like you're fed up with this program by now. That's fair. But you can always learn something by tweaking a little more. Consider looking back at your program in a few months and seeing if there is anything you would do differently.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1