Initial Commit

This commit is contained in:
2020-03-03 08:18:35 -05:00
commit 3585879e64
17 changed files with 214 additions and 0 deletions

2
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

10
.idea/codeStyles/Project.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>
</code_scheme>
</component>

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

10
.idea/kotlinc.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.3" />
<option name="languageVersion" value="1.3" />
</component>
</project>

19
.idea/libraries/KotlinJavaRuntime.xml generated Normal file
View File

@@ -0,0 +1,19 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime">
<CLASSES>
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-reflect.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-test.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk7.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk8.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-sources.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-reflect-sources.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-test-sources.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk7-sources.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk8-sources.jar!/" />
</SOURCES>
</library>
</component>

6
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/HelloKotlin.iml" filepath="$PROJECT_DIR$/HelloKotlin.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

12
HelloKotlin.iml Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

Binary file not shown.

Binary file not shown.

Binary file not shown.

76
src/ams.kt Normal file
View File

@@ -0,0 +1,76 @@
import java.util.*
fun main(args: Array<String>) {
feedTheFish()
}
fun shouldChangeWater(
day: String,
temp: Int = 22,
dirty: Int = 22
): Boolean {
return when {
isTooHot(temp) -> true
isDirty(dirty) -> true
isSunday(day) -> true
else -> false
}
}
fun isTooHot(temp: Int) = temp > 30
fun isDirty(dirty: Int) = dirty > 30
fun isSunday(day: String) = day == "Sunday"
fun dayOfWeek() {
println("What day is it today?")
var day: String? = null
day = when(Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
1 -> "Sunday"
2 -> "Monday"
3 -> "Tuesday"
4 -> "Wednesday"
5 -> "Thursday"
6 -> "Friday"
7 -> "Saturday"
else -> "wut?"
}
println("Today is: $day")
}
fun feedTheFish() {
val day = randomDay()
val food = fishFood(day)
print("Today is $day and the fish eat $food")
if (shouldChangeWater(day)) {
println("Change the water today")
}
}
fun fitMoreFish(
tankSize: Double,
currentFish: List<Int>,
fishSize: Int = 2,
hasDecorations: Boolean = true
): Boolean {
val tankCap = tankSize.times(if(hasDecorations) 0.8 else 1.0)
val currentTotalSize = if(currentFish.isNotEmpty()) currentFish.sum() else 0
return tankCap.minus(currentTotalSize) >= fishSize
}
fun fishFood(day: String): String {
return when(day) {
"Monday" -> "flakes"
"Tuesday" -> "pellets"
"Wednesday" -> "redworms"
"Thursday" -> "granules"
"Friday" -> "mosquitoes"
"Saturday" -> "lettuce"
"Sunday" -> "plankton"
else -> "fasting"
}
}
fun randomDay() : String {
val week = listOf<String>("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
return week[Random().nextInt(week.count())]
}

6
src/dice.kt Normal file
View File

@@ -0,0 +1,6 @@
import java.util.*
fun main(args: Array<String>) {
val roll12 = { Random().nextInt(12) + 1 }
val rollDie: (Int) -> Int = { sides -> if (sides == 0) 0 else Random().nextInt(sides) + 1 }
}

31
src/fortune.kt Normal file
View File

@@ -0,0 +1,31 @@
fun main(args: Array<String>) {
val birthday = getBirthday()
val fortune = getFortune(birthday)
println("Your fortune is: $fortune")
}
fun getBirthday(): Int {
print("Enter your birthday:")
return readLine()?.toIntOrNull() ?: 1
}
fun getFortune(birthday: Int): String {
val fortunes = listOf<String>(
"You will have a great day!",
"Things will go well for you today.",
"Enjoy a wonderful day of success.",
"Be humble and all will turn out well.",
"Today is a good day for exercising restraint.",
"Take it easy and enjoy life!",
"Treasure your friends because they are your greatest fortune."
)
val index = when(birthday) {
in 1..5 -> 4
6, 22 -> 5
else -> birthday.rem(fortunes.size)
}
return fortunes[index]
}

23
src/whatToDoToday.kt Normal file
View File

@@ -0,0 +1,23 @@
fun main(args: Array<String>) {
println(whatShouldIDoToday("happy"))
}
fun whatShouldIDoToday(
mood: String,
weather: String = "sunny",
temp: Int = 24
): String {
return when {
isHappyMood(mood) && isSunnyWeather(weather) -> "go for a walk"
isSadMood(mood) && isRainyWeather(weather) && isColdTemp(temp) -> "stay in bed"
isWarmTemp(temp) -> "go swimming"
else -> "stay home and read"
}
}
fun isHappyMood(mood: String) = mood == "happy"
fun isSadMood(mood: String) = mood == "sad"
fun isSunnyWeather(weather: String) = weather == "sunny"
fun isRainyWeather(weather: String) = weather == "rainy"
fun isColdTemp(temp: Int) = temp <= 0
fun isWarmTemp(temp: Int) = temp >= 23