我正在写一个名为 discount.java 的 class,我在处理数量和价格的负值时遇到问题
Im writing a class called discount.java and i am having issues processing negative values for quantity and price
正如我提到的,我正在编写一个名为 discount.java 和 TestDiscount.java 的 class;到目前为止一切正常,但当数量或价格和负值时,我在获取错误消息而不是实际值时遇到问题。这里有一些关于所需输出和我到目前为止所做的更多信息:
输出必须是以下形式:
数量为:xx 单价为:$xx.xx 折扣为:$xx.xx
或
数量为:-xx 数量为负数。无法计算折扣
或
数量为:150 单价为:-$15.00 钱为负数,不能
计算折扣金额
public class discount {
int quantity;
double price;
public static int NINETY_NINE = 99;
public static int TWENTY = 20;
private static int TEN = 10, FORTY_NINE = 49;
public static double TEN_PERCENT = 0.10, FIVE_PERCENT = 0.05,
TWO_PERCENT = 0.02, THREE_PERCENT = 0.03, ONE_PERCENT = 0.01 ;
private double discount;
// public static double discount_amount = discount * quantity * price;
discount(int quantity, double price)
{
double discount = 0;
//double d = discount;
this.quantity = quantity;
this.price = price;
}
boolean quantityOutOfRange()
{
return quantity < 0;
}
boolean priceOutOfRange()
{
return price < 0;
}
public double getDiscount()
{
return discount;
}
public int getQuantity()
{
return quantity;
}
public double getUnitPrice()
{
return price;
}
public void calculate()
{
if (quantity > NINETY_NINE)
{
if (price > TWENTY)
discount = TEN_PERCENT;
else if ( price > TEN)
discount = FIVE_PERCENT;
else
discount = TWO_PERCENT;
}
else if (quantity > FORTY_NINE)
{
// calculate discount
if (price > TWENTY)
discount = THREE_PERCENT;
else if ( price > TEN)
discount = TWO_PERCENT;
else
discount = ONE_PERCENT;
}
else
{
// calculate discount
if (price > TWENTY)
discount = TWO_PERCENT;
else if ( price > TEN)
discount = ONE_PERCENT;
else
discount = 1;
}
if (quantity < 0)
{
System.out.println("Quantity is negative. Cannot compute discount");
}
//double discount_amount = discount * quantity * price;
}
}
public class TestDiscount {
public static void main(String[] arg) {
String input = JOptionPane.showInputDialog("Enter the quantity desired "
+ ", and unit price "
+ "\n(Separated by spaces)");
Scanner in = new Scanner(input);
int quantity = in.nextInt();
double price = in.nextDouble();
discount current = new discount(quantity, price);
current.calculate();
System.out.println("\nDiscounts:\n");
System.out.println("The quantity is: " + current.getQuantity()+ "\tThe unit price is = $ " + current.getUnitPrice() +
"\tThe discount is = $ " + current.getQuantity()* current.getUnitPrice() * current.getDiscount());
System.exit(0) ;
}
}
创建名为 like 的新方法
public boolean validated() {
boolean ok = true;
if (quantity < 0)
{
System.out.println("Quantity is negative. Cannot compute
discount");
ok = false;
}
if (price< 0)
{
System.out.println("Price is negative. Cannot compute
discount");
ok = false;
}
return ok;
}
然后在main
discount current = new discount(quantity, price);
if (current.validated ()) {
current.calculate ();
}
在计算方法中首先检查负值
if (quantity < 0)
{
System.out.println("error message");
}
if (price < 0)
{
System.out.println("error message");
}
正如我提到的,我正在编写一个名为 discount.java 和 TestDiscount.java 的 class;到目前为止一切正常,但当数量或价格和负值时,我在获取错误消息而不是实际值时遇到问题。这里有一些关于所需输出和我到目前为止所做的更多信息:
输出必须是以下形式:
数量为:xx 单价为:$xx.xx 折扣为:$xx.xx
或
数量为:-xx 数量为负数。无法计算折扣
或
数量为:150 单价为:-$15.00 钱为负数,不能 计算折扣金额
public class discount {
int quantity;
double price;
public static int NINETY_NINE = 99;
public static int TWENTY = 20;
private static int TEN = 10, FORTY_NINE = 49;
public static double TEN_PERCENT = 0.10, FIVE_PERCENT = 0.05,
TWO_PERCENT = 0.02, THREE_PERCENT = 0.03, ONE_PERCENT = 0.01 ;
private double discount;
// public static double discount_amount = discount * quantity * price;
discount(int quantity, double price)
{
double discount = 0;
//double d = discount;
this.quantity = quantity;
this.price = price;
}
boolean quantityOutOfRange()
{
return quantity < 0;
}
boolean priceOutOfRange()
{
return price < 0;
}
public double getDiscount()
{
return discount;
}
public int getQuantity()
{
return quantity;
}
public double getUnitPrice()
{
return price;
}
public void calculate()
{
if (quantity > NINETY_NINE)
{
if (price > TWENTY)
discount = TEN_PERCENT;
else if ( price > TEN)
discount = FIVE_PERCENT;
else
discount = TWO_PERCENT;
}
else if (quantity > FORTY_NINE)
{
// calculate discount
if (price > TWENTY)
discount = THREE_PERCENT;
else if ( price > TEN)
discount = TWO_PERCENT;
else
discount = ONE_PERCENT;
}
else
{
// calculate discount
if (price > TWENTY)
discount = TWO_PERCENT;
else if ( price > TEN)
discount = ONE_PERCENT;
else
discount = 1;
}
if (quantity < 0)
{
System.out.println("Quantity is negative. Cannot compute discount");
}
//double discount_amount = discount * quantity * price;
}
}
public class TestDiscount {
public static void main(String[] arg) {
String input = JOptionPane.showInputDialog("Enter the quantity desired "
+ ", and unit price "
+ "\n(Separated by spaces)");
Scanner in = new Scanner(input);
int quantity = in.nextInt();
double price = in.nextDouble();
discount current = new discount(quantity, price);
current.calculate();
System.out.println("\nDiscounts:\n");
System.out.println("The quantity is: " + current.getQuantity()+ "\tThe unit price is = $ " + current.getUnitPrice() +
"\tThe discount is = $ " + current.getQuantity()* current.getUnitPrice() * current.getDiscount());
System.exit(0) ;
}
}
创建名为 like 的新方法
public boolean validated() {
boolean ok = true;
if (quantity < 0)
{
System.out.println("Quantity is negative. Cannot compute
discount");
ok = false;
}
if (price< 0)
{
System.out.println("Price is negative. Cannot compute
discount");
ok = false;
}
return ok;
}
然后在main
discount current = new discount(quantity, price);
if (current.validated ()) {
current.calculate ();
}
在计算方法中首先检查负值
if (quantity < 0)
{
System.out.println("error message");
}
if (price < 0)
{
System.out.println("error message");
}