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

Create a class named Invoice that holds an invoice number, balance due, and thre

ID: 3693612 • Letter: C

Question

Create a class named Invoice that holds an invoice number, balance due, and three fields representing the month, day, and year when the balance is due. Create a constructor that accepts values for all five data fields. Within the constructor, assign each argument to the appropriate field with the following exceptions: If an invoice number is less than 1000, force the invoice number to 0. If the month field is less than 1 or greater than 12, force the month field to 0. If the day field is less than 1 or greater than 31, force the day field to 0. If the year field is less than 2011 or greater than 2017, force the year field to 0. In the Invoice class, include a display method that displays all the fields on an Invoice object. Save the file as Invoice.java. Write an application containing a main() method that declares several Invoice objects, proving that all the statements in the constructor operate as specified. Save the file as TestInvoice.Java.

Explanation / Answer

import java.util.Date;
/**
* File: Invoice
*/

public class Invoice
   {
       private int invoiceNumber;
       private double balanceDue;
       private int month;
       private int day;
       private int year;
       public Invoice(int invoice, double balance, int mth, int d, int yr)
           {
               balanceDue = balance;
               if (invoice < 1000)
                   {
                       invoice = 0;
                   }
               else
                   {
                       invoiceNumber = invoice;
                   }
               if (mth < 1 || mth > 12)
                   {
                       mth = 0;
                   }
               else
                   {
                       month = mth;
                   }
               if (d < 1 || d > 31)
                   {
                       d = 0;
                   }
               else
                   {
                       day = d;
                   }
               if (yr < 2011 || yr > 2017)
                   {
                       yr = 0;
                   }
               else
                   {
                       year = yr;
                   }
           }
       public void displayInvoice()
           {
               System.out.println("Invoice Number: " + invoiceNumber);
               System.out.println("Due Date: " + month + "-" + day + "-" + year);
               System.out.println("Balance Due: " + balanceDue);
           }
   }

/**
* File: TestInvoice
* Write an application containing a main() method that declares several
* Invoice objects, proving that all the statements in the constructor
* operate as specified. Save the file as TestInvoice.java.
*/

public class TestInvoice
   {
       public static void main(String[] args)
           {
               Invoice lessThan = new Invoice(999, 0, 0, 0, 2010);
               Invoice moreThan = new Invoice(1001, 56.00, 13, 32, 2018);
               Invoice correct = new Invoice(1234, 185.00, 3, 12, 2014);
               lessThan.displayInvoice();
               System.out.println();
               moreThan.displayInvoice();
               System.out.println();
               correct.displayInvoice();
           }
   }


/**
* File: TestInvoice2
* Then modify the TestInvoice class to create Invoice2 objects.
* Create enough objects to test every decision in the constructor.
* Save this file as TestInvoice2.java.
*/

public class TestInvoice2
   {
       public static void main(String[] args)
           {
               Invoice2 test001 = new Invoice2(999, 56.00, 1, 32, 2014);
               Invoice2 test002 = new Invoice2(5423, 56.00, 2, 31, 2014);
               Invoice2 test003 = new Invoice2(1235, 56.00, 3, 32, 2014);
               Invoice2 test004 = new Invoice2(8563, 56.00, 4, 31, 2014);
               Invoice2 test005 = new Invoice2(9999, 56.00, 5, 32, 2014);
               Invoice2 test006 = new Invoice2(2222, 56.00, 6, 31, 2014);
               Invoice2 test007 = new Invoice2(1111, 56.00, 7, 32, 2014);
               Invoice2 test008 = new Invoice2(4444, 56.00, 8, 32, 2014);
               Invoice2 test009 = new Invoice2(5555, 56.00, 9, 31, 2014);
               Invoice2 test010 = new Invoice2(6666, 56.00, 10, 32, 2014);
               Invoice2 test011 = new Invoice2(7777, 56.00, 11, 31, 2014);
               Invoice2 test012 = new Invoice2(8888, 56.00, 12, 32, 2014);
               test001.displayInvoice();
               test002.displayInvoice();
               test003.displayInvoice();
               test004.displayInvoice();
               test005.displayInvoice();
               test006.displayInvoice();
               test007.displayInvoice();
               test008.displayInvoice();
               test009.displayInvoice();
               test010.displayInvoice();
               test011.displayInvoice();
               test012.displayInvoice();
           }
   }


/**
* File: Invoice2
* Modify the constructor in the Invoice class so that the day is not greater
* than 31, 30, or 28, depending on the month. For example, if a user tries
* to create an invoice for April 31, force it to April 30. Also, if the month
* is invalid, and thus forced to 0, also force the day to 0. Save the modified
* Invoice class as Invoice2.java.
*/

public class Invoice2
   {
       private int invoiceNumber;
       private double balanceDue;
       private int month;
       private int day;
       private int year;
       public Invoice2(int invoice, double balance, int mth, int d, int yr)
           {
               invoiceNumber = invoice;
               balanceDue = balance;
               month = mth;
               day = d;
               year = yr;
               if (invoice < 1000)
                   {
                       invoice = 0;
                       if (invoice == 0)
                           {
                               System.out.println("Invoice INVALID: ");
                           }
                   }
               if ((month == 2 && day > 28) || (month == 1 && day > 31) || (month == 3 && day > 31)
               || (month == 5 && day > 31) || (month == 7 && day > 31) || (month == 8 && day > 31)
               || (month == 10 && day > 31) || (month == 12 && day > 31) || (month == 4 && day > 30)
               || (month == 6 && day > 30) || (month == 9 && day > 30) || (month == 11 && day > 30))
                   {
                       switch (month)
                           {
                               case 1:
                               case 3:
                               case 5:
                               case 7:
                               case 8:
                               case 10:
                               case 12:
                                   day = 31;
                                   break;
                               case 2:
                                   day = 28;
                                   break;
                               case 4:
                               case 6:
                               case 9:
                               case 11:
                                   day = 30;
                                   break;
                               default:
                                   System.out.println("Month INVALID or Day, Day set to last of the month");
                           }
                   }
           }
       public void displayInvoice()
           {
               System.out.println("Invoice Number: " + invoiceNumber);
               System.out.println("Due Date: " + month + "-" + day + "-" + year);
               System.out.println("Balance Due: " + balanceDue);
           }
   }


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