We need a fallback for wrong wallpapers

Report a typo

We already have two methods in our code that change the wallpaper on the desktop:

def executeChangingWallpaper(picture: String): Unit

def executeDefaultWallpaper(): Unit

The first method can be called only with a correct image file. We have written a function to check the file format, but we did not have time to handle scenarios with bad input.

Refine the function using the getOrElse method and executeDefaultWallpaper. Note that the executeDefaultWallpaper method should not be called if the passed wallpaper is correct.

Sample Input 1:

wallpaper.png

Sample Output 1:

wallpaper was changed to wallpaper.png
Write a program in Scala 3
def setWallpaper(wallpaper: String): Unit = {
val pattern: Regex = "^([a-zA-Z][0-9a-zA-Z]*.png)$".r
val maybeCorrectWallpaper = pattern.findFirstIn(wallpaper)

executeChangingWallpaper(maybeCorrectWallpaper.get) // todo: None case
}
___

Create a free account to access the full topic