59.4k views
5 votes
Using the SalesTaxDemo program you wrote in Exercise 6A, modify that program so that after the 10 Sale objects are displayed, they are sorted in order by the amount of tax owed and displayed again. Modify the Sale class so its objects are comparable to each other based on tax owed using IComparable.CompareTo(). Name this modified program SalesTaxDemo2.

Each item should be displayed as follows:
Sale # 1 Amount: 567 Sale $1.00
Tax is $0.08
In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));
This is my code:
using System;
namespace SalesTaxDemo2
{
class Sale : IComparable
{
private string _inventoryNumber;
public string InventoryNumber
{
get { return this._inventoryNumber; }
set { this._inventoryNumber = value; }
}
private double _amountOfSale;
public double AmountOfSale
{
get { return this._amountOfSale; }
// When amount of sale is set, calculate the Taz as well
set { this._amountOfSale = value; this.SetTax(); }
}
private double _taxOwned;
public double TaxOwned { get { return this._taxOwned; } }
///
/// Calculate Tax based on Amount of sale
///
private void SetTax()
{

// If less than 100, 8%

if (this.AmountOfSale <= 100)

{
this._taxOwned = this.AmountOfSale * 0.08;
}
// first $100 for 8% and remaning for 6%
else
{
this._taxOwned = 100 * 0.08 + ((this.AmountOfSale - 100) * 0.06);
}
}
public int CompareTo(object obj)
{
if (obj == null)

{

return 1;

}

// Cast to Sale object
Sale other = obj as Sale;
if (other == null)
{
throw new ArgumentException("A Sale object is required for comparison.", "obj");

}

// Compare the TaxOwned

return this.TaxOwned.CompareTo(other.TaxOwned);

}

}

}

Program.cs File:

using System;

using System.Globalization;

namespace SalesTaxDemo2

{

class Program

{

static void Main(string[] args)

{

// Create objects of 10 sale

Sale[] sales = new Sale[10];

// Loop through and get data for all the objects

for(int index = 0; index < sales.Length; index++)

{

// Cret=ate new sale

sales[index] = new Sale();

//get inventory number

Console.Write("Enter Inventory number #" + (index + 1) + " " );

sales[index].InventoryNumber = Console.ReadLine();

// Get amount of sale

Console.Write("Enter amount of sale: ");

sales[index].AmountOfSale = double.Parse(Console.ReadLine());

}

// Display all the sales

for (int index = 0; index < sales.Length; index++)

{

Console.WriteLine("Sale # {0} Amount: {1} Sale {2}\\ Tax is {3}", (index+1), sales[index].InventoryNumber, sales[index].AmountOfSale.ToString("C", CultureInfo.GetCultureInfo("en-US")),

sales[index].TaxOwned.ToString("C", CultureInfo.GetCultureInfo("en-US")) );

}

// Sort the sales

Array.Sort(sales);

Console.WriteLine("\\After sorting:");

// Display all the sales

for (int index = 0; index < sales.Length; index++)

{

Console.WriteLine("Sale # {0} Amount: {1} Sale {2}\\ Tax is {3}", (index + 1), sales[index].InventoryNumber, sales[index].AmountOfSale.ToString("C", CultureInfo.GetCultureInfo("en-US")),

sales[index].TaxOwned.ToString("C", CultureInfo.GetCultureInfo("en-US")));

}

Console.ReadKey();

}

}

}

}

}

Can someone help me with this?

User AndrewPK
by
7.9k points

1 Answer

5 votes

Final answer:

The provided code already includes the necessary modifications to sort the Sale objects based on the amount of tax owed.

Step-by-step explanation:

The provided code already includes the necessary modifications to sort the Sale objects based on the amount of tax owed. The modification involves implementing the IComparable interface and overriding the CompareTo method in the Sale class. This allows the Array.Sort method to sort the Sale objects based on the tax owed. Additionally, the program uses CultureInfo.GetCultureInfo method to format the output statements by adding the $ symbol to currency values. The Sort the sales with Array.Sort(sales) and then display the sorted sales using a for loop.

User Cyberflohr
by
8.3k points

No related questions found