导入javax.swing.JOptionPane;
公共类BeerStoreBP {
私人字符串名称;
私人双重保管;
private double numBeers = 0;
private double beerDisc = 0;
private double beerType = 0;
private double theBeerBrand;
private double total = 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;
}
Noob在这里,所以请放下一些怜悯。上述计划的目的是收集啤酒的数量(必须是3-6),为顾客提供每种啤酒的选择,计算啤酒的总量,并应用折扣(取决于啤酒的数量)。非常简单的东西,但我碰到了一堵砖墙。
我的问题在于计算最终价格。我似乎无法弄清楚为什么calcBeerTotal方法没有传递给我的calcFinalPrice方法。我的输出显示的啤酒数量不是最终价格。
所以我的主要问题是:我的calc方法需要某种setter和getter方法吗?我已经尝试了一些setter和getter方法,但我仍然没有得到最终价格(方法位于calcBeerDisc之上)。我的编程知识有限,所以请不要比我写的更复杂。如果除了制定者和吸气剂之外,请远离阵列等,因为我也不完全理解它们。非常感谢任何帮助!
答案 0 :(得分: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部分并将它们包含在其他地方。