Answer to Question #247203 in C# for Phamela

Question #247203

You need 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 Implement the following classes Class Car It must have a constructor, the necessary Get and Set methods, as well as the following methods: public boolean isCheap() // returns TRUE if the car is selling for under R10000.00 public void decreasePrice(double Amount) // decreases the selling price by Amount public void Display() // displays the Registration Number and Price of a car Class CarList It must contain the standard methods you would expect in a list class. The class must be able to interact with a data file (in other words, get initial data from a data file, and write the final data to a data file), and uses ArrayLists to implement a list


1
Expert's answer
2021-10-06T04:04:32-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 void Main(string[] args)
        {
            ArrayList cars = new ArrayList();


            cars.Add(new Car("45643113","XS5","Red",50000));
            cars.Add(new Car("78465465", "X1", "Black",12000));
            cars.Add(new Car("13246451", "S5", "Blue", 5000));


            //Save all cars to the file "Cars.txt"
            using (StreamWriter writer = new StreamWriter("Cars.txt"))
            {
                for (int i = 0; i < cars.Count; i++) {
                    Car car = (Car)(cars[i]);
                    writer.WriteLine(car.RegistrationNumber+","+car.Model+","+car.Colour+","+car.Price.ToString());
                }
            }
            cars.Clear();
            //Read the file "Cars.txt"
            string[] lines = File.ReadAllLines("Cars.txt");
            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])));
            }


            //Display to the file "Cars.txt"
            for (int i = 0; i < cars.Count; i++)
            {
                Car car = (Car)(cars[i]);
                car.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