为什么我无法计算销售额和销售税金额?

时间:2015-09-28 14:07:46

标签: java loops methods

public class Purchase
{

    int invoiceNumber = 1234;
    double salePrice = 10.00;
    double SalesTax;

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

    public void setSalePrice(double saleAmount)
    {
        salePrice = saleAmount;
        SalesTax = (saleAmount * .05);//when I'm compiling it's not calculating

    }

    public void displaySalePrice()
    {
        System.out.println("Your invoice number is:" + invoiceNumber + ".");
        System.out.println("Your sale amount is: " + salePrice + ".");
        System.out.println("Your sales tax is: " + SalesTax + ".");
    }

 }

您的发票号码是:1234。 您的销售金额为:10.0。 您的销售税是:0.0 .-------问题区域

---- jGRASP wedge2:进程的退出代码为0。  ---- jGRASP:操作完成。

3 个答案:

答案 0 :(得分:2)

这会有用......

public class Purchase
{

    int invoiceNumber = 1234;
    double salePrice = 10.00;
    double SalesTax = 0.0; // by default this is initialized to zero.

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

    public void setSalePrice(double saleAmount)
    {
        salePrice = saleAmount;
        SalesTax = (saleAmount * .05);//when I'm compiling it's not calculating
    }

    public void displaySalePrice()
    {
        System.out.println("Your invoice number is:" + invoiceNumber + ".");
        System.out.println("Your sale amount is: " + salePrice + ".");
        System.out.println("Your sales tax is: " + SalesTax + ".");
    }

    public static void main(String args[]) 
    {
        setSalePrice(10.0); // sets SalesTax to (100.0 * .05)
        displaySalePrice();
    }

 }

请注意,此课程存在一些风格问题。

  • “SalesTax”以大写字母开头,应该为类(和接口)名称保留。正确的拼写是“salesTax”。

  • 缺少构造函数。

示例构造函数:

public Purchase(int invoiceN, double salesP, doubles salesT) {
    invoiceNum = invoiceN;
    salesPrice = salesP;
    salesTax = salesT;
} 
  • 购买是一件不会改变的东西。它的数据成员是可变的(可变的),但它们应该是不变的(最终的或恒定的)。

    final int invoiceNumber; // These are set in the Constructor.
    final double salePrice; // Once they are set, they don't change.
    final double salesTax;
    
  • 该类具有setter(设置/更改变量),但它缺少getter(在不更改变量的情况下检索变量的值)。通常,变量应尽可能声明为“私有”和“最终”。所以如果我写这个课,我会这样写:

修订示例:

public class Purchase
{

    private final int invoiceNumber;
    private final double salePrice;
    private final double salesTax;

    // Constructor
    public Purchase(int invoiceN, double salesP) {
        invoiceNum = invoiceN;
        salesPrice = salesP;
        salesTax = salesPrice * .05; // The Constructor can figure this out.
    } 

    public int getInvoiceNumber()
    {
        return this.invoiceNumber; // "this." is optional
    }


    public double getSalePrice()
    {
        return this.salePrice();
    }

    public double getSalesTax()
    {
        return this.salesTax;
    }

    public void displaySalePrice()
    {
        System.out.println("Your invoice number is:" + getInvoiceNumber() + ".");
        System.out.println("Your sale amount is: " + getSalePrice() + ".");
        System.out.println("Your sales tax is: " + getSalesTax() + ".");
    }

    public static void main(String args[]) 
    {
        Purchase shoesPurchase = new Purchase(1234, 10.00);
        shoesPurchase.displaySalePrice();
    }

 }

答案 1 :(得分:1)

您永远不会使用setSalePrice方法,因此永远不会初始化您的SalesTax参数。你可以初始化它:double SalesTax = salePrice * 0.05;

答案 2 :(得分:0)

您永远不会调用setSalePrice,因此销售税永远不会被设置

这是纠正这种情况的一种方法,但实际上您应该在调用displaySalePrice之前调用setSalePrice,而不是在其内部调用

public void displaySalePrice()
{
    setSalePrice(salePrice);
    System.out.println("Your invoice number is:" + invoiceNumber + ".");
    System.out.println("Your sale amount is: " + salePrice + ".");
    System.out.println("Your sales tax is: " + SalesTax + ".");
}