Answer to Question #247205 in C# for Phamela

Question #247205

This path follow the last posted once:

Application Class The application class must have methods that do the following  Adds a new car  Sells a car  Decreases the price of a specific car by a percentage provided by the user  Displays the registration numbers and prices of all the cars that are not cheap.


1
Expert's answer
2021-10-06T10:00:34-0400
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;


namespace App
{
    class Program
    {


        class Car
        {
            private string registrationNumber;
            private string model;
            private string colour;
            private double price;


            public Car() { }


            public Car(string registrationNumber, string model, string colour, double price)
            {
                this.registrationNumber = registrationNumber;
                this.model = model;
                this.colour = colour;
                this.price = price;
            }
            /// <summary>
            ///  returns TRUE if the car is selling for under R10000.00
            /// </summary>
            /// <returns></returns>
            public bool isCheap()
            {
                return (price < 10000.00);
            }
            /// <summary>
            /// decreases the selling price by Amount 
            /// </summary>
            /// <param name="Amount"></param>
            public void decreasePrice(double Amount)
            {
                this.price -= Amount;
            }
            /// <summary>
            /// Displays the Registration Number and Price of a car Class CarList.
            /// It must contain the standard methods you would expect in a list class.
            /// </summary>
            public void Display()
            {
                Console.WriteLine("Registration Number: {0}", registrationNumber);
                Console.WriteLine("Price of a car: {0}", price);
            }






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








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




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




            public double Price
            {
                get { return price; }
                set { price = value; }
            }






        }


        static int getCarIndex(ArrayList cars, string registrationNumber)
        {
            for (int i = 0; i < cars.Count; i++)
            {
                Car car = (Car)(cars[i]);
                if (car.RegistrationNumber.CompareTo(registrationNumber) == 0)
                {
                    return i;
                }
            }
            return -1;
        }
        static void Main(string[] args)
        {
            ArrayList cars = new ArrayList();
            const string FILE_NAME = "Cars.txt";
            if (File.Exists(FILE_NAME))
            {
                //Read the file "Cars.txt"
                string[] lines = File.ReadAllLines(FILE_NAME);
                for (int i = 0; i < lines.Length; i++)
                {
                    string[] values = lines[i].Split(',');
                    cars.Add(new Car(values[0], values[1], values[2], double.Parse(values[3])));
                }
            }




            int ch = -1;
            string registrationNumber;
            string model;
            string colour;
            double price;
            while (ch != 5)
            {
                //Adds a new car
                //Sells a car 
                //Decreases the price of a specific car by a percentage provided by the user 
                //Displays the registration numbers and prices of all the cars that are not cheap.
                Console.WriteLine("1. Add a new car");
                Console.WriteLine("2. Sell a car");
                Console.WriteLine("3. Decrease the price of a specific car by a percentage provided by the user");
                Console.WriteLine("4. Displays the registration numbers and prices of all the cars that are not cheap");
                Console.WriteLine("5. Exit");
                Console.Write("Your choice: ");
                ch = int.Parse(Console.ReadLine());


                switch (ch)
                {
                    case 1:
                        {
                            Console.Write("Enter a car registration number: ");
                            registrationNumber = Console.ReadLine();
                            Console.Write("Enter a car model: ");
                            model = Console.ReadLine();
                            Console.Write("Enter a car colour: ");
                            colour = Console.ReadLine();
                            Console.Write("Enter a car price: ");
                            price = double.Parse(Console.ReadLine());
                            cars.Add(new Car(colour, model, colour, price));
                            saveCars(cars, FILE_NAME);
                            Console.WriteLine("\nA new car has been added.\n");
                        }
                        break;
                    case 2:
                        {
                            Console.Write("Enter a car registration number to sell: ");
                            registrationNumber = Console.ReadLine();
                            int index = getCarIndex(cars,registrationNumber);
                            if (index != -1)
                            {
                                cars.RemoveAt(index);
                                saveCars(cars, FILE_NAME);
                                Console.WriteLine("\nA new car has been sold.\n");
                            }
                            else {
                                Console.WriteLine("\nA car does not exist.\n");
                            }
                            
                        }
                        break;
                    case 3:
                        {
                            Console.Write("Enter a car registration number to decrease the price: ");
                            registrationNumber = Console.ReadLine();
                            int index = getCarIndex(cars, registrationNumber);
                            if (index != -1)
                            {
                                Car car=((Car)(cars[index]));
                                double percentage = -1;
                                while (percentage < 0 || percentage > 100) {
                                    Console.Write("Enter a percentage[0-100]: ");
                                    percentage = double.Parse(Console.ReadLine());
                                }
                                double amount = car.Price * percentage / 100.0;
                                car.decreasePrice(amount);
                                saveCars(cars, FILE_NAME);
                                Console.WriteLine("\nA car price has been decreased.\n");
                            }
                            else
                            {
                                Console.WriteLine("\nA car does not exist.\n");
                            }
                            
                        }
                        break;
                    case 4:
                        {
                            for (int i = 0; i < cars.Count; i++)
                            {
                                Car car = (Car)(cars[i]);
                                if (!car.isCheap()) {
                                    car.Display();
                                }
                                
                            }
                        }
                        break;
                    case 5:
                        break;
                }
            }


        }


        static void saveCars(ArrayList cars, string fileName)
        {
            //Save all cars to the file "Cars.txt"
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                for (int i = 0; i < cars.Count; i++)
                {
                    Car car = (Car)(cars[i]);
                    writer.WriteLine(car.RegistrationNumber + "," + car.Model + "," + car.Colour + "," + car.Price.ToString());
                }
            }
        }


    }
}

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
New on Blog
APPROVED BY CLIENTS