0

导入 javax.swing.JOptionPane;

公共类 BeerStoreBP {

私有字符串名称;

私人双重监护;

私人双 numBeers = 0;

私人双 beerDisc = 0;

私人双啤酒类型 = 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 之外的东西,请远离数组等,因为我也不完全理解它们。任何帮助深表感谢!

4

1 回答 1

0

重写:

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 部分从此类中取出并将它们包含在其他地方。

于 2012-12-06T03:58:22.807 回答