Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object GeometricShapes extends App {
- val inputHeight = scala.io.StdIn.readLine("Please insert the height of your shape:")
- val validHeight = validInput(inputHeight)
- println(s" höhe: $validHeight")
- val inputWidth = scala.io.StdIn.readLine("Please insert the width of your shape:")
- val validWidth = validInput(inputWidth)
- println(s" höhe: $validWidth")
- val shape = scala.io.StdIn.readLine("Enter the type of your shape (rectangle,triangle or circle):")
- val shape1 = shape.trim()
- shapes(validHeight , validWidth, shape1)
- def validInput(input: String): Double = {
- val pattern = """\s*(\d+[.,]?\d*)\s*(mm|cm|dm|m)\s*""".r
- input match {
- case pattern(value, unit) => {
- val replacedValue = value.replace(",", ".")
- if (unit == "mm") return replacedValue.toDouble / 10.0
- else if (unit == "dm") return replacedValue.toDouble * 10
- else if (unit == "m") return replacedValue.toDouble * 100.0
- else return replacedValue.toDouble
- }
- case _ => {
- println("This is not a valid dimension! Use a number followed by either \"m\",\"dm\",\"cm\" or \"mm\".")
- System.exit(0)
- return 0.0}
- }
- }
- def shapes(height: Double, width: Double, shape: String): Any = {
- shape match {
- case "rectangle" => println("The area of your rectangle with height= %.2fcm and width=%.2fcm is: %.2f cm2".format(height, width, width*height))
- case "triangle" => println("The area of your triangle with height= %.2fcm and width=%.2fcm is: %.2f cm2".format(height, width, ((width*height)/2).toDouble))
- case "circle" => {
- if(height == width) println("The area of your circle with height= %.2fcm and width=%.2fcm is: %.2f cm2".format(height, width, height*height*math.Pi))
- else {
- println("Height and width of a circle need to be equal!")
- System.exit(0)
- }
- }
- case wrong => println("Shape \"%s\" is not supported. Please use \"rectangle\",\"triangle\" or \"circle\"".format(shape))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement