Отображение объекта на несколько атрибутов (Java) - PullRequest
1 голос
/ 16 марта 2020

узнав о лямбдах и потоках в моем классе java и пытаясь выяснить эту конкретную часть c.

Вот наше назначение: Используйте класс Invoice, предоставленный для создания массива объектов Invoice. Класс Invoice включает четыре переменных экземпляра; partNumber (тип String), partDescription (тип String), количество покупаемого товара (тип int0 и pricePerItem (тип double). Выполните следующие запросы к массиву объектов Invoice и отобразите результаты:

А. Используйте потоки для сортировки объектов Invoice по partDescription, затем отобразите результаты.

b. Используйте потоки для сортировки объектов Invoice по pricePerItem, а затем отобразите результаты.

c. Используйте потоки, чтобы сопоставить каждый счет-фактуру с его partDescription и количеством, отсортировать результаты по количеству, а затем отобразить результаты

г. Используйте потоки, чтобы сопоставить каждый счет-фактуру с его partDescription и значением счета-фактуры (т. Е. Количество * pricePerItem). Упорядочить результаты по значению счета-фактуры.

е. Изменить деталь (d), чтобы выбрать значения счета-фактуры в диапазоне от $ 200,00 до $ 500,00.

е. Найти любой один счет, в котором partDescription содержит слово «saw».

Где я нахожусь: Итак, у меня есть a) и b), но я немного путать с частью c). Я не нашел в Интернете или в своей книге ничего, что могло бы предложить вам сопоставить объект более чем с одним из его собственных атрибутов. Я видел один пример этого проекта, где кто-то создал отдельную функцию, где они создали строку из двух элементов вместе, но я не думаю, что мой профессор даст очки за это, потому что он не сказал никаких изменений в своем классе Invoice. Мне интересно, хочет ли он, чтобы мы использовали лямбду для изменения метода toString в Invoice, но это не совсем правильно, потому что тогда мы технически не будем отображать объект обоих атрибутов, просто отображать один и изменять его вывод. Найдите его код (не могу изменить) ниже и мой код, который у меня есть.

Код профессора (не может быть изменен):

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

Описание: Electri c Шлифовальная машина Количество: 7

Описание: Кувалда Количество: 11

Описание: Электропила Количество: 18

Описание: Лобзик Количество: 21

Описание: Количество гаечных ключей: 34

Описание: Количество молотка: 76

Описание: Количество отверток: 106

Ответы [ 3 ]

1 голос
/ 16 марта 2020

Вы можете сопоставить элемент чему угодно, включая, например, String:

list.
    stream().
    sorted(Comparator.comparing(Invoice::getQuantity)).
    map(invoice -> 
        String.format(
            "Description: %-15s  Quantity: %-4d", 
            invoice.getPartDescription(), 
            invoice.getQuantity()
        )
    ).
    forEach(System.out::println);
1 голос
/ 17 марта 2020

Вот решение. Похоже, вы уже хорошо понимаете потоки и лямбды. Итак, я не добавил слишком много объяснений в комментарии к коду. Дайте мне знать, если вам нужны какие-либо объяснения.

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class InvoiceProcessor {

    //MAIN METHOD - START HERE !!!
    public static void main(String[] args) {
        List<Invoice> invoices = getInvoices();
        System.out.println("\nQuestion A:");
        partA(invoices);
        System.out.println("\nQuestion B:");
        partB(invoices);
        System.out.println("\nQuestion C:");
        partC(invoices);
        System.out.println("\nQuestion D:");
        partD(invoices);
        System.out.println("\nQuestion E:");
        partE(invoices);
        System.out.println("\nQuestion F:");
        partF(invoices);
    }

    //Generate some sample invoices to use in our code - using lambdas and streams!
    public static List<Invoice> getInvoices(){
        List<String> partNames = Arrays.asList("Lawn mower", "Electric sander", "Sledge hammer",
                "Power saw", "Jig saw", "Wrench", "Hammer", "Screwdriver");

        List<Invoice> invoices = IntStream
                .range(0, partNames.size())
                //Use each number to generate Invoices,i.e. 1 number is MAP-ped to 1 invoice.
                .mapToObj(n -> new Invoice(n, partNames.get(n), (n%3)+1, (n+1) * 50.0))
                //Collect all the invoices in a list.
                .collect(Collectors.toList());

        return invoices;
    }

    //a. Use streams to sort the Invoice objects by partDescription, then display the results.
    public static void partA(List<Invoice> invoices){
        invoices.stream()
                .sorted(Comparator.comparing(Invoice::getPartDescription))
                .forEach(System.out::println);
    }

    //b. Use streams to sort the Invoice objects by pricePerItem, then display the results.
    public static void partB(List<Invoice> invoices){
        invoices.stream()
                .sorted(Comparator.comparing(Invoice::getPrice))
                .forEach(System.out::println);
    }

    //c. Use streams to map each Invoice to its partDescription and quantity, sort the results by quantity,
    // then display the results.
    public static void partC(List<Invoice> invoices){
        //Do we really need to do any mapping here?
        invoices.stream()
                .sorted(Comparator.comparing(Invoice::getQuantity))
                .forEach(i -> System.out.println("Description: " + i.getPartDescription() + " Quantity: " + i.getQuantity())
                );
    }

    //d. Use streams to map each Invoice to its partDescription and the value of the
    // Invoice (i.e., quantity * pricePerItem). Order the results by Invoice value.
    public static void partD(List<Invoice> invoices){
        //Do we really need to do any mapping here?
        invoices.stream()
                .sorted( Comparator.comparingDouble(i -> i.getQuantity() * i.getPrice() ) )
                .forEach(i -> System.out.println("Description: " + i.getPartDescription() + " Invoice value: " + i.getQuantity() * i.getPrice())
                );
    }

    //e. Modify Part (d) to select the Invoice values in the range $200.00 to $500.00.
    public static void partE(List<Invoice> invoices){
        invoices.stream()
                .filter(i -> 200.0 <= i.getQuantity() * i.getPrice() && i.getQuantity() * i.getPrice() <= 500.0)
                .forEach(i -> System.out.println("Description: " + i.getPartDescription() + " Invoice value: " + i.getQuantity() * i.getPrice()));
    }

    //f. Find any one Invoice in which the partDescription contains the word "saw".
    public static void partF(List<Invoice> invoices){
        Invoice saw = invoices.stream()
                .filter(i -> i.getPartDescription().contains("saw"))
                .findFirst()
                .get();
        System.out.println(saw);
    }

}
0 голосов
/ 16 марта 2020

При сопоставлении Stream вы сможете получить доступ только к partDescription, а не к quantity одновременно.

.map(Invoice::getPartDescription) // changed to Stream<String> from Stream<Invoice>

Используйте потоки, чтобы сопоставить каждый Счет-фактуру с его частью Описание и количество, отсортировать результаты по количеству, затем отобразить результаты

Думая, чтобы сохранить кортеж оба эти атрибута одновременно, Map могут быть полезны для сбора информации для дальнейшей обработки ( сортировка ). Это будет выглядеть так:

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(e -> System.out.println("Description: " + e.getKey() + " Quantity: " + e.getValue()));
  • Вы можете использовать аналогичный подход для раздела 'd' с изменением атрибутов.
  • Далее для раздела 'e' вы бы вам нужно использовать .filter для выборочной обработки элементов.
  • Как только вы знаете, как filter, вам нужно findAny для секции 'f', пока вы фильтруете на основе заданного условия вокруг описания.
...