Bookstore: Kotlin a simple program
Bookstore a Kotlin example code
LEVEL: BEGINNER
This example is good for understanding
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-
Happy coding
LEVEL: BEGINNER
This example is good for understanding
- Array
- methods
- conditions(if and else)
- variable declaration
The program features are-
- First, we welcome to our customer to our bookstore
- Then we ask the user to which book do you want?
- If we found that book we show a price about the book.
- Then we say to our customer that we have a discount for student and teacher only.
- we ask the customer about his profession.
- If our customer's profession is student and teachers then we give different discount
- 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
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 = .15fNow 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
No comments :