107 286
Assignments Done
97.9%
Successfully Done
In March 2023

# Answer to Question #260720 in C# for Phamela

Question #260720

This part continues with last one:

Now add the following functionality  Add a SortedState variable to your CarList class. SortedState = 1 if the list is sorted in ascending order of price, SortedState = 2 if the list in sorted in ascending order of Registration number and SortedState = 0 otherwise.  Add a BinarySearch method to your list class (based on the Registration number). Adapt the existing Find method (in the list class) to use the BinarySearch method when the list is sorted in ascending order of Registration number, otherwise the LinearSearch method should be used.  Use a sorting algorithm effectively in a new method in the application class that displays the details of the cheapest car.  Change the add method in CarList to keep the list sorted in ascending order on price even when a new car is added to the list

1
2021-11-03T17:31:00-0400
using System;
using System.Collections.Generic;

namespace App
{
class Car
{
private int registrationNumber;

public int RegistrationNumber
{
get { return registrationNumber; }
set { registrationNumber = value; }
}
private string model;

public string Model
{
get { return model; }
set { model = value; }
}
private string colour;

public string Colour
{
get { return colour; }
set { colour = value; }
}
private double price;

public double Price
{
get { return price; }
set { price = value; }
}
public Car(int registrationNumber, string model, string colour, double price)
{
this.registrationNumber = registrationNumber;
this.model = model;
this.colour = colour;
this.price = price;
}
/// <summary>
///  Cars should be classified as cheap if the selling price is < R50 000
/// </summary>
/// <returns></returns>
public bool isCheap()
{
if (this.price < 50000)
{
return true;
}
return false;
}

public override string ToString()
{
return "Registration number: " + registrationNumber.ToString() + Environment.NewLine +
"Model: " + model + Environment.NewLine +
"Colour: " + colour + Environment.NewLine +
"Price: R" + price.ToString() + Environment.NewLine;
}
}

class CarList
{

private List<Car> cars = new List<Car>();

public CarList()
{

}

//SortedState = 1 if the list is sorted in ascending order of price,
//SortedState = 2 if the list in sorted in ascending order of Registration
//number and SortedState = 0 otherwise.
private int sortedState;

public int SortedState
{
get { return sortedState; }
set { sortedState = value; }
}

public void Search(int registrationNumber)
{
if (SortedState == 2)
{
BinarySearch(registrationNumber);
}
else {
LinearSearch(registrationNumber);
}
}

//Registration number). Adapt the existing Find method (in the list class)
//to use the BinarySearch method when the list is sorted in ascending order of
//Registration number, otherwise the LinearSearch method should be used.
public void BinarySearch(int registrationNumber) {

int index = int.MaxValue;
int low = 0;
int high = cars.Count - 1;
while (low <= high)
{
int mid = low + ((high - low) / 2);
if (cars[mid].RegistrationNumber < registrationNumber)
{
low = mid + 1;
}
else if (cars[mid].RegistrationNumber > registrationNumber)
{
high = mid - 1;
}
else if (cars[mid].RegistrationNumber == registrationNumber)
{
index = mid;
break;
}
}

if (index != int.MaxValue)
{
Console.WriteLine(cars[index] );
}else{
Console.WriteLine("The car does not exist.");
}
}
public void LinearSearch(int registrationNumber)
{
int index=-1;
for (int i = 0; i < cars.Count; i++)
{
if (cars[i].RegistrationNumber == registrationNumber)
{
index = i;

break;
}
}
if (index != -1)
{
Console.WriteLine(cars[index]);
}
else {
Console.WriteLine("The car does not exist.");
}
}

//Use a sorting algorithm effectively in a new method in the application
//class that displays the details of the cheapest car.
//Change the add method in CarList to keep the list sorted in ascending
//order on price even when a new car is added to the list

public void displayCheapestCars() {
Console.WriteLine("The details of the cheapest cars: ");
for (int i = 0; i < cars.Count; i++)
{
if (cars[i].isCheap()) {
Console.WriteLine(cars[i].ToString() + Environment.NewLine);
}

}
}

{
}

public void display()
{
for (int i = 0; i < cars.Count; i++)
{
Console.WriteLine(cars[i].ToString() + Environment.NewLine);
}
}

public void sortByPrice()
{
cars.Sort((x, y) => x.Price.CompareTo(y.Price));
sortedState = 1;
}
public void sortByRegistrationNumber()
{
cars.Sort((x, y) => x.RegistrationNumber.CompareTo(y.RegistrationNumber));
sortedState = 2;
}
}

class Program
{

static void Main(string[] args)
{
CarList carList = new CarList();
carList.addCar(new Car(5646321, "Ford X5", "Red", 45000));
carList.addCar(new Car(7894546, "Ford X8", "Blue", 85000));
carList.addCar(new Car(8745565, "Reno X45", "Yellow", 95050));
carList.addCar(new Car(4575457, "Ford X2", "Green", 19000));
carList.addCar(new Car(2454477, "Ford S2", "Black", 25000));
carList.display();
carList.sortByPrice();
Console.WriteLine("Sorted CarList");
carList.display();

carList.sortByRegistrationNumber();
Console.WriteLine("Sorted CarList by Registration Number");
carList.display();
carList.Search(4575457);
Console.WriteLine("Sorted CarList by price");
carList.sortByPrice();
carList.Search(4575457);
carList.Search(45754552);
Console.WriteLine();
Console.WriteLine();
carList.displayCheapestCars();

}

}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!