Home Calling a calculated variable from a class
Reply: 3

Calling a calculated variable from a class

Claudia Benitez de Armstrong
1#
Claudia Benitez de Armstrong Published in 2013-06-22 23:41:24Z

I'm having a little bit of a problem with this exercise and I was wondering if anyone could help. here is the problem:

Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase’s details. Save as Purchase.class b. Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1,000 and 8,000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a nonnegative value. After a valid Purchase object has been created, display the object’s invoice number, sale amount, and sales tax.

Here's my code for my Purchase class

import javax.swing.JOptionPane;
import java.util.Scanner;

public class Purchase
{
    //variables
    public static int invoice;
    public static double saleAmount;
    public static double saleTax;

    //get&set for Invoice 
    public void setInvoice(int x)
        {
            invoice = x;
        }
    public int getInvoice( )
        {
            return invoice;
        }

    //get&set for saleAmount
    public void setSaleAmount(double y)
        {
            saleTax = y * 0.05;
            saleAmount = y;
        }
    public double getSaleAmount( )
        {
            return saleAmount;
        }

    //get for saleTax
    public double getSaleTax( )
        {

            return saleTax;
        }

    //display method
    public void display(int invoice, double saleAmount, double saleTax)
        {
        System.out.println("Invoice number: " + invoice + '\n' + "Sale's Amount: " + saleAmount + '\n' + "Sale's Tax: " + saleTax);
        }
}

And the code for the CreatePurchase class

import javax.swing.JOptionPane;
import java.util.Scanner;

public class CreatePurchase

{
    public static void main(String[] args)

    {           
        Purchase purchase1 = new Purchase ();

        //scanner for sales amount      
        Scanner inputDevice = new Scanner(System.in);
      System.out.println("Please enter the sale amount: ");
      Purchase.saleAmount = inputDevice.nextDouble();

        //loop for saleAmount
                while (Purchase.saleAmount < 1)
                    {   
                    System.out.print('\n'+ "Error, your sale amount needs to be more than 0. Please enter a valid sale amount: >> ");
                    Purchase.saleAmount = inputDevice.nextDouble();
                    }

        //scanner for invoice   
        System.out.println("Please enter an invoice number between 1000 and 8000: ");
        Purchase.invoice = inputDevice.nextInt();

                //loop for invoice
                while (Purchase.invoice < 999 || Purchase.invoice > 8000)
                    {
                    System.out.print('\n'+ "Error, please enter a valid invoice number between 1000 and 8000: >> ");
                    Purchase.invoice = inputDevice.nextInt();
                    }

        //display result
        JOptionPane.showMessageDialog(null, "Your invoice number is " + Purchase.invoice + '\n' 
        + "Your sale tax is: " + Purchase.saleTax + '\n'
        + "Your grand total is: " + Purchase.saleAmount);               

    }
}

As you can see, when you run the second class, the saleAmount doesn't include the extra 5% for sale tax and sale tax remains as 0. Is probably something really silly, but I have no idea where to start.

Joni
2#
Joni Reply to 2013-06-22 23:59:14Z

The saleTax is calculated by the setSaleAmount method. You never call this method, so the saleTax is never calculated.

Instead of:

                Purchase.saleAmount = inputDevice.nextDouble();

you probably want:

                purchase1.setSaleAmount(inputDevice.nextDouble());

You may also want to read through some texts that explain the difference between classes and objects, and between static and instance members. But by changing that one line of code the program will at least work.

Eran
3#
Eran Reply to 2013-06-22 23:51:20Z

The definition of invoice, saleAmount and saleTax as public static defeats the purpose of object oriented programming.

They should be private instance variables, and you should access and modify them by calling the get and set methods on the instance you create (purchase1). Otherwise there's no point in defining those setters and getters, and there's no point in creating this instance, which you never use.

Ravi Trivedi
4#
Ravi Trivedi Reply to 2013-06-22 23:51:47Z

1) Convert your Static class variables into Instance Variables.

private int invoice;
private double saleAmount;
private double saleTax;

2) Then use Getter/Setter on your Purchase object

purchase1.setSaleAmount();
purchase1.getSaleAmount();

Note: Static fields do not recognize Object instances. If want to have multiple Purchase objects, your variables should be Instance variables and not class(static) variables.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.326973 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO