Answer to Question #260719 in C# for Phamela

Question #260719

This task is based on your prac 3, where you needed to implement a program for a car salesman who wants to keep record of the cars he has for sale. For each car he keeps the following: Registration Number (always unique), Model, Colour and Price You can carry on with your own solution, and update it to do the following:  Cars should be classified as cheap if the selling price is < R50 000  Make use of the CarPracSkeletonCode file, to implement the methods in CarList related to sorting. Test whether your sorting is valid by calling the public sorting methods from the application class. Alternatively, you can start a new Console application, create the required classes and copy the partial code from the CarPrac SkeletonCode document to the relevant classes  Then implement the methods in Carlist related to sorting. Test whether your sorting is valid by calling the public sorting methods from the application class. 


1
Expert's answer
2021-11-03T11:43:56-0400
using System;
using System.Collections.Generic;


namespace App
{
    class Car
    {
        private string registrationNumber;


        public string 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(string 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 + 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()
        {


        }


        public void addCar(Car car)
        {
            this.cars.Add(car);
        }


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


        public void sort()
        {
            cars.Sort((x, y) => x.Price.CompareTo(y.Price));
        }


    }


    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.sort();
            Console.WriteLine("Sorted CarList");
            carList.display();


            Console.ReadLine();
        }




    }
}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS