Untitled

 avatar
user_7995305297
plain_text
3 years ago
6.1 kB
7
Indexable
fun main(args:Array<String>){

    var carType = arrayListOf<String>()
    var carModel = arrayListOf<String>()
    var carYear = arrayListOf<String>()
    var carPrice = arrayListOf<Int>()
    var carKilometer = arrayListOf<Int>()
    var chassisNumber = hashMapOf<Int,String>()

    var carKilometerReg = Regex("([0-9])")
    var carYearReg = Regex("([0-9]{4})")
    var carTypeReg = Regex("([a-zA-Z]{1,20})")

    var counter = 0
    var prices =0

    print("How many cars would you like to add?")
    var i = readLine()!!.toInt()

    while (counter < i)
    {
        print("Enter the type of the car:")
        carType[i] = readLine()!!.toString()

        while (!carTypeReg.matches(carType[i])) {
            print("Enter only letters:")
            carType[i] = readLine()!!.toString()
        }

        print("Enter the model of the car:")
        carModel[i] = readLine()!!.toString()

        print("Enter the year of the car:")
        carYear[i] = readLine()!!.toString()

        while (!carYearReg.matches(carYear[i])) {
            print("Enter only 4 numbers:")
            carYear[i] = readLine()!!.toString()
        }

        print("Enter the price of the car:")
        carPrice[i] = readLine()!!.toInt()
        prices += carPrice[i]

        print("Enter how many kilometers the car have gone:")
        carKilometer[i] = readLine()!!.toInt()

        while (!carKilometerReg.matches(carKilometer[i].toString())) {
            print("Enter only numbers:")
            carKilometer[i] = readLine()!!.toInt()
        }

        print("Enter the chassis number of the car:")
        chassisNumber.put(i,readLine()!!.toString())

        i++
    }

    var condition = 1
    while (condition != 0) {
        println(
            "Enter 1 to print all cars, enter 2 to retrive a car by chassis number, enter 3 to display cars by price, " +
                    "enter 4 to display cars by kilometers, enter 5 to apply discount and display cars prices before and after discount, " +
                    "enter 6 to display number of availabe cars in the showroom, enter 7 to display sum of cars price, " +
                    "enter 8 to display cars by model, enter 9 to search for a car, enter 0 to close "
        )
        var option = readLine()!!.toInt()

        when (option) {

            0 -> break

            1 -> for (i in 0 until carType.size) {
                println("Car Type: " + carType[i] + "\n Car model: " + carModel[i] + "\n Car year: " + carYear[i] + "\n Car price: " + carPrice[i] + "\n Car odometer: " + carKilometer[i] + "\n Car chassis number: " + chassisNumber.get(i))
            }

            2 -> {
                println("Enter the car chassis number: ")
                var carChassis = readLine()!!.toString()
                for (i in 0 until chassisNumber.size)
                    if (carChassis.equals(chassisNumber[i]))
                        println("Car Type: " + carType[i] + "\n Car model: " + carModel[i] + "\n Car year: " + carYear[i] + "\n Car price: " + carPrice[i] + "\n Car odometer: " + carKilometer[i] + "\n Car chassis number: " + chassisNumber.get(i))
            }
            3 -> {
                println("Enter the price from: ")
                var from = readLine()!!.toInt()
                println("Enter the price to: ")
                var to = readLine()!!.toInt()

                for (i in 0 until carPrice.size) {
                    if (from <= carPrice[i] && to >= carPrice[i])
                        println("Car type: " + carType[i] + "\n Car model: " + carModel[i] + "\n Car year: " + carYear[i] + "\n Car price: " + carPrice[i] + "\n Car odometer: " + carKilometer[i] + "\n Car chassis number: " + chassisNumber.get(i))
                }
            }
            4 -> {
                println("Enter the kilometer from:")
                var KilometerFrom = readLine()!!.toInt()
                println("Enter the kilometer to:")
                var KilometerTo = readLine()!!.toInt()

                for (i in 0 until carKilometer.size)
                    if (carKilometer[i] in KilometerFrom..KilometerTo)
                        println("Car type: " + carType[i] + "\n Car model: " + carModel[i] + "\n Car year: " + carYear[i] + "\n Car price: " + carPrice[i] + "\n Car odometer: " + carKilometer[i] + "\n Car chassis number: " + chassisNumber.get(i))
            }
            5 -> {
                println("Enter the discount: ")
                var Discount = readLine()!!.toInt()
                for (i in 0 until carType.size) {
                    println(
                        "Car type: " + carType[i] + "\n Car model: " + carModel[i] + "\n Car year: " + carYear[i] + "\n" + "Price before discount: " + carPrice[i] + "\n" +
                                "Price after discount: " + (carPrice[i] * Discount / 100 - carPrice[i]))
                }
            }

            6 -> println("Total cars in showroom is: " + i)

            7 -> println("Total prices of all cars is: " + prices)

            8 -> {
                println("Enter the year from:")
                var YearFrom = readLine()!!.toInt()
                println("Enter the year to:")
                var YearTo = readLine()!!.toInt()

                for (i in 0 until carYear.size)
                    if (carYear[i] in YearFrom.toString()..YearTo.toString())
                        println("Car type: " + carType[i] + "\n Car model: " + carModel[i] + "\n Car year: " + carYear[i] + "\n Car price: " + carPrice[i] + "\n Car odometer: " + carKilometer[i] + "\n Car chassis number: " + chassisNumber.get(i))

            }

            9-> {
                println("Enter the car Type: ")
                var cartype = readLine()!!.toString()
                for (i in 0 until carType.size)
                    if (cartype.equals(carType[i]))
                        println("Car Type: " + carType[i] + "\n Car model: " + carModel[i] + "\n Car year: " + carYear[i] + "\n Car price: " + carPrice[i] + "\n Car odometer: " + carKilometer[i] + "\n Car chassis number: " + chassisNumber.get(i))

            }
            }
        }
    }
Editor is loading...