عبدالله الحربي

 avatar
unknown
plain_text
2 years ago
4.8 kB
5
Indexable
fun main() {
    // this variable will be used to stop the program
    var running = true
    // this variable will be used to stop the inner loop
    var inner = true

    // this variable will be used to know which command to use
    var command = "m";
    /**
     * commands:
     *
     * ac         -> add new car
     * da         -> display all cars
     * do         -> display cars with order by (from, to)
     * dk         -> display cars with order by kilometers (from, to)
     * md         -> make discount
     * cc         -> count all cars
     * dp         -> display car prices
     * dy         -> display cars with order by release date
     * s         -> search by car name
     */


    val commands = arrayListOf(
        "add new car",
        "display all cars",
        "display cars by price (from to)",
        "display cars by kilometers (from, to)",
        "make discount",
        "show total number of cars",
        "display car prices",
        "display cars by release date",
        "search for a car by name",
        "Exit",
    )

    val commandsCode = arrayListOf(
        "ac",
        "da",
        "do",
        "dk",
        "md",
        "cc",
        "dp",
        "dy",
        "s",
        "e"
    )

    var cars = arrayListOf<String>()
    var releaseDate = arrayListOf<Int>()
    var prices = arrayListOf<Double>()
    var kilometers = arrayListOf<Int>()

    val numberReg = Regex("\\d")


    var currentCommandCode = ""

    while(running) {
        currentCommandCode = ""
        print("\nEnter the number of the command:\n")

        for (i in 0 until commands.size) {
            val commandId = (i+1);
            print("$commandId- "+commands[i] + "\n")
        }

        val commandIdStr = readLine()!!.toString()


        if(!numberReg.matches(commandIdStr)) {
            println("invalid value, please enter a number.")
            return
        }

        val commandId = commandIdStr.toInt()

        val commandRange = 1..10
        if(!(commandId in commandRange)) {
            println("please enter a number one of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10")
            return
        }
        val commandName = commands[commandId - 1]
        val commandCode = commandsCode[commandId - 1]

        println("you choose: $commandName at $commandCode")
        currentCommandCode = commandCode



        if(currentCommandCode == "ac") {
            println("\n-- Add new cars --")

            var numberOfCars:Int = 0

            while(inner) {
                print("How many cars you want to add ? ")
                val numberOfCarsStr = readLine()!!.toString()

                if(!numberReg.matches(numberOfCarsStr)) {
                    println("invalid value, please enter a number")
                    continue
                }

                numberOfCars = numberOfCarsStr.toInt()
                break
            }







            for (i in 1..numberOfCars) {
                print("Enter #$i Car name: ")
                val carName = readLine()!!.toString()
                cars.add(carName)

                print("Enter price for car [$carName#$i]: ")
                val carPriceStr = readLine()!!.toString()

                // only accept a decimal
                val carPriceReg = Regex("[0-9]*\\.?[0-9]*")
                if(!carPriceReg.matches(carPriceStr)) {
                    println("please enter a valid number")
                    return
                }

                val carPrice = carPriceStr.toDouble()
                prices.add(carPrice)

                print("Enter the release date for car [$carName#$i]: ")
                val releaseDateStr = readLine()!!.toString()
                val releaseDateReg = Regex("\\d{4}")

                if(!releaseDateReg.matches(releaseDateStr)) {
                    println("please enter a valid release year like 2022.")
                    return
                }

                val releaseDateDate = releaseDateStr.toInt()
                releaseDate.add(releaseDateDate)

                // ---

                print("Enter the kilometers for car [$carName#$i]: ")
                val kiloStr = readLine()!!.toString()
                val kiloReg = Regex("\\d")

                if(!kiloReg.matches(kiloStr)) {
                    println("please enter a valid kilo like 150.")
                    return
                }

                val kiloInt = releaseDateStr.toInt()
                kilometers.add(kiloInt)

            }

        }

        if(commandName == "da") {
            for (i in 1 until cars.size) {
                val currentCarName = cars[i]
                val price = prices[i]
                val release = releaseDate[i]
                val kilo = kilometers[i]
                println("#$i - $currentCarName $")
            }
        }
    }
}
Editor is loading...