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

问题描述 投票:0回答:1

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

这是我们的作业:使用提供的类Invoice创建一个Invoice对象数组。类别发票包括四个实例变量; partNumber(字符串类型),partDescription(字符串类型),购买商品的数量(类型int0和pricePerItem(类型double)。对发票对象数组进行以下查询并显示结果:

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

b。使用流按pricePerItem对发票对象进行排序,然后显示结果。

c。使用流将每个发票映射到其零件说明和数量,对按数量显示结果,然后显示结果

d。使用流将每个发票映射到其partDescription和发票(即数量* pricePerItem)。按发票值订购结果。

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

f。查找其中partDescription包含单词“ saw”的任何一张发票。

我在哪里:所以我有a)和b)了,但是我对c)部分感到有些困惑。在网上或我的书中没有发现任何建议您可以将一个对象映射到其多个属性之一的内容。我已经看到了这个项目的一个示例,其中有人创建了一个单独的函数,在其中创建了两个元素组合而成的字符串,但是我认为我的教授不会为此提出任何意见,因为他说没有修改Invoice类。我想知道他是否要我们使用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

描述:扳手数量:34

描述:锤子数量:76

描述:螺丝刀数量:106

java lambda stream maps mapping
1个回答
0
投票

使用流将每个发票映射到其部件说明和数量,按数量对结果排序,然后显示结果

list.stream()
        .collect(Collectors.toMap(Invoice::getPartDescription,
                Invoice::getQuantity)) // map description to its quantity
        .entrySet().stream()
        .sorted(Map.Entry.comparingByValue()) // sort by quantity (values of map)
        .forEach(System.out::println);
© www.soinside.com 2019 - 2024. All rights reserved.