ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[Kotlin Spring boot] A simple demo app

2020-11-05 03:31:27  阅读:216  来源: 互联网

标签:val simple Spring Seat num Kotlin import com row


Structure:

 

tempalates/seatBooking.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>com.virtualpairprogrammers.theater.services.TheaterService Booking</title>
</head>
<body>
    <h1>com.virtualpairprogrammers.theater.services.TheaterService Booking</h1>
<h2>Seat availability</h2>

    <form action="/checkAvailability" th:object="${bean}" method="POST">
    <p>Check if seat number
        <select th:field="*{selectedSeatNum}">
            <option th:each="n : ${bean.seatNums}" th:value="${n}" th:text="${n}" />
        </select>
         in row
        <select th:field="*{selectedSeatRow}">
            <option th:each="r : ${bean.seatRows}" th:value="${r}" th:text="${r}" />
        </select>
        is available <input type="submit"/></p>

    </form>

    <p th:text="${bean.result}"></p>
</body>
</html>

 

Control/MainController:

package com.virtualpairprogrammers.theater.control

import com.virtualpairprogrammers.theater.services.BookingService
import com.virtualpairprogrammers.theater.services.TheaterService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.servlet.ModelAndView

@Controller
class MainController {

    @Autowired
    lateinit var theaterService: TheaterService

    @Autowired
    lateinit var bookingService: BookingService

    @RequestMapping("")
    fun homePage() : ModelAndView {
        return ModelAndView("seatBooking", "bean", CheckAvailabilityBackingBean())
    }

    @RequestMapping("checkAvailability", method= arrayOf(RequestMethod.POST))
    fun checkAvailability(bean: CheckAvailabilityBackingBean): ModelAndView {
        val selectedSeat = theaterService.find(bean.selectedSeatNum, bean.selectedSeatRow)
        val result =  bookingService.isSeatFree(selectedSeat)
        bean.result = "Seat $selectedSeat is " + if (result) "available" else "booked"
        return ModelAndView("seatBooking", "bean", bean)
    }
}

/*
* A typical JavaServer Faces application includes one or more backing beans,
* each of which is a type of JavaServer Faces managed bean that can be associated with
* the components used in a particular page.
* This section introduces the basic concepts of creating, configuring,
* and using backing beans in an application.
* */
class CheckAvailabilityBackingBean() {
    val seatNums = 1..36
    val seatRows = 'A'..'O'
    // below must be var because those data are sent back and forward between
    // server and template
    var selectedSeatNum: Int = 1
    var selectedSeatRow: Char = 'A'
    var result: String = ""
}

 

domain/Seat:

package com.virtualpairprogrammers.theater.domain

import java.math.BigDecimal

data class Seat(val row: Char, val num: Int, val price: BigDecimal, val description: String) {
    override fun toString(): String = "Seat $row-$num $$price ($description)"
}

 

services/TheaterService:

package com.virtualpairprogrammers.theater.services

import com.virtualpairprogrammers.theater.domain.Seat
import org.springframework.stereotype.Service
import java.math.BigDecimal


@Service
class TheaterService {

    private val hiddenSeats = mutableListOf<Seat>()

    constructor() {

        fun getPrice(row: Int, num: Int) : BigDecimal {
            return when {
                row >=14 -> BigDecimal(14.50)
                num <=3 || num >= 34 -> BigDecimal(16.50)
                row == 1 -> BigDecimal(21)
                else -> BigDecimal(18)
            }

        }

        fun getDescription(row: Int, num: Int) : String {
            return when {
                row == 15 -> "Back Row"
                row == 14 -> "Cheaper Seat"
                num <=3 || num >= 34 -> "Restricted View"
                row <=2 -> "Best View"
                else -> "Standard Seat"
            }
        }

        for (row in 1..15) {
            for (num in 1..36) {
                hiddenSeats.add(Seat((row+64).toChar(), num, getPrice(row,num), getDescription(row,num) ))
            }
        }
    }

    val seats
    get() = hiddenSeats.toList()


    fun find(num: Int, row: Char): Seat {
        return seats.filter {it.row === row && it.num === num }.first()
    }
}

 

services/BookingService:

package com.virtualpairprogrammers.theater.services

import com.virtualpairprogrammers.theater.domain.Seat
import org.springframework.stereotype.Service

@Service
class BookingService() {

    fun isSeatFree(seat: Seat): Boolean {
        return true
    }
}

 

标签:val,simple,Spring,Seat,num,Kotlin,import,com,row
来源: https://www.cnblogs.com/Answer1215/p/13929638.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有