gpt4 book ai didi

java - 将对象映射到多个属性 (Java)

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:03 25 4
gpt4 key购买 nike

在我的 java 类中学习 lambda 和流,并尝试弄清楚这一特定部分。

这是我们的作业:使用提供的 Invoice 类来创建 Invoice 对象的数组。类发票包括四个实例变量;零件编号(字符串类型)、零件描述(字符串类型)、所购买商品的数量(类型 int0 和pricePerItem(类型 double))。执行对 Invoice 对象数组进行以下查询并显示结果:

a.使用流按partDescription对Invoice对象进行排序,然后显示结果。

b.使用流按 PricePerItem 对 Invoice 对象进行排序,然后显示结果。

c.使用流将每个发票映射到其零件描述和数量,对按数量排序结果,然后显示结果

d.使用流将每个发票映射到其partDescription和发票(即数量 * 每件商品的价格)。按发票金额对结果进行排序。

e.修改 (d) 部分以选择 $200.00 到 $500.00 范围内的发票值。

f.查找任何一张零件描述包含单词“锯”的发票。

我在哪里:所以我已经得到了 a) 和 b),但我对 c) 部分有点困惑。我在网上或我的书中没有发现任何内容表明您可以将对象映射到其自身的多个属性。我见过这个项目的一个例子,其中有人创建了一个单独的函数,他们在其中创建了两个元素组合的字符串,但我认为我的教授不会为此评分,因为他说没有修改他的发票类。我想知道他是否希望我们使用 lambda 来修改 Invoice 的 toString 方法,但这似乎不太正确,因为从技术上讲,我们不会映射两个属性的对象,而只是映射到一个属性并更改其输出。在下面找到他的代码(无法修改),以及我迄今为止拥有的代码。

教授代码(无法修改):

public class Invoice {
private final int partNumber;
private final String partDescription;
private int quantity;
private double price;

// constructor
public Invoice(int partNumber, String partDescription, int quantity, double price)
{
if (quantity < 0) { // validate quantity
throw new IllegalArgumentException("Quantity must be>= 0");
}

if (price < 0.0) { // validate price
throw new IllegalArgumentException(
"Price per item must be>= 0");
}

this.partNumber = partNumber;
this.partDescription = partDescription;
this.quantity = quantity;
this.price = price;
}

// get part number
public int getPartNumber() {
return partNumber; // should validate
}

// get description
public String getPartDescription() {
return partDescription;
}

// set quantity
public void setQuantity(int quantity) {
if (quantity <0) { // validate quantity
throw new IllegalArgumentException("Quantity must be>= 0");
}

this.quantity = quantity;
}

// get quantity
public int getQuantity() {
return quantity;
}

// set price per item
public void setPrice(double price) {
if (price <0.0) { // validate price
throw new IllegalArgumentException(
"Price per item must be>= 0");
}

this.price = price;
}

// get price per item
public double getPrice() {
return price;
}

// return String representation of Invoice object
@Override
public String toString() {
return String.format(
"Part #: %-2d Description: %-15s Quantity: %-4d Price: $%,6.2f",
getPartNumber(), getPartDescription(),
getQuantity(), getPrice());
}
}

**这是迄今为止我的代码:**

// import statements
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class InvoiceDriver {

//***************************************************************
// Method: developerInfo
// Description: The developer information method of the program
// Parameters: none
// Returns: n/a
//**************************************************************
public static void developerInfo() {
System.out.println("");
System.out.println("*************************************************");
System.out.println ("Name: Allison Crenshaw");
System.out.println ("Course: ITSE 2317 Intermediate Java Programming");
System.out.println ("Program: Five");
System.out.println("*************************************************");
} // End of developerInfo

public static void main(String[] args) {
// variables
Invoice[] invoices = {
new Invoice(83, "Electric sander",
7, 57.98),
new Invoice(24,"Power saw",
18, 99.99),
new Invoice(7, "Sledge hammer",
11, 21.50),
new Invoice(77, "Hammer",
76, 11.99),
new Invoice(39, "Lawn mower",
3, 79.50),
new Invoice(68, "Screwdriver",
106, 6.99),
new Invoice(56, "Jig saw",
21, 11.00),
new Invoice(3, "Wrench",
34, 7.50)};

// display developer info
developerInfo();

// welcome message
System.out.println("Welcome to this Invoice Program.");
System.out.println("This program receives invoice information " +
"and displays");
System.out.println("the info based on various sorts using lambdas " +
"and streams.");
System.out.println();

// get list view of Invoices and use to stream and print
List<Invoice> list = Arrays.asList(invoices);

// use a st

// a) use streams to sort the invoices by descriptions, then display
System.out.println("Invoices sorted by description: ");
Arrays.stream(invoices)
.sorted(Comparator.comparing(Invoice::getPartDescription))
.forEach(System.out::println);
System.out.println();

// b) use streams to sort the invoices by price, then display
System.out.println("Invoices sorted by price: ");
Arrays.stream(invoices)
.sorted(Comparator.comparing(Invoice::getPrice))
.forEach(System.out::println);
System.out.println();

// c) use streams to map each invoice to its description and quantity,
// sort the results by quantity, then display the results
System.out.println("Invoices mapped to description and quantity " +
"and sorted by quantity: ");
list.stream()
.map(Invoice::getPartDescription)
.forEach(System.out::println);

// d) use streams to map each invoice to its description and the
// value of the invoice (quantity * price) then order by value

// e) modify part d) to select the invoice values in range $200-$500

// f) find any one invoice in which description contains the word "saw"


} // main

} // end InvoiceDriver

**以下是 c) 部分的输出:**

发票映射到描述和数量:

描述:割草机数量:3

描述:电动打磨机数量:7

描述:大锤数量:11

描述:电锯数量:18

描述:曲线锯数量:21

描述: Spanner 数量:34

描述:锤子数量:76

描述: Screwdriver 数量:106

最佳答案

您可以将元素映射到您想要的任何内容,例如,包括String:

list.
stream().
sorted(Comparator.comparing(Invoice::getQuantity)).
map(invoice ->
String.format(
"Description: %-15s Quantity: %-4d",
invoice.getPartDescription(),
invoice.getQuantity()
)
).
forEach(System.out::println);

关于java - 将对象映射到多个属性 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60701000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com