Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add a due date to the Invoice application 1. Import the project named ch15 exLIn

ID: 3827029 • Letter: A

Question

Add a due date to the Invoice application

1. Import the project named ch15 exLInvoice that's in the ex starts Then, review the code in the Invoice and InvoiceApp classes.

This is the invoice code

package murach.business;

import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;

public class Invoice {

    // the instance variables
    private ArrayList lineItems;
    private LocalDateTime invoiceDate;
  
    // the constructor
    public Invoice() {
        lineItems = new ArrayList<>();
        invoiceDate = LocalDateTime.now();
    }

    public void addItem(LineItem lineItem) {
        lineItems.add(lineItem);
    }

    public ArrayList getLineItems() {
        return lineItems;
    }

    public double getTotal() {
        double invoiceTotal = 0;
        for (LineItem lineItem : lineItems) {
            invoiceTotal += lineItem.getTotal();
        }
        return invoiceTotal;
    }

    public String getTotalFormatted() {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(getTotal());
    }
  
    public void setInvoiceDate(LocalDateTime invoiceDate) {
        this.invoiceDate = invoiceDate;
    }

2. In the Invoice class, modify the getlnvoiceDateFormatted method so it returns the due date using the MEDIUM format style
3. In the Invoice class, add a method named getDueDate. This method should calculate and retum a LocalDate Time object that's 30 days after the invoice
4. In the Invoice class, add a method named getDueDateFormatted. This method should retum the due date using the MEDIUM format style.
5. In the Invoice App class, modify the displaylnvoice method so it displays the invoice date and the due date before it displays the line items like this
Invoice date                july 1, 2015
Invoice due datet:         Jul 31, 2015

Description                               Price               QTY               Total
Murach's Java Programming $57.50               2                   $115.00
Invoice total $115.00

this is the steps i Need .!! what you mean be productive

    public LocalDateTime getInvoiceDate() {
        return invoiceDate;
    }

    public String getInvoiceDateFormatted() {
        DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(
                FormatStyle.LONG);      
        return dtf.format(invoiceDate);
    }  
}

and this is is the invoiceApp code

package murach.ui;

import murach.db.ProductDB;
import murach.business.Invoice;
import murach.business.LineItem;
import murach.business.Product;

public class InvoiceApp {

    public static Invoice invoice = new Invoice();

    public static void main(String args[]) {
        System.out.println("Welcome to the Invoice application ");
        getLineItems();
        displayInvoice();
    }

    public static void getLineItems() {
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
            String productCode = Console.getString("Enter product code: ");
            int quantity = Console.getInt("Enter quantity:     ");

            Product product = ProductDB.getProduct(productCode);
            invoice.addItem(new LineItem(product, quantity));

            choice = Console.getString("Another line item? (y/n): ");
            System.out.println();
        }
    }

    public static void displayInvoice() {
        StringBuilder sb = new StringBuilder();
        sb.append("Invoice date: ");
        sb.append(invoice.getInvoiceDateFormatted());      
        sb.append(" ");
      
        sb.append(StringUtil.pad("Description", 34));
        sb.append(StringUtil.pad("Price", 10));      
        sb.append(StringUtil.pad("Qty", 5));      
        sb.append(StringUtil.pad("Total", 10));
        sb.append(" ");

        for (LineItem lineItem : invoice.getLineItems()) {
            Product product = lineItem.getProduct();
            sb.append(StringUtil.pad(product.getDescription(), 34));
            sb.append(StringUtil.pad(product.getPriceFormatted(), 10));
            sb.append(StringUtil.pad(lineItem.getQuantityFormatted(), 5));
            sb.append(StringUtil.pad(lineItem.getTotalFormatted(), 10));
            sb.append(" ");
        }
        sb.append(" Invoice total: ");
        sb.append(invoice.getTotalFormatted());
        sb.append(" ");
        System.out.println(sb);
    }
}


2. In the Invoice class, modify the getlnvoiceDateFormatted method so it returns the due date using the MEDIUM format style
3. In the Invoice class, add a method named getDueDate. This method should calculate and retum a LocalDate Time object that's 30 days after the invoice
4. In the Invoice class, add a method named getDueDateFormatted. This method should retum the due date using the MEDIUM format style.
5. In the Invoice App class, modify the displaylnvoice method so it displays the invoice date and the due date before it displays the line items like this
Invoice date                july 1, 2015
Invoice due datet:         Jul 31, 2015

Description                               Price               QTY               Total
Murach's Java Programming $57.50               2                   $115.00
Invoice total $115.00

6 Run the application to make sure it work correctly

here is the both classes codes

This is the invoice code

package murach.business;

import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;

public class Invoice {

    // the instance variables
    private ArrayList lineItems;
    private LocalDateTime invoiceDate;
  
    // the constructor
    public Invoice() {
        lineItems = new ArrayList<>();
        invoiceDate = LocalDateTime.now();
    }

    public void addItem(LineItem lineItem) {
        lineItems.add(lineItem);
    }

    public ArrayList getLineItems() {
        return lineItems;
    }

    public double getTotal() {
        double invoiceTotal = 0;
        for (LineItem lineItem : lineItems) {
            invoiceTotal += lineItem.getTotal();
        }
        return invoiceTotal;
    }

    public String getTotalFormatted() {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(getTotal());
    }
  
    public void setInvoiceDate(LocalDateTime invoiceDate) {
        this.invoiceDate = invoiceDate;
    }

    public LocalDateTime getInvoiceDate() {
        return invoiceDate;
    }

    public String getInvoiceDateFormatted() {
        DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(
                FormatStyle.LONG);      
        return dtf.format(invoiceDate);
    }  
}

and this is is the invoiceApp code

package murach.ui;

import murach.db.ProductDB;
import murach.business.Invoice;
import murach.business.LineItem;
import murach.business.Product;

public class InvoiceApp {

    public static Invoice invoice = new Invoice();

    public static void main(String args[]) {
        System.out.println("Welcome to the Invoice application ");
        getLineItems();
        displayInvoice();
    }

    public static void getLineItems() {
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
            String productCode = Console.getString("Enter product code: ");
            int quantity = Console.getInt("Enter quantity:     ");

            Product product = ProductDB.getProduct(productCode);
            invoice.addItem(new LineItem(product, quantity));

            choice = Console.getString("Another line item? (y/n): ");
            System.out.println();
        }
    }

    public static void displayInvoice() {
        StringBuilder sb = new StringBuilder();
        sb.append("Invoice date: ");
        sb.append(invoice.getInvoiceDateFormatted());      
        sb.append(" ");
      
        sb.append(StringUtil.pad("Description", 34));
        sb.append(StringUtil.pad("Price", 10));      
        sb.append(StringUtil.pad("Qty", 5));      
        sb.append(StringUtil.pad("Total", 10));
        sb.append(" ");

        for (LineItem lineItem : invoice.getLineItems()) {
            Product product = lineItem.getProduct();
            sb.append(StringUtil.pad(product.getDescription(), 34));
            sb.append(StringUtil.pad(product.getPriceFormatted(), 10));
            sb.append(StringUtil.pad(lineItem.getQuantityFormatted(), 5));
            sb.append(StringUtil.pad(lineItem.getTotalFormatted(), 10));
            sb.append(" ");
        }
        sb.append(" Invoice total: ");
        sb.append(invoice.getTotalFormatted());
        sb.append(" ");
        System.out.println(sb);
    }
}

Here is the linetime class code

package murach.business;


import java.text.NumberFormat;

public class LineItem {

    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;
        this.calculateTotal();
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Product getProduct() {
        return product;
    }

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

    public int getQuantity() {
        return quantity;
    }

    public String getQuantityFormatted() {
        NumberFormat number = NumberFormat.getNumberInstance();
        return number.format(quantity);
    }
  
    public void setTotal(double total) {
        this.total = total;
    }

    public double getTotal() {
        return total;
    }

    public void calculateTotal() {
        total = quantity * product.getPrice();
    }

    public String getTotalFormatted() {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(total);
    }
}

Explanation / Answer

InvoiceApp.java

package murach.ui;

import murach.db.ProductDB;
import murach.business.Invoice;
import murach.business.LineItem;
import murach.business.Product;

public class InvoiceApp {

    public static Invoice invoice = new Invoice();

    public static void main(String args[]) {
        System.out.println("Welcome to the Invoice application ");
        getLineItems();
        displayInvoice();
    }

    public static void getLineItems() {
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
            String productCode = Console.getString("Enter product code: ");
            int quantity = Console.getInt("Enter quantity:     ");

            Product product = ProductDB.getProduct(productCode);
            invoice.addItem(new LineItem(product, quantity));

            choice = Console.getString("Another line item? (y/n): ");
            System.out.println();
        }
    }

    public static void displayInvoice() {
        StringBuilder sb = new StringBuilder();
        sb.append("Invoice date: ");
        sb.append(invoice.getInvoiceDateFormatted());      
        sb.append(" ");
        sb.append("Due date:     ");
        sb.append(invoice.getDueDateFormatted());      
        sb.append(" ");
      
        sb.append(StringUtil.pad("Description", 34));
        sb.append(StringUtil.pad("Price", 10));      
        sb.append(StringUtil.pad("Qty", 5));      
        sb.append(StringUtil.pad("Total", 10));
        sb.append(" ");

        for (LineItem lineItem : invoice.getLineItems()) {
            Product product = lineItem.getProduct();
            sb.append(StringUtil.pad(product.getDescription(), 34));
            sb.append(StringUtil.pad(product.getPriceFormatted(), 10));
            sb.append(StringUtil.pad(lineItem.getQuantityFormatted(), 5));
            sb.append(StringUtil.pad(lineItem.getTotalFormatted(), 10));
            sb.append(" ");
        }
        sb.append(" Invoice total: ");
        sb.append(invoice.getTotalFormatted());
        sb.append(" ");
        System.out.println(sb);
    }
}

Invoice.java

package murach.business;

import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;

public class Invoice {

    // the instance variables
    private ArrayList<LineItem> lineItems;
    private LocalDateTime invoiceDate;
  
    // the constructor
    public Invoice() {
        lineItems = new ArrayList<>();
        invoiceDate = LocalDateTime.now();
    }

    public void addItem(LineItem lineItem) {
        lineItems.add(lineItem);
    }

    public ArrayList<LineItem> getLineItems() {
        return lineItems;
    }

    public double getTotal() {
        double invoiceTotal = 0;
        for (LineItem lineItem : lineItems) {
            invoiceTotal += lineItem.getTotal();
        }
        return invoiceTotal;
    }

    public String getTotalFormatted() {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(getTotal());
    }
  
    public void setInvoiceDate(LocalDateTime invoiceDate) {
        this.invoiceDate = invoiceDate;
    }

    public LocalDateTime getInvoiceDate() {
        return invoiceDate;
    }

    public String getInvoiceDateFormatted() {
        DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(
                FormatStyle.MEDIUM);      
        return dtf.format(invoiceDate);
    }  
  
    public LocalDateTime getDueDate() {
        LocalDateTime dueDate = invoiceDate.plusDays(30);
        return dueDate;
    }

    public String getDueDateFormatted() {
        DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(
                FormatStyle.MEDIUM);      
        return dtf.format(getDueDate());
    }  
  
}

LineItem.java

package murach.business;


import java.text.NumberFormat;

public class LineItem {

    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;
        this.calculateTotal();
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Product getProduct() {
        return product;
    }

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

    public int getQuantity() {
        return quantity;
    }

    public String getQuantityFormatted() {
        NumberFormat number = NumberFormat.getNumberInstance();
        return number.format(quantity);
    }
  
    public void setTotal(double total) {
        this.total = total;
    }

    public double getTotal() {
        return total;
    }

    public void calculateTotal() {
        total = quantity * product.getPrice();
    }

    public String getTotalFormatted() {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(total);
    }
}

Product.java

package murach.business;

import java.text.NumberFormat;

public class Product {

    private String code;
    private String description;
    private double price;

    public Product() {
        code = "";
        description = "";
        price = 0;
    }

    public Product(String code, String description, double price) {
        this.code = code;
        this.description = description;
        this.price = price;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

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

    public double getPrice() {
        return price;
    }

    public String getPriceFormatted() {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(price);
    }

}

Console.java

package murach.ui;

import java.util.Scanner;

public class Console {

    private static Scanner sc = new Scanner(System.in);
  
    public static void displayLine() {
        System.out.println();
    }

    public static void displayLine(String s) {
        System.out.println(s);
    }

    public static String getString(String prompt) {
        System.out.print(prompt);
        String s = sc.nextLine();
        return s;
    }

    public static int getInt(String prompt) {
        int i = 0;
        while (true) {
            System.out.print(prompt);
            try {
                i = Integer.parseInt(sc.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Error! Invalid integer. Try again.");
            }
        }
        return i;
    }

    public static double getDouble(String prompt) {
        double d = 0;
        while (true) {
            System.out.print(prompt);
            try {
                d = Double.parseDouble(sc.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Error! Invalid decimal. Try again.");
            }
        }
        return d;
    }
}

StringUtil.java

package murach.ui;

public class StringUtil {

    public static String pad(String s, int length) {
        if (s.length() < length) {
            StringBuilder sb = new StringBuilder(s);
            while (sb.length() < length) {
                sb.append(" ");
            }
            return sb.toString();
        } else {
            return s.substring(0, length);
        }
    }
}

ProductDB.java

package murach.db;

import murach.business.Product;


public class ProductDB {

    public static Product getProduct(String productCode) {
        // In a more realistic application, this code would
        // get the data for the product from a file or database
        // For now, this code just uses if/else statements
        // to return the correct product

        // create the Product object
        Product product = new Product();

        // fill the Product object with data
        product.setCode(productCode);
        if (productCode.equalsIgnoreCase("java")) {
            product.setDescription("Murach's Java Programming");
            product.setPrice(57.50);
        } else if (productCode.equalsIgnoreCase("jsp")) {
            product.setDescription("Murach's Java Servlets and JSP");
            product.setPrice(57.50);
        } else if (productCode.equalsIgnoreCase("mysql")) {
            product.setDescription("Murach's MySQL");
            product.setPrice(54.50);
        } else {
            product.setDescription("Unknown");
        }
        return product;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote