我对编码很新,因此有疑问。 主要目标是获得LeastPrice项目(包括折扣)。 以下代码工作正常,但只显示最低价格忽略折扣。 如何计算折扣?
答案 0 :(得分:0)
在您比较最低价格的代码区中,您可以考虑每个项目的折扣。
尝试更换:
if (item[i].getItemPrice() < min)
用,
if (item[i].getItemPrice() * item[i].getItemDiscount() < min)
希望有所帮助!自从我使用Java以来已经有一段时间了。)
答案 1 :(得分:0)
There is a bit of logic error in your getLeastPriceItem
function.
Problem #1:
for(int i=1;i<item.length;i++)
The first index should be 0 not 1. Otherwise you are not processing the first item.
Problem #2:
if (item[i].getItemPrice() < min)
You are not including the discount price for comparison.
Assuming discount is by whole number and not percentage then the code should be something like double afterDiscount = item[i].getItemPrice() - item[i].getItemDiscount();
Problem #3:
public static double getLeastPriceItem
Not so much of a problem but I will recommend returning the entity Item
instead.
So here is my proposed solution;
public static void main(String[] args) {
Item[] item= new Item[5];
item[0] = new Item(1, "a", 100.0, 20.0);
item[1] = new Item(2, "b", 80.0, 30.0);
item[2] = new Item(3, "c", 300.0, 40.0);
item[3] = new Item(4, "d", 400.0, 390.0);
item[4] = new Item(5, "e", 500.0, 60.0);
Item cheapest = getLeastPriceItem(item);
System.out.println("Least item price: "+ cheapest.getItemPrice() + " with discount of: " + cheapest.getItemDiscount());
}
public static Item getLeastPriceItem(Item[] item)
{
double currMin = Double.MAX_VALUE;
Item cheapestItem = null;
for(int i=0;i<item.length;i++)
{
double afterDiscount = item[i].getItemPrice() - item[i].getItemDiscount();
if(afterDiscount < currMin)
{
currMin = afterDiscount;
cheapestItem = item[i];
}
}
return cheapestItem;
}
Output:
Least item price: 400.0 with discount of: 390.0
The algorithm here is to track the current cheapest item's value and also the cheapest item object. Loop through all of the items in the array and compare them accordingly, then return the cheapest item object.
Returning the item is a more elegant solution because once you figured out the instance of the cheapest object, you can do/print whatever you like from it's properties. E.g. finding how much discount value it has etc.
答案 2 :(得分:0)
import java.util.Scanner;
public class FindingPrice {
private static double dis;
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Please Enter the Item Name :");
String itmName = s.next();
System.out.println("Please Enter the Item No :");
int itmNo = s.nextInt();
System.out.println("Please Enter the Item rate :");
double rate = s.nextDouble();
System.out.println("Please Enter the Item Quantity :");
int quantity = s.nextInt();
double nPrice;
nPrice = rate * quantity; // nPrice means Net Price
if (nPrice >= 1000 && nPrice < 2000)
dis = 0.10 * nPrice;
else
if (nPrice >= 2000 && nPrice < 3000)
dis = 0.15 * nPrice;
else
if (nPrice >=3000 && nPrice < 4000)
dis = 0.20 * nPrice;
else
if (nPrice >= 5000)
dis = 0.25 * nPrice;
double tPrice = nPrice - dis;
double savings = nPrice - tPrice;
double dPercent = (dis/nPrice) * 100;
int eSavings = 0; // Extra Savings... I will use this for more practice..
System.out.println("Item Number is :" + itmNo);
System.out.println("Item Name is :" + itmName);
System.out.println("Net price of the basket is :" + nPrice);
System.out.println("Discount percentage is :" + dPercent + "%");
System.out.println("Discount on the Basket is :" + dis);
System.out.println("Extra savings on the basket :" + eSavings);
System.out.println("Total savings on the basket is :" + savings);
System.out.println("Total Price of Basket is :" + tPrice);
}
}