Create a class called Invoice with the properties (Part number, Part Description, Quantity and Price).
Create appropriate methods and data types.
Use the class Invoice and create an array of Invoice objects (Part number, Part Description, Quantity and Price) initialized as shown below:
// initialize array of invoices
Invoice[] invoices = {
new Invoice( 83, "Electric sander", 7, 57.98M ),
new Invoice( 24, "Power saw", 18, 99.99M ),
new Invoice( 7, "Sledge hammer", 11, 21.5M ),
new Invoice( 77, "Hammer", 76, 11.99M ),
new Invoice( 39, "Lawn mower", 3, 79.5M ),
new Invoice( 68, "Screwdriver", 106, 6.99M ),
new Invoice( 56, "Jig saw", 21, 11M ),
new Invoice( 3, "Wrench", 34, 7.5M )
};
Write a console application that displays the results:
a) Use a Selection sort to sort the Invoice objects by PartDescription in ascending order.
b) Use an Insertion sort to sort the Invoice objects by Price in descending.
c) Calculate the total amount for each invoice amount (Price * Quantity)
d) Display the description and totals in ascending order by the totals.
Sorted by description ascending order:
83 Electric sander 7 $57.98
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
39 Lawn mower 3 $79.50
24 Power saw 18 $99.99
68 Screwdriver 106 $6.99
7 Sledge hammer 11 $21.50
3 Wrench 34 $7.50
Sorted by price in descending order:
24 Power saw 18 $99.99
39 Lawn mower 3 $79.50
83 Electric sander 7 $57.98
7 Sledge hammer 11 $21.50
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
3 Wrench 34 $7.50
68 Screwdriver 106 $6.99
Here is what I have so far. I am getting so many errors though. I am new to c++ so please explain or help in any way possible.
#include<iostream.h>
int main()
{
class Invoice
{
int p,r,s;
char[] q=new char();
Invoice(int p,char q,int r,int s)
{
this.p=p;
this.q=q;
this.r=r;
this.s=s;
}
};
Invoice[] invoices = {
new Invoice( 83, "Electric sander", 7, 57.98M ),
new Invoice( 24, "Power saw", 18, 99.99M ),
new Invoice( 7, "Sledge hammer", 11, 21.5M ),
new Invoice( 77, "Hammer", 76, 11.99M ),
new Invoice( 39, "Lawn mower", 3, 79.5M ),
new Invoice( 68, "Screwdriver", 106, 6.99M ),
new Invoice( 56, "Jig saw", 21, 11M ),
new Invoice( 3, "Wrench", 34, 7.5M )
};
void SelectionSort(Invoice &invoice)
{
int i, j, first;
char temp;
int invoiceLength =invoice.length( );
for (i= invoiceLength - 1; i > 0; i--)
{
first = 0; // initialize to subscript of first element
for (j=1; j<=i; j++) // locate smallest between positions 1 and i.
{
if (invoice[j].q < invoice[first].q)
first = j;
}
temp = invoice[first].q; // Swap smallest found with element in position i.
invoice[first].q = invoice[i].q;
invoice[i].q = temp;
}
for(i=0;i<invoiceLength;i++)
cout<<" "<<invoice[i].p<<" "<<invoice[i].q<<" "<<invoice[i].r<<" "<<invoice[i].s;
return;
}
void insertion_sort()
{
int key,i;
for(int j=1;j<invoice.length;j++)
{
key=invoice[j].s;
i=j-1;
while(invoice[i].s>key && i>=0)
{
invoice[i+1].s=invoice[i].s;
i--;
}
invoice[i+1].s=key;
}
}
for(i=0;i<invoice.length;i++)
cout<<" "<<invoice[i].p<<" "<<invoice[i].q<<" "<<invoice[i].r<<" "<<invoice[i].s;
}
void total()
{
for(i=0;i<invoice.length;i++)
cout<<" "<<invoice[i].p*invoice[i].r<<endl;
}
return 0;
}