Question

JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes...

JAVA

Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes four instance variables; partNumber (type String), partDescription (type String), quantity of the item being purchased (type int0, and pricePerItem (type double). Perform the following queries on the array of Invoice objects and display the results:

  1. Use streams to sort the Invoice objects by partDescription, then display the results.

  2. Use streams to sort the Invoice objects by pricePerItem, then display the results.

  3. Use streams to map each Invoice to its partDescription and quantity, sort the

    results by quantity, then display the results

  4. 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.

  5. Modify Part (d) to select the Invoice values in the range $200.00 to $500.00.

  6. Find any one Invoice in which the partDescription contains the word “saw”.

Use the data below to test your program.

Invoice Data

Part Number

Part Description

Quantity

Price

83

Electric sander

   

7

57.98

24

Power saw

   

18

99.99

7

Sledge hammer

   

11

21.50

77

Hammer

76

11.99

39

Lawn mower

3

79.50

68

Screwdriver

106

6.99

56

Jig saw

21

11.00

3

Wrench

34

7.50

//***************************************************************
//  Description:       Invoice class.  Do NOT modify this Class.
//***************************************************************

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());
   } 
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code Implemented in java:

Code comments are available for part implementation.

Filename: Invoice.java

public class Invoice {
    int PartNumber;
    String PartDescription;
    int Quantity;
    double Price;

    @Override
    public String toString() {
        return "Invoice{" + "PartNumber=" + PartNumber + ", PartDescription=" + PartDescription + ", Quantity=" + Quantity + ", Price=" + Price + '}';
    }

    public Invoice(int PartNumber, String PartDescription, int Quantity, double Price) {
        this.PartNumber = PartNumber;
        this.PartDescription = PartDescription;
        this.Quantity = Quantity;
        this.Price = Price;
    }

    public int getPartNumber() {
        return PartNumber;
    }

    public void setPartNumber(int PartNumber) {
        this.PartNumber = PartNumber;
    }

    public String getPartDescription() {
        return PartDescription;
    }

    public void setPartDescription(String PartDescription) {
        this.PartDescription = PartDescription;
    }

    public int getQuantity() {
        return Quantity;
    }

    public void setQuantity(int Quantity) {
        this.Quantity = Quantity;
    }

    public double getPrice() {
        return Price;
    }

    public void setPrice(double Price) {
        this.Price = Price;
    }

}

Filename: Main.java

import java.sql.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Main {
public static void main(String[] args) {
Object[] array = {
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, "Lawm nower", 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)
};

try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost:3306/store_DB", "root", " ");
Statement statement = connection.createStatement();

//insert
PreparedStatement ps = connection.prepareStatement
("INSERT into invoice(fname,mname,lname) VALUES(?,?,?,?)");
for (Object obj : array) {
Invoice invoice = (Invoice) obj;
ps.setInt(1, invoice.getPartNumber());
ps.setString(2, invoice.getPartDescription());
ps.setInt(3, invoice.getQuantity());
ps.setDouble(4, invoice.getPrice());
ps.addBatch();
}

//select
ResultSet resultSet = statement.executeQuery("SELECT * from invoice");
List<Invoice> invoices = new ArrayList<>();
while (resultSet.next()) {
Invoice invoice = new Invoice(
resultSet.getInt("Part_Number"),
resultSet.getString("Part_Description"),
resultSet.getInt("Quantity"),
resultSet.getDouble("Price")
);
invoices.add(invoice);
}

//a (sort by PartDescription)
invoices.stream()
.sorted(Comparator.comparing(i -> i.getPartDescription()))
.forEach(s -> System.out.println(s));

//b (sort by Price)
invoices.stream()
.sorted(Comparator.comparing(i -> i.getPrice()))
.forEach(s -> System.out.println(s));

//c
invoices.stream()
.sorted(Comparator.comparing(i -> i.getQuantity()))
.map(i -> i.getPartDescription() + " " + i.getQuantity())
.forEach(s -> System.out.println(s));

//d
invoices.stream()
.sorted(Comparator.comparing(i -> i.getQuantity() * i.getPrice()))
.map(i -> i.getPartDescription() + " " + (i.getPrice() * i.getQuantity()))
.forEach(s -> System.out.println(s));

//e
invoices.stream()
.sorted(Comparator.comparing(i -> i.getQuantity() * i.getPrice()))
.filter(i-> (i.getQuantity() * i.getPrice()) >=200 && (i.getQuantity() * i.getPrice()) <=500 )
.map(i -> i.getPartDescription() + " " + (i.getPrice() * i.getQuantity()))
.forEach(s -> System.out.println(s));

} catch (ClassNotFoundException ex) {
System.err.println("Class Not Found!!!");
ex.printStackTrace();
} catch (SQLException ex) {
System.err.println("SQL Error!!!");
ex.printStackTrace();
}
}
}



If you like my answer, hit thumbs up.

Add a comment
Know the answer?
Add Answer to:
JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 02: Based on LINQ extension methods. [15 marks] Create an Invoice class which includes four...

    Exercise 02: Based on LINQ extension methods. [15 marks] Create an Invoice class which includes four properties - a PartNumber ( type int), a PartDescription ( type string), a Quantity of item being purchased (type int) and a Pricel type decimal). Use the following sample data for Invoice class objects: Quantity 7 Part Number 87 24 7 77 39 68 Part Description Electric Sander Power Saw Sledge Hammer Hammer Lawn Mower Screw Driver Jig saw 18 11 76 3 Price...

  • Add appropriate descriptive comments to each line of the code, explaining why the code is in...

    Add appropriate descriptive comments to each line of the code, explaining why the code is in the application. import java.text.NumberFormat; public class LineItem implements Cloneable { private Product product; private int quantity; private double total; public LineItem() { this.product = new Product(); this.quantity = 0; this.total = 0; } public LineItem(Product product, int quantity) { this.product = product; this.quantity = quantity; } public void setProduct(Product product) { this.product = product; } public Product getProduct() { return product; } public void...

  • in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "Ret...

    in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "RetailItem" class which simulates the sale of a retail item. It should have a constructor that accepts a "RetailItem" object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. Your class should have these methods: getSubtotal: returns the subtotal of the sale, which is quantity times price....

  • Write an equals method for the Shirt class provided below. Two shirts are logically equivalent if...

    Write an equals method for the Shirt class provided below. Two shirts are logically equivalent if they have the same size, color, and price. public class Shirt { private Size size; private String color; private double price; enum Size { SMALL, MEDIUM, LARGE } public Shirt(Size size, String color, double price) { this.size = size; this.color = color; this.price = price; } public Size getSize() { return size; } public String getColor() { return color; } public double getPrice() {...

  • Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...

    Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts] The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object   [25 pts] In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects)  [25...

  • Use java and continue stage 2 and 3 stage 1 code public abstract class BabyItem { protected String name;...

    Use java and continue stage 2 and 3 stage 1 code public abstract class BabyItem { protected String name; public BabyItem() { name=""; } public BabyItem(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { } public abstract double getCost(); } ======================================================================================== public class BabyFood extends BabyItem { private int numberOfJars; private double pricePerDozen; public BabyFood() { super(); numberOfJars = 0; pricePerDozen = 0; } public BabyFood(int numberOfJars, double pricePerDozen) {...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT