OneCompiler

CreditChecker

1680

Following is solution in Go.

package main

import (
	"encoding/json"
	"fmt"
	"time"
)

// Структуры для соответствия с JSON
type Passport struct {
	Series     string    `json:"series"`
	Number     string    `json:"number"`
	IssuedAt   time.Time `json:"issuedAt"`
	Issuer     string    `json:"issuer"`
	IssuerCode string    `json:"issuerCode"`
}

type Credit struct {
	Type                  string     `json:"type"`
	Currency              string     `json:"currency"`
	IssuedAt              time.Time  `json:"issuedAt"`
	Rate                  float32    `json:"rate"`
	LoanSum               int        `json:"loanSum"`
	Term                  int        `json:"term"`
	RepaidAt              *time.Time `json:"repaidAt"`
	CurrentOverdueDebt    float32    `json:"currentOverdueDebt"`
	NumberOfDaysOnOverdue int        `json:"numberOfDaysOnOverdue"`
	RemainingDebt         float32    `json:"remainingDebt"`
	CreditId              string     `json:"creditId"`
}

type Client struct {
	FirstName     string    `json:"firstName"`
	MiddleName    string    `json:"middleName"`
	LastName      string    `json:"lastName"`
	BirthDate     time.Time `json:"birthDate"`
	Citizenship   string    `json:"citizenship"`
	Passport      Passport  `json:"passport"`
	CreditHistory []Credit  `json:"creditHistory"`
}

func calculateAge(birthDate time.Time) int {
	now := time.Now()
	years := now.Year() - birthDate.Year()
	if now.YearDay() < birthDate.YearDay() {
		years--
	}
	return years
}

func checkMinAge(client Client) bool {
	return calculateAge(client.BirthDate) >= 20
}

func checkPassportValidity(client Client) bool {
	age := calculateAge(client.BirthDate)
	if age > 20 && client.Passport.IssuedAt.Before(client.BirthDate.AddDate(20, 0, 0)) {
		return false
	}
	if age >= 45 && client.Passport.IssuedAt.Before(client.BirthDate.AddDate(45, 0, 0)) {
		return false
	}
	return true
}

func checkCreditHistory(client Client) bool {
	overdueCount := 0
	for _, credit := range client.CreditHistory {
		if credit.CurrentOverdueDebt > 0 {
			return false
		}
		if credit.Type != "Кредитная карта" && credit.NumberOfDaysOnOverdue > 60 {
			return false
		}
		if credit.Type == "Кредитная карта" && credit.NumberOfDaysOnOverdue > 30 {
			return false
		}
		if credit.Type != "Кредитная карта" && credit.NumberOfDaysOnOverdue > 15 {
			overdueCount++
		}
	}
	return overdueCount <= 2
}

func isCLientEligible(client Client) bool {
	return checkMinAge(client) &&
		checkPassportValidity(client) &&
		checkCreditHistory(client)
}

func main() {
	data := `{	
		"firstName": "Иван",  
		"middleName": "Иванович", 
		"lastName": "Иванов", 
		"birthDate": "1969-12-31T21:00:00.000Z",
		"citizenship": "РФ", 
		"passport": { 
			"series": "12 34", 
			"number": "123456", 
			"issuedAt": "2023-03-11T21:00:00.000Z", 
			"issuer": "УФМС", 
			"issuerСode": "123-456"
		}, 
		"creditHistory": [ 
		{ 
			"type": "Кредит наличными", 
			"currency": "RUB",  
			"issuedAt": "2003-02-27T21:00:00.000Z",
			"rate": 0.13, 
			"loanSum": 100000, 
			"term": 12, 
			"repaidAt": "2004-02-27T21:00:00.000Z", 
			"currentOverdueDebt": 0, 
			"numberOfDaysOnOverdue": 0, 
			"remainingDebt": 0, 
			"creditId": "25e8a350-fbbc-11ee-a951-0242ac120002" 
		}, 
		{ 
			"type": "Кредитная карта", 
			"currency": "RUB", 
			"issuedAt": "2009-03-27T21:00:00.000Z",
			"rate": 0.24, 
			"loanSum": 30000, 
			"term": 3, 
			"repaidAt": "2009-06-29T20:00:00.000Z", 
			"currentOverdueDebt": 0, 
			"numberOfDaysOnOverdue": 2, 
			"remainingDebt": 0, 
			"creditId": "81fb1ff6-fbbc-11ee-a951-0242ac120002" 
		},
		{ 
			"type": "Кредит наличными", 
			"currency": "RUB", 
			"issuedAt": "2009-02-27T21:00:00.000Z", 
			"rate": 0.09, 
			"loanSum": 200000, 
			"term": 24, 
			"repaidAt": "2011-03-02T21:00:00.000Z", 
			"currentOverdueDebt": 0, 
			"numberOfDaysOnOverdue": 14, 
			"remainingDebt": 0, 
			"creditId": "c384eea2-fbbc-11ee-a951-0242ac120002" 
		}, 
		{ 
			"type": "Кредитная наличными", 
			"currency": "RUB", 
			"issuedAt": "2024-05-15T21:00:00.000Z", 
			"rate": 0.13, 
			"loanSum": 200000, 
			"term": 36, 
			"repaidAt": null, 
			"currentOverdueDebt": 10379, 
			"numberOfDaysOnOverdue": 15, 
			"remainingDebt": 110000, 
			"creditId": "ebeddfde-fbbc-11ee-a951-0242ac120002" 
		}
	] 
}`
	var client Client
	err := json.Unmarshal([]byte(data), &client)
	if err != nil {
		fmt.Println("Ошибка при разборе JSON", err)
		return
	}

	if isCLientEligible(client) {
		fmt.Println("Клиент прошёл все проверки")
	} else {
		fmt.Println("Клиент не прошёл проверки")
	}
}