-3

Hello I'm new to programming and a first time poster here. I'm having trouble getting a Java application to display the correct values assigned via Set methods in a public class. Specifically the CreatePurchase application returns 0 for all 3 user defined variables (invoiceNumber, saleAmount, salesTax) which are Set in the Purchase public class.

Set and display the values here

public class Purchase
{
private int invoiceNumber;
private double saleAmount;
private double salesTax;
public int getInvoiceNumber()
{
    return invoiceNumber;
}
public void setInvoiceNumber(int inv)
{
    inv = invoiceNumber;
}
public double getSaleAmount()
{
    return saleAmount;
}
public void setSaleAmount(double sale)
{
    sale = saleAmount;
}
public double getSalesTax()
{
    return salesTax;
}   
public void setSalesTax(double tax)
{
    tax = saleAmount *.05;
    tax = salesTax;
}  
public static void displayPurchase(Purchase aPurch)
{
    System.out.println("Purchase invoice number is " + aPurch.getInvoiceNumber() + "and the sale amount is " + aPurch.getSaleAmount() + "the taxable amount is " +aPurch.getSalesTax());
}
}

This is the CreatePurchase class that prompts the user for the variables and calls the method to display the values for the new object

import java.util.Scanner;
public class CreatePurchase
{
public static void main(String args[])
{
  Purchase aPurchase;
  aPurchase = getPurchaseInfo();
  Purchase.displayPurchase(aPurchase);
}
public static Purchase getPurchaseInfo()
{
  Purchase tempPur = new Purchase();
  int invoice;
  double value;
  double value2;
  Scanner input = new Scanner(System.in);
  System.out.println("Enter the invoice number:");
  invoice = input.nextInt();
  while(invoice < 1000 || invoice > 8000)
  {
     System.out.println("You made an invalid selection");
     System.out.println("You entered " + invoice);
     System.out.println("Please enter a whole number between 1000 and 8000");
     invoice = input.nextInt();
  }
  tempPur.setInvoiceNumber(invoice);
  System.out.println("Enter the amount of the sale:");
  value = input.nextDouble();
  value2 = (value * .05);
  while(value < 0)
  {
     System.out.println("You made an invalid selection");
     System.out.println("You entered " + value);
     System.out.println("Please enter a non negative number");
     value = input.nextDouble();
     value2 = (value *.05);
  }
  tempPur.setSaleAmount(value);
  tempPur.setSalesTax(value2);
  return tempPur;
  }
}

Any direction or advice on how to get the values entered to set and display properly would be greatly appreciated.

user3871672
  • 1
  • 1
  • 2

5 Answers5

3

The setter needs to assign the new value to the instance field.

public void setSaleAmount(double sale)
{
   sale = saleAmount;
}

Yours do the opposite now, switch the assignment around:

public void setSaleAmount(final double sale)
{
   this.saleAmount = sale;
}

You can also optionally add final to the parameter (since you don't intend to change it), and make it clear what the instance field is by using this.. Purely optional, but good practice, and in this case either addition would have resulted in a compile-time error to alert you of the mistake.

Thilo
  • 257,207
  • 101
  • 511
  • 656
2

Your assignments are wrong(for the setters).

It should be salesTax that is set:

public void setSalesTax(double tax)
{
    tax = saleAmount *.05;
    tax = salesTax; /* wrong assignment(for the purpose) */
}

And so on for your other class variables.

Remember: Setters and Getters are used to modify a class' variables.

Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22
1

All setters need to be modified as below:

public void setSalesTax(double tax)
{
    tax = saleAmount *.05;
    //tax = salesTax; <-- This line is wrong..
    this.salesTax= tax ;  // Change this line and it should work...
}

public void setInvoiceNumber(int inv)
{
    //inv = invoiceNumber ; --> Invalid setting
    this.invoiceNumber = inv; // Correct setting
}

public void setSaleAmount(double sale)
{
    //sale = saleAmount;  --> Invalid setting
    this.saleAmount = sale; // Correct setting
}

Reason:

tax is local variable which is specific to the method setSalesTax.

salesTax is global variable for the object.

While setting value in your object, you need to set it/assign in the global variable salesTax

To avoid confusions, The best way to use a setter would be: Object Class:

private double amount;
public void setAmount (double amount)
{
   this.amount = amount;
}
ngrashia
  • 9,869
  • 5
  • 43
  • 58
1

It is because you do not set the new Value to your object value.

The correct way would be

public void setSalesTax(double tax)
{
    tax = saleAmount *.05;
    salexTax = tax; //THIS is the mistake
}  

Everytime you want to assign a value to an Object, Integer, or whatever, it goes from left to right. So, if you write:

int value=5;
int value2=10;
value2=value;

Value2 is 5 then.

romaneso
  • 1,097
  • 1
  • 10
  • 26
0

Your setter method is wrong. You must use actual variable on LHS instead of formal argument.

for Ex, setInvoceNumber method should be like below :

public void setInvoiceNumber(int inv)
{
     invoiceNumber = inv ;
}

Here inv is formal argument while invoiceNumber is actual variable which is going to store your value in object. You were assigning value of invoiceNumber to inv variable which has no effect in your code. Same goes for all your setter method.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47