title: Java method returning incorrect values
body: <p>I'm struggling learning to properly code methods in Java. I've looked at many examples and can't see what I'm doing wrong. I've got a main class called InvoiceApp which uses a class called Validator to screen input and then a class called Invoice which processes the user input. When InvoiceApp is run, the user inputs either 'r' or 'c' as the Customer Type, and a double value for "subtotal". The app creates an object of the Invoice class to use the getInvoice() method, which returns a formatted invoice. 
  The formatted invoice is returned, but with NULL value for the String and ZEROES for all the numbers (except that which the user input in the 1st place). The Invoice class is apparently not assigning values to the variables. I've done this many, many different ways and cannot get the thing to work.</p>
  
  <p>Can anyone see what I've done wrong? Here is my code:</p>
  
  <p>The InvoiceApp class:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class InvoiceApp
  {    
  public static void main(String[] args)
  {
      // display a welcome message
      System.out.println("Welcome to the Invoice Total Calculator");
      System.out.println();  // print a blank line
  
      Scanner sc = new Scanner(System.in);
      String choice = "y";
      while(choice.equalsIgnoreCase("y"))
      {
          // get user entries
          String customerType = Validator.getString(sc,
              "Enter customer type (r/c):   ");
          double subtotal = Validator.getDouble(sc,
              "Enter subtotal:              ", 0, 10000);
  
          Invoice i;
          i = new Invoice(customerType, subtotal);
          System.out.println();
          System.out.println(i.getInvoice());
  
          System.out.println();
          System.out.print("Continue? (y/n): ");
          choice = sc.next();
          System.out.println();
      }
  } 
  }
  </code></pre>
  
  <p>The Invoice class:</p>
  
  <pre><code>import java.text.NumberFormat;
  
  public class Invoice {
  private final String customerType;
  private final double subtotal;
  private double discountAmount;
  private double discountPercent;
  private double invoiceTotal;
  private String sCustomerType;
  
  public Invoice(String customerType, double subtotal){
      this.customerType = customerType;
      this.subtotal = subtotal;
  }
  public void setDiscountPercent(double discountPercent){
      if (customerType.equalsIgnoreCase("r"))
      {
          if (subtotal &gt;= 500)
              discountPercent = .2;
          else if (subtotal &gt;= 250 &amp;&amp; subtotal &lt; 500)
              discountPercent =.15;
          else if (subtotal &gt;= 100 &amp;&amp; subtotal &lt; 250)
              discountPercent =.1;
          else if (subtotal &lt; 100)
              discountPercent =.0;
      }
      else if (customerType.equalsIgnoreCase("c"))
      {
              discountPercent = .2;
      }
      else
      {
          discountPercent = .05;
      }
  }
  public double getDiscountPercent(){
      return discountPercent;
  }
  public void setDiscountAmount(double discountAmount){
      discountAmount = subtotal * (getDiscountPercent());
  }
  public double getDiscountAmount(){
      return discountAmount;
  }
  public void setInvoiceTotal(double invoiceTotal){
      invoiceTotal = subtotal - (getDiscountAmount());
  }
  public double getInvoiceTotal(){
      return invoiceTotal;
  }
  public void setCustomerType(String sCustomerType){
      sCustomerType = "Unknown";
          if (customerType.equalsIgnoreCase("r"))
              sCustomerType = "Retail";
          else if (customerType.equalsIgnoreCase("c"))
              sCustomerType = "College";
  }
  public String getCustomerType(){
      return sCustomerType;
  }
  public String getInvoice(){
      NumberFormat currency = NumberFormat.getCurrencyInstance();
      NumberFormat percent = NumberFormat.getPercentInstance();
      sCustomerType = this.getCustomerType();
      discountPercent = this.getDiscountPercent();
      discountAmount = this.getDiscountAmount();
      invoiceTotal = this.getInvoiceTotal();
      // Create a string representation of the full invoice and return
      String invoice = ("Subtotal:         " + currency.format(subtotal) + "\n"
                     + "Customer type:    " + sCustomerType + "\n"
                     + "Discount percent: " + percent.format(discountPercent)+ "\n"
                     + "Discount amount:  " + currency.format(discountAmount)+ "\n"
                     + "Total:            " + currency.format(invoiceTotal) + "\n");
      return invoice;
  }
  }
  </code></pre>
  
  <p>The Validator class:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Validator
  {
  public static String getString(Scanner sc, String prompt)
  {
      System.out.print(prompt);
      String s = sc.next();  // read user entry
      sc.nextLine();  // discard any other data entered on the line
      return s;
  }
  
  public static int getInt(Scanner sc, String prompt)
  {
      int i = 0;
      boolean isValid = false;
      while (isValid == false)
      {
          System.out.print(prompt);
          if (sc.hasNextInt())
          {
              i = sc.nextInt();
              isValid = true;
          }
          else
          {
              System.out.println("Error! Invalid integer value. Try again.");
          }
          sc.nextLine();  // discard any other data entered on the line
      }
      return i;
  }
  
  public static int getInt(Scanner sc, String prompt,
  int min, int max)
  {
      int i = 0;
      boolean isValid = false;
      while (isValid == false)
      {
          i = getInt(sc, prompt);
          if (i &lt;= min)
              System.out.println(
                  "Error! Number must be greater than " + min + ".");
          else if (i &gt;= max)
              System.out.println(
                  "Error! Number must be less than " + max + ".");
          else
              isValid = true;
      }
      return i;
  }
  
  public static double getDouble(Scanner sc, String prompt)
  {
      double d = 0;
      boolean isValid = false;
      while (isValid == false)
      {
          System.out.print(prompt);
          if (sc.hasNextDouble())
          {
              d = sc.nextDouble();
              isValid = true;
          }
          else
          {
              System.out.println("Error! Invalid decimal value. Try again.");
          }
          sc.nextLine();  // discard any other data entered on the line
      }
      return d;
  }
  
  public static double getDouble(Scanner sc, String prompt,
  double min, double max)
  {
      double d = 0;
      boolean isValid = false;
      while (isValid == false)
      {
          d = getDouble(sc, prompt);
          if (d &lt;= min)
              System.out.println(
                  "Error! Number must be greater than " + min + ".");
          else if (d &gt;= max)
              System.out.println(
                  "Error! Number must be less than " + max + ".");
          else
              isValid = true;
      }
      return d;
  }
  }
  </code></pre>
  
  <p>Thanks to anyone that can help</p>
  
  <hr>
  
  <p>Thank you all. I removed the parameters from the setter methods and added this. to the constructors. Obviously, there are two major areas that I'm confused about:</p>
  
  <ol>
  <li><p>What parameters are supposed to be in the set methods? I tried to insert the variables that the set methods actually <em>use</em>, but that didn't work either.</p></li>
  <li><p>I thought get methods were simply used to return the values generated by the set methods. Since the setters are all void, they can't be directly used. Working examples I've seen are constructed similarly (however they actually work). </p></li>
  </ol>
  
  <p>Here is what I changed in the Invoice class.</p>
  
  <pre><code>    import java.text.NumberFormat;
  
  public class Invoice {
  private final String customerType;
  private final double subtotal;
  private double discountAmount;
  private double discountPercent;
  private double invoiceTotal;
  private String sCustomerType;
  
  public Invoice(String customerType, double subtotal){
      this.customerType = customerType;
      this.subtotal = subtotal;
  }
  public void setDiscountPercent(){
      if (customerType.equalsIgnoreCase("r"))
      {
          if (subtotal &gt;= 500)
              this.discountPercent = .2;
          else if (subtotal &gt;= 250 &amp;&amp; subtotal &lt; 500)
              this.discountPercent =.15;
          else if (subtotal &gt;= 100 &amp;&amp; subtotal &lt; 250)
              this.discountPercent =.1;
          else if (subtotal &lt; 100)
              this.discountPercent =.0;
      }
      else if (customerType.equalsIgnoreCase("c"))
      {
              this.discountPercent = .2;
      }
      else
      {
          this.discountPercent = .05;
      }
  }
  public double getDiscountPercent(){
      return this.discountPercent;
  }
  public void setDiscountAmount(){
      this.discountAmount = subtotal * (getDiscountPercent());
  }
  public double getDiscountAmount(){
      return this.discountAmount;
  }
  public void setInvoiceTotal(){
      this.invoiceTotal = subtotal - (getDiscountAmount());
  }
  public double getInvoiceTotal(){
      return this.invoiceTotal;
  }
  public void setCustomerType(){
      this.sCustomerType = "Unknown";
          if (customerType.equalsIgnoreCase("r"))
              this.sCustomerType = "Retail";
          else if (customerType.equalsIgnoreCase("c"))
              this.sCustomerType = "College";
  }
  public String getCustomerType(){
      return this.sCustomerType;
  }
  public String getInvoice(){
      NumberFormat currency = NumberFormat.getCurrencyInstance();
      NumberFormat percent = NumberFormat.getPercentInstance();
      sCustomerType = this.getCustomerType();
      discountPercent = this.getDiscountPercent();
      discountAmount = this.getDiscountAmount();
      invoiceTotal = this.getInvoiceTotal();
      // Create a string representation of the full invoice and return
      String invoice = ("Subtotal:         " + currency.format(subtotal) + "\n"
                     + "Customer type:    " + sCustomerType + "\n"
                     + "Discount percent: " + percent.format(discountPercent)+ "\n"
                     + "Discount amount:  " + currency.format(discountAmount)+ "\n"
                     + "Total:            " + currency.format(invoiceTotal) + "\n");
      return invoice;
  }
  }
  </code></pre>
  
  <p>I GOT IT TO WORK, Thanks to you guys! Now I understand much more about set and get methods, as well as the this. keyword.
  Here is the working Invoice class:</p>
  
  <pre><code>    import java.text.NumberFormat;
  
  public class Invoice {
  private final String customerType;
  private double subtotal;
  private double discountAmount;
  private double discountPercent;
  private double invoiceTotal;
  private String sCustomerType;
  
  public Invoice(String customerType, double subtotal){
      this.customerType = customerType;
      this.subtotal = subtotal;
  }
  public void setSubtotal(double subtotal){
      this.subtotal = subtotal;
  }
  public double getSubtotal(){
      return subtotal;
  }
  public void setDiscountPercent(double discountPercent){
      this.discountPercent = discountPercent;
  }
  public double getDiscountPercent(){
      if (customerType.equalsIgnoreCase("r"))
      {
          if (subtotal &gt;= 500)
              discountPercent = .2;
          else if (subtotal &gt;= 250 &amp;&amp; subtotal &lt; 500)
              discountPercent =.15;
          else if (subtotal &gt;= 100 &amp;&amp; subtotal &lt; 250)
              discountPercent =.1;
          else if (subtotal &lt; 100)
              discountPercent =.0;
      }
      else if (customerType.equalsIgnoreCase("c"))
      {
              discountPercent = .2;
      }
      else
      {
          discountPercent = .05;
      }
      return discountPercent;
  }
  public void setDiscountAmount(double discountAmount){
      this.discountAmount = discountAmount;
  }
  public double getDiscountAmount(){
      discountAmount = subtotal * (getDiscountPercent());
      return discountAmount;
  }
  public void setInvoiceTotal(double invoiceTotal){
      this.invoiceTotal = invoiceTotal;
  }
  public double getInvoiceTotal(){
      invoiceTotal = subtotal - (getDiscountAmount());
      return invoiceTotal;
  }
  public void setCustomerType(String sCustomerType){
      this.sCustomerType = sCustomerType;
  }
  public String getCustomerType(){
      sCustomerType = "Unknown";
          if (customerType.equalsIgnoreCase("r"))
              sCustomerType = "Retail";
          else if (customerType.equalsIgnoreCase("c"))
              sCustomerType = "College";
      return sCustomerType;
  }
  public String getInvoice(){
      NumberFormat currency = NumberFormat.getCurrencyInstance();
      NumberFormat percent = NumberFormat.getPercentInstance();
      // Create a string representation of the full invoice and return
      String invoice = ("Subtotal:         " + currency.format(getSubtotal()) + "\n"
                     + "Customer type:    " + getCustomerType() + "\n"
                     + "Discount percent: " + percent.format(getDiscountPercent())+ "\n"
                     + "Discount amount:  " + currency.format(getDiscountAmount())+ "\n"
                     + "Total:            " + currency.format(getInvoiceTotal()) + "\n");
      return invoice;
  }
  }
  </code></pre>
  
  <p>Thanks again!</p>