【问题标题】:Does one need getters and setters for calculation methods?计算方法是否需要 getter 和 setter?
【发布时间】:2012-12-06 03:50:04
【问题描述】:

导入 javax.swing.JOptionPane;

公共类 BeerStoreBP {

私有字符串名称;

私人双重保管;

private double numBeers = 0;

私人双 beerDisc = 0;

私人双 beerType = 0;

私人双啤酒品牌;

私人双总数 = 0;

私人双啤酒 = 0;

public void setAge(double theAge)
{
    custAge = theAge;
}

public void setName(String theName)
{
    name = theName;
}

public void setNumBeers(double theNumBeers)
{
    numBeers = theNumBeers;
}

public void setBeerType(double theBeerType)
{
    beerType = theBeerType;
}

public double getAge()
{
    return custAge;
}

public String getName()
{
    return name;
}

public double getNumBeers()
{
    return numBeers;
}

public double calcNumBeersValid()
{

    String numBeersStr;

    while (numBeers < 3 || numBeers > 6)
    {
        numBeersStr = JOptionPane.showInputDialog("You must pick 3-6 " +
                                                                "beers");
        numBeers = Double.parseDouble(numBeersStr);
    }

    return numBeers;
}

public double calcBeerPrice()
{

    final double LAGIMRED = 1.90;  
    final double DESINIPA = 2.00;
    final double BELBEBRN = 1.80;
    final double SCHOATST = 2.50;
    final double BOULFHSAISN = 2.75;
    final double GANDANCPRTR = 1.75;


       if (theBeerBrand == 1)
           beer = LAGIMRED;

       else if (theBeerBrand == 2)
           beer = DESINIPA;

       else if (theBeerBrand == 3)
           beer = BELBEBRN;

       else if (theBeerBrand == 4)
           beer = SCHOATST;

       else if (theBeerBrand == 5)
           beer = BOULFHSAISN;

       else 
           beer = GANDANCPRTR;        

       return beer;


public double calcBeerTotal()
{
    String beerTypeStr;
    double count = 1;

    while (count <= numBeers)
    {    

    beerTypeStr = JOptionPane.showInputDialog("Please choose between " 
                + "these fine selections:\n1 - Lagunitas Imperial Red - " +
                       "$1.90\n2 - Deschutes Inversion IPA - $2.00\n3 - " +
                 "Bell's Best Brown Ale - $1.80\n4 - Schlafly's Oatmeal " +
                 "Stout - $2.50\n5 - Boulevard's Farmhouse Saison - $2.75" 
                + "\n6 - Gandy Dancer Porter - $1.75");
        beerType = Double.parseDouble(beerTypeStr);
        beerType = theBeerBrand;
        total += beer;
        count++;
    }    

    return total;

}

public double getBeerTotal()
{
    double theTotal;
    theTotal = total; 

    return theTotal;
}

public double calcBeerDisc()
{

    if (numBeers == 6)
        beerDisc = .10;

    else if (numBeers >= 4)
        beerDisc = .05;

    else 
        beerDisc = .00; 

    return beerDisc;
}

public double calcFinalPrice()
{

   double finalPrice;

    finalPrice = total-(total * beerDisc);

    return finalPrice;

}

这里是菜鸟,所以请怜悯。上述程序的要点是收集啤酒的数量(必须是 3-6),为客户提供每种啤酒的选择,计算啤酒的总量,并应用折扣(取决于啤酒的数量)。很简单的东西,但我已经碰壁了。

我的问题在于计算最终价格。我似乎无法弄清楚为什么 calcBeerTotal 方法中的任何内容都没有传递给我的 calcFinalPrice 方法。我的输出显示啤酒的数量,而不是最终价格。

所以我的主要问题是:我的 calc 方法是否需要某种 setter 和 getter 方法?我尝试了一些 setter 和 getter 方法,但仍然没有得到最终价格(方法位于 calcBeerDisc 上方)。我的编程知识有限,所以请不要变得比我写的更复杂。如果它是除了 setter 和 getter 之外的东西,请远离数组等,因为我也不完全理解它们。非常感谢任何帮助!

【问题讨论】:

    标签: class methods getter-setter


    【解决方案1】:

    重写:

    import javax.swing.JOptionPane;
    
    public class BeerStoreBP {
    
    private String name;
    
    private double custAge;
    
    private double numBeers = 0;
    
    private double beerDisc = 0;
    
    private double total = 0;
    
    // This only exists in one method.  There is no need for it to be global
    //private double beerType = 0;
    
    // This only exists in one method.  There is no need for it to be global    
    //private double theBeerBrand;
    
    // This only exists in one method.  There is no need for it to be global    
    //private double total = 0;
    
    // This only exists in one method.  There is no need for it to be global
    //private double beer = 0;
    
    public void setAge(double theAge)
    {
        custAge = theAge;
    }
    
    public void setName(String theName)
    {
        name = theName;
    }
    
    public void setNumBeers(double theNumBeers)
    {
        numBeers = theNumBeers;
    }
    
    // Your functionality is parsed by your dialog.  There is no need for this.
    //public void setBeerType(double theBeerType)
    //{
    //    beerType = theBeerType;
    //}
    
    public double getAge()
    {
        return custAge;
    }
    
    public String getName()
    {
        return name;
    }
    
    public double getNumBeers()
    {
        return numBeers;
    }
    
    public double calcNumBeersValid()
    {
    
        String numBeersStr;
    
        while (numBeers < 3 || numBeers > 6)
        {
            numBeersStr = JOptionPane.showInputDialog("You must pick 3-6 " +
                                                                    "beers");
            numBeers = Double.parseDouble(numBeersStr);
        }
    
        return numBeers;
    }
    
    public double calcBeerPrice(int beerBrand)
    {
    
        final double LAGIMRED = 1.90;  
        final double DESINIPA = 2.00;
        final double BELBEBRN = 1.80;
        final double SCHOATST = 2.50;
        final double BOULFHSAISN = 2.75;
        final double GANDANCPRTR = 1.75;
    
    
        double beerPrice;
    
           if (beerBrand == 1)
               beerPrice = LAGIMRED;
    
           else if (beerBrand == 2)
               beerPrice = DESINIPA;
    
           else if (beerBrand == 3)
               beerPrice = BELBEBRN;
    
           else if (beerBrand == 4)
               beerPrice = SCHOATST;
    
           else if (beerBrand == 5)
               beerPrice = BOULFHSAISN;
    
           else 
               beerPrice = GANDANCPRTR;        
    
           return beerPrice;
    
    }
    
    public void calcBeerTotal()
    {
        String beerTypeStr;
        double count = 1;
        double beerPrice;
        // No need to be double
        int beerBrand;
    
        while (count <= numBeers)
        {    
    
            beerTypeStr = JOptionPane.showInputDialog("Please choose between " 
                    + "these fine selections:\n1 - Lagunitas Imperial Red - " +
                           "$1.90\n2 - Deschutes Inversion IPA - $2.00\n3 - " +
                     "Bell's Best Brown Ale - $1.80\n4 - Schlafly's Oatmeal " +
                     "Stout - $2.50\n5 - Boulevard's Farmhouse Saison - $2.75" 
                    + "\n6 - Gandy Dancer Porter - $1.75");
            beerBrand = Integer.parseInt(beerTypeStr);
            beerPrice = calcBeerPrice(beerBrand);
            total += beerPrice;
            count++; 
    
        }
    
    }
    
    public double getBeerTotal()
    {
        return total;
    }
    
    public double calcBeerDisc()
    {
    
        if (numBeers == 6)
            beerDisc = .10;
    
        else if (numBeers >= 4)
            beerDisc = .05;
    
        else 
            beerDisc = .00; 
    
        return beerDisc;
    }
    
    public double calcFinalPrice()
    {
    
       double finalPrice;
    
        finalPrice = total-(total * beerDisc);
    
        return finalPrice;
    
    }
    
    
    }
    

    这处于工作状态,但我建议您将 UI 部分从此类中取出并将它们包含在其他地方。

    【讨论】:

    • 我已经尝试了您发布的内容,但仍然没有得到最终价格。 beer 作为一个全局变量,或者其中的任何一个,是否会影响方法之间的值传递?
    • 我没有更改任何代码,我只是添加了 cmets。编辑:我更改了一些代码
    • 我的意思是我将 calcBeerPrice 方法中的 theBeerBrand 更改为 beerType 并消除了 beerType = theBeerBrand 语句。还是没有bueno。编辑:我会尽快尝试一下,谢谢。
    • 成功了!!只是为了让我完全理解你写的内容,没有办法调用 calcBeerPrice 方法使我无法将值传递给该方法吗?
    • 值已设置,但如果您不调用该方法,则不会发生任何事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2013-12-29
    • 2011-04-14
    • 2017-09-09
    • 1970-01-01
    相关资源
    最近更新 更多