如何编写一个方法,将项目数组作为输入和 return 最低价格的项目(也考虑折扣)?
How to write a method which will take item array as input and return item with least price (considering discount as well)?
我是 Java 的新手,我正在尝试在此 class 中声明另一个名为 getItemPrice
的方法,它将项目数组作为输入并 return价格最低的商品(也考虑折扣)。
到目前为止,我已经在 main 方法中声明了一个包含 2 个项目对象的数组。
这是我的主要方法:
public static void main(String[] args) {
Item[] Item;
Item = new Item[2]
Item[0] = new Item(1, "Item One", 5, 2);
Item[1] = new Item(2, "Item Two", 10, 5);
}
正如您在上面看到的,每个项目数组都有一个 id、名称、价格和折扣。
现在,使用这些项目数组,我试图在这个 class 中声明另一个方法,称为 getItemPrice
,它将项目数组作为输入,并且 return 价格最低的项目(也考虑打折)。
例如,该方法将接受项目一和项目 2,但将 return 项目 1,因为 5 减 2 得到 3,小于项目 2,即 10 减 5,给出5.
因此,商品 1 将被 return 编辑,因为它在添加折扣时价格最低。
我真的不确定如何完成这个,所以任何帮助将不胜感激。
Class 实施:
public class Item {
int ItemId;
String itemName;
double itemPrice;
double itemDiscount;
public Item(int ItemId, String itemName, double itemPrice, double itemDiscount) {
super();
this.ItemId = itemId;
this.ItemName = itemName;
this.itemPrice = itemPrice;
this.itemDiscount = itemDiscount;
}
// Getters and setters follow after this
}
你可以迭代每个Item
,如果它的价格低于之前的较低价格
,则保存对当前价格的引用
static Item getItemPrice(Item[] items) {
double lowerPrice = Double.MAX_VALUE;
Item lower = null;
for (Item item : items) {
double finalPrice = item.itemPrice - item.itemDiscount;
if (finalPrice < lowerPrice) {
lowerPrice = finalPrice;
lower = item;
}
}
return lower;
}
public static void main(String[] args) {
Item[] items = new Item[]{
new Item(1, "Item One", 5, 2),
new Item(2, "Item Two", 10, 5)};
System.out.println(getItemPrice(items)); // Item{id=1, name='Item One', price=5, discount=2}
}
高级 Stream
static Item getItemPrice(Item[] items) {
return Arrays.stream(items)
.min(Comparator.comparingDouble(item -> item.itemPrice - item.itemDiscount))
.orElse(null);
}
请注意,变量名的约定是小驼峰命名,不要以 class 名称作为前缀,一个好的 class 应该是
class Item {
int id;
String name;
double price;
double discount;
public Item(int id, String name, double price, double discount) {
this.id = id;
this.name = name;
this.price = price;
this.discount = discount;
}
}
我是 Java 的新手,我正在尝试在此 class 中声明另一个名为 getItemPrice
的方法,它将项目数组作为输入并 return价格最低的商品(也考虑折扣)。
到目前为止,我已经在 main 方法中声明了一个包含 2 个项目对象的数组。
这是我的主要方法:
public static void main(String[] args) {
Item[] Item;
Item = new Item[2]
Item[0] = new Item(1, "Item One", 5, 2);
Item[1] = new Item(2, "Item Two", 10, 5);
}
正如您在上面看到的,每个项目数组都有一个 id、名称、价格和折扣。
现在,使用这些项目数组,我试图在这个 class 中声明另一个方法,称为 getItemPrice
,它将项目数组作为输入,并且 return 价格最低的项目(也考虑打折)。
例如,该方法将接受项目一和项目 2,但将 return 项目 1,因为 5 减 2 得到 3,小于项目 2,即 10 减 5,给出5.
因此,商品 1 将被 return 编辑,因为它在添加折扣时价格最低。
我真的不确定如何完成这个,所以任何帮助将不胜感激。
Class 实施:
public class Item {
int ItemId;
String itemName;
double itemPrice;
double itemDiscount;
public Item(int ItemId, String itemName, double itemPrice, double itemDiscount) {
super();
this.ItemId = itemId;
this.ItemName = itemName;
this.itemPrice = itemPrice;
this.itemDiscount = itemDiscount;
}
// Getters and setters follow after this
}
你可以迭代每个Item
,如果它的价格低于之前的较低价格
static Item getItemPrice(Item[] items) {
double lowerPrice = Double.MAX_VALUE;
Item lower = null;
for (Item item : items) {
double finalPrice = item.itemPrice - item.itemDiscount;
if (finalPrice < lowerPrice) {
lowerPrice = finalPrice;
lower = item;
}
}
return lower;
}
public static void main(String[] args) {
Item[] items = new Item[]{
new Item(1, "Item One", 5, 2),
new Item(2, "Item Two", 10, 5)};
System.out.println(getItemPrice(items)); // Item{id=1, name='Item One', price=5, discount=2}
}
高级 Stream
static Item getItemPrice(Item[] items) {
return Arrays.stream(items)
.min(Comparator.comparingDouble(item -> item.itemPrice - item.itemDiscount))
.orElse(null);
}
请注意,变量名的约定是小驼峰命名,不要以 class 名称作为前缀,一个好的 class 应该是
class Item {
int id;
String name;
double price;
double discount;
public Item(int id, String name, double price, double discount) {
this.id = id;
this.name = name;
this.price = price;
this.discount = discount;
}
}