PU02 partially done

main
Eren Saglam 2022-12-12 19:08:10 +01:00
parent 35f617bbcc
commit 932139bc7c
6 changed files with 337 additions and 3 deletions

View File

@ -1,4 +1,4 @@
module Pflichtuebung_01.Date exposing (..) module Date exposing (..)
type alias Day = type alias Day =
Int Int

View File

@ -1,10 +1,10 @@
module Pflichtuebung_01.DateTests exposing (..) module DateTests exposing (..)
import Expect exposing (Expectation) import Expect exposing (Expectation)
import Fuzz exposing (Fuzzer, int, list, string) import Fuzz exposing (Fuzzer, int, list, string)
import Test exposing (..) import Test exposing (..)
import Pflichtuebung_01.Date exposing (..) import Date exposing (..)
testYear : Test testYear : Test

24
pu2/elm.json 100644
View File

@ -0,0 +1,24 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0"
},
"indirect": {
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.3"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}

210
pu2/src/Main.elm 100644
View File

@ -0,0 +1,210 @@
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Browser
import Html.Events exposing (..)
import List
import Date exposing (..)
import String
type alias Model =
{
arbeit : Arbeit
, beginndatum : Date
, besprechungsWochentag : Weekday
, anzahlWochenZwischenBesprechung : Int
}
type Msg
= SetStartDate String
| SetInterval String
| SetDegree String
| SetBesprechundsWochentag String
type Arbeit
= Bachelor
| Master
initModel : Model
initModel =
{
arbeit = Bachelor
, beginndatum = (21, 12, 2022)
, besprechungsWochentag = Mon
, anzahlWochenZwischenBesprechung = 1
}
getWeekday : Date -> Weekday
getWeekday date =
addWeekdays (Date.sub date (21, 12, 2022)) Wed
addWeekdays : Int -> Weekday -> Weekday
addWeekdays count day =
if count < 0
then
case count of
0 -> day
_ -> addWeekdays (count + 1) (prevWeekDay day)
else
if count == 0
then
case count of
0 -> day
_ -> addWeekdays (count - 1) (nextWeekDay day)
else day
nextWeekDay : Weekday -> Weekday
nextWeekDay day =
case day of
Mon -> Tue
Tue -> Wed
Wed -> Thu
Thu -> Fri
Fri -> Sat
Sat -> Sun
Sun -> Mon
prevWeekDay : Weekday -> Weekday
prevWeekDay day =
case day of
Mon -> Tue
Tue -> Wed
Wed -> Thu
Thu -> Fri
Fri -> Sat
Sat -> Sun
Sun -> Mon
getMonthLength : Date -> Int
getMonthLength date =
case Date.month date of
1 -> 31
2 -> if Date.leapyear (Date.year date) then 28 else 29
3 -> 31
4 -> 30
5 -> 31
6 -> 30
7 -> 31
8 -> 31
9 -> 30
10 -> 31
11 -> 30
12 -> 31
_ -> 30
addMonths : Int -> Date -> Date
addMonths count date =
if count > 0
then addMonths (count - 1) (addDays (getMonthLength date) date)
else date
addDays : Int -> Date -> Date
addDays count date =
if count > 0
then addDays (count - 1) (Date.next date)
else date
addMonthsLiterally : Int -> Date -> Date
addMonthsLiterally count date =
let newMonth = (Date.month date) + count
newDate = (Date.day date, if newMonth > 12 then (modBy 12 newMonth) else newMonth, if newMonth > 12 then (Date.year date) + 1 else Date.year date)
newDateMonthLength = getMonthLength newDate
in if Date.day newDate > newDateMonthLength then (Date.day date, newDateMonthLength, Date.year date) else newDate
calculateAbgabeEndDate : Arbeit -> Date -> Date
calculateAbgabeEndDate arbeit date =
case arbeit of
Bachelor -> Date.prev (addMonthsLiterally 3 date)
Master -> Date.prev (addMonthsLiterally 6 date)
validiereModel : Model -> Result String String
validiereModel model =
case model.besprechungsWochentag of
Sat -> Err "Datum darf kein Samstag sein"
Sun -> Err "Datum darf kein Sonntag sein"
_ -> Ok "Alles klärchen"
berechneDaten : Model -> List String
berechneDaten model = [] {- TODO -}
update : Msg -> Model -> Model
update msg model =
case msg of
SetStartDate date ->
{model | beginndatum = (
case (String.split " " date) of
["asap"] -> (21, 12, 2022) {- TODO -}
["ASAP"] -> (21, 12, 2022) {- TODO -}
[d, m, y] -> case [String.toInt d, String.toInt m, String.toInt y] of
[Just a, Just b, Just c] -> (a, b, c)
_ -> (21, 12, 2022)
_ ->(21, 12, 2022)
) }
SetDegree arbeits ->
if arbeits == "Bachelor" then
{model | arbeit = Bachelor }
else
{model | arbeit = Master }
SetInterval str ->
{model | anzahlWochenZwischenBesprechung = String.toInt str |> Maybe.withDefault 0}
SetBesprechundsWochentag weekday ->
{model | besprechungsWochentag = case weekday of
"Montag" -> Mon
"Dienstag" -> Tue
"Mittwoch" -> Wed
"Donnerstag" -> Thu
"Freitag" -> Fri
_ -> Mon
}
view : Model -> Html Msg
view model =
div []
[
select [onInput SetDegree]
[
option [value "Bachelor"] [text "Bachelor"]
, option [value "Master"] [text "Master"]
]
, input [onInput SetStartDate, placeholder "21 12 2022"] []
, select [onInput SetBesprechundsWochentag]
[
option [value "Montag"] [text "Montag"]
, option [value "Dienstag"] [text "Dienstag"]
, option [value "Mittwoch"] [text "Mittwoch"]
, option [value "Donnerstag"] [text "Donnerstag"]
, option [value "Freitag"] [text "Freitag"]
]
, select [onInput SetInterval]
[
option [value "1"] [text "1 Woche"]
, option [value "2"] [text "2 Wochen"]
, option [value "3"] [text "3 Wochen"]
, option [value "4"] [text "4 Wochen"]
]
, br [] []
, ul []
(model
|> berechneDaten
|> List.map (\datum -> li [] [text datum]))
, text (Debug.toString model.besprechungsWochentag), br[][]
, text (Debug.toString model.beginndatum), br[][]
, text (Debug.toString model.arbeit), br[][]
, text (Debug.toString model.anzahlWochenZwischenBesprechung), br[][]
, text (Debug.toString (getWeekday model.beginndatum)), br[][]
, text (Debug.toString (calculateAbgabeEndDate Bachelor model.beginndatum)), br[][]
, text (Debug.toString (calculateAbgabeEndDate Master model.beginndatum)), br[][]
, text (Debug.toString (getMonthLength model.beginndatum)), br[][]
, text (Debug.toString (Date.sub model.beginndatum (21, 12, 2022))), br[][]
]
main = Browser.sandbox { init = initModel, update = update, view = view }

100
pu2/src/Simple.elm 100644
View File

@ -0,0 +1,100 @@
module Simple exposing (main)
import Browser
import Html exposing (..)
import Html.Events exposing (onClick, onInput)
type Direction
= LessThan
| GreaterThan
type Msg
= Change String
| Inc
| Filter Direction
type alias Model =
{ numbers : List Int
, limit : Maybe Int
, filter : Int -> Bool
}
initialmodel : Model
initialmodel =
{ numbers = []
, filter = \_ -> True
, limit = Nothing
}
view : Model -> Html Msg
view model =
div []
[ h1 []
[ text "Zahlen" ]
, p []
[ text "Dies ist ein Beispiel." ]
, button [ onClick Inc ]
[ text "add" ]
, button [ onClick (Filter LessThan) ]
[ text "filter less than" ]
, input [ onInput Change ]
[]
, button [ onClick (Filter GreaterThan) ]
[ text "filter greater than" ]
, ul []
(model.numbers
|> List.filter model.filter
|> List.map viewNumber
)
]
viewNumber : Int -> Html Msg
viewNumber i =
li [] [ text (String.fromInt i) ]
functionBuilder : Model -> Direction -> (Int -> Bool)
functionBuilder model dir =
\i ->
case model.limit of
Just n ->
if dir == LessThan then
i > n
else
n > i
Nothing ->
True
update : Msg -> Model -> Model
update msg model =
case msg of
Change limit ->
{ model | limit = String.toInt limit }
Filter dir ->
{ model | filter = functionBuilder model dir }
Inc ->
{ model
| numbers =
case List.maximum model.numbers of
Just max ->
(max + 1) :: model.numbers
Nothing ->
0 :: model.numbers
}
main : Program () Model Msg
main =
Browser.sandbox { init = initialmodel, update = update, view = view }