Thursday, June 15, 2017

Bookstore: Kotlin a simple program

Bookstore a Kotlin example code
LEVEL: BEGINNER

kotlin


This example is good for understanding
  1. Array
  2. methods
  3. conditions(if and else)
  4. variable declaration
The program features are-
  1. First, we welcome to our customer to our bookstore
  2. Then we ask the user to which book do you want?
  3. If we found that book we show a price about the book.
  4. Then we say to our customer that we have a discount for student and teacher only.
  5. we ask the customer about his profession.
  6. If our customer's profession is student and teachers then we give different discount
  7. After that, we show the discount prices and give a thank message
That's our today's main target.

I also a publish a post about the same program in Java you can also see that post.(Click here)

First, we declare an array
//books name
val booksName = arrayOf("java","c","c++","kotlin","c#","html")
Now add a scanner that's scan user input
val scanner = Scanner(System.`in`)
Now add two variable for two type of discount
val studentDiscount = .25f
val teacherDiscount = .15f
Now time to welcome to our customer and get customer input
println("*************WELCOME TO OUR BOOK STORE**********")
println("Which book do you want?")
print("Answer: ")
val book = scanner.nextLine()
Now create a method that calculates price after discount-
private fun calculatePrice(x: Float): Unit {
    var price = 200f
    price -= price * x

    println("Your total payable price is $price TK.")
}
Time to write some conditions against user input-
if (booksName.contains(book.toLowerCase())){
        println("You Want $book book. price 200 Tk.")
        println("we have some discount for student and teacher.\nWhat is your occupation?")
        print("Answer: ")
        val occupation = scanner.nextLine()

        when(occupation.toLowerCase()){
            "student" -> {
                calculatePrice(studentDiscount)
            }

            "teacher" ->{
                calculatePrice(teacherDiscount)
            }

            else -> println("Sorry you don't get any discount."+
                    "\nYour total payable price is 200 TK.")
        }

    } else{
        println("Sorry we don't have $book book")
    }
Code analysis-
1.If we have customer desire book that we show a price 200 and If we don't then we print Sorry we don't have that book.
2.If we found the book then we ask again user about his/her occupation
3.If he/she is a student or teacher then we give some discount
4.If the student then we call to calculate the price and pass the student discount value or If the teacher, then we call to calculate the price and pass the teacher discount value.

Full code-
package firstKotlin

import java.util.*

fun main(arg: Array<String>){

    //books name
    val booksName = arrayOf("java","c","c++","kotlin","c#","html")

    val scanner = Scanner(System.`in`)

    val studentDiscount = .25f
    val teacherDiscount = .15f

    println("*************WELCOME TO OUR BOOK STORE**********")
    println("Which book do you want?")
    print("Answer: ")
    val book = scanner.nextLine()

    if (booksName.contains(book.toLowerCase())){
        println("You Want $book book. price 200 Tk.")
        println("we have some discount for student and teacher.\nWhat is your occupation?")
        print("Answer: ")
        val occupation = scanner.nextLine()

        when(occupation.toLowerCase()){
            "student" -> {
                calculatePrice(studentDiscount)
            }

            "teacher" ->{
                calculatePrice(teacherDiscount)
            }

            else -> println("Sorry you don't get any discount."+
                    "\nYour total payable price is 200 TK.")
        }

    } else{
        println("Sorry we don't have $book book")
    }
}

private fun calculatePrice(x: Float): Unit {
    var price = 200f
    price -= price * x

    println("Your total payable price is $price TK.")
}
Thanks for reading.
Happy coding

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :