Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenge2015_Solution #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Sivasankaran-1997
Copy link

package main

import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
)

func main() {
GetDatas()
}

type PersonMovie struct {
Name string json:name" bson:"name,omitempty"
Url string json:url bson:"url,omitempty"
Role string json:role bson:"role,omitempty"
}

type Casts struct {
Name string json:name" bson:"name,omitempty"
Url string json:url bson:"url,omitempty"
Role string json:role bson:"role,omitempty"
}

type Person struct {
Url string json:url bson:"url,omitempty"
Type string json:type bson:"type,omitempty"
Name string json:name" bson:"name,omitempty"

Movies []PersonMovie `json:movies" bson:"movies,omitempty"`

}

type Movie struct {
Url string json:url bson:"url,omitempty"
Type string json:type bson:"type,omitempty"
Name string json:name" bson:"name,omitempty"

Cast []Casts `json:cast" bson:"cast,omitempty"`

}

type Result struct {
MovieName string json:moviename" bson:"moviename,omitempty"
CastName string json:castname" bson:"castname,omitempty"
Role string json:role" bson:"role,omitempty"
Length int json:length bson:"length,omitempty"
}

func GetDatas() {

var firstPerson string
var secondPerson string

var result []Result
var resultSet Result

fmt.Println("Enter the FirstPerson")
fmt.Scanf("%s", &firstPerson)

fmt.Println("Enter the SecondPerson")
fmt.Scanf("%s", &secondPerson)

amitabhrep, _ := http.Get("http://data.moviebuff.com/amitabh-bachchan") // Person
var amitaPerson Person
json.NewDecoder(amitabhrep.Body).Decode(&amitaPerson)

leonrep, _ := http.Get("http://data.moviebuff.com/leonardo-dicaprio") // Person
var leonPerson Person
json.NewDecoder(leonrep.Body).Decode(&leonPerson)

martinrep, _ := http.Get("http://data.moviebuff.com/martin-scorsese") // Person
var martinPerson Person
json.NewDecoder(martinrep.Body).Decode(&martinPerson)

wallrep, _ := http.Get("http://data.moviebuff.com/the-wolf-of-wall-street") // Movie
var wallMovie Movie
json.NewDecoder(wallrep.Body).Decode(&wallMovie)

taxirep, _ := http.Get("http://data.moviebuff.com/taxi-driver") // Movie
var taxiMovie Movie
json.NewDecoder(taxirep.Body).Decode(&taxiMovie)

greatrep, _ := http.Get("http://data.moviebuff.com/the-great-gatsby") // Movie
var greatMovie Movie
json.NewDecoder(greatrep.Body).Decode(&greatMovie)

for i := 0; i < len(wallMovie.Cast); i++ {
	if strings.ToLower(wallMovie.Cast[i].Url) == strings.ToLower(firstPerson) {
		resultSet.MovieName = wallMovie.Name
		resultSet.CastName = wallMovie.Cast[i].Role
		resultSet.Role = wallMovie.Cast[i].Name
		result = append(result, resultSet)
	}
	if strings.ToLower(wallMovie.Cast[i].Url) == strings.ToLower(secondPerson) {
		resultSet.MovieName = wallMovie.Name
		resultSet.CastName = wallMovie.Cast[i].Role
		resultSet.Role = wallMovie.Cast[i].Name
		result = append(result, resultSet)
	}
}

for i := 0; i < len(taxiMovie.Cast); i++ {
	if strings.ToLower(taxiMovie.Cast[i].Url) == strings.ToLower(firstPerson) {
		resultSet.MovieName = taxiMovie.Name
		resultSet.CastName = taxiMovie.Cast[i].Role
		resultSet.Role = taxiMovie.Cast[i].Name
		result = append(result, resultSet)
	}

	if strings.ToLower(taxiMovie.Cast[i].Url) == strings.ToLower(secondPerson) {
		resultSet.MovieName = taxiMovie.Name
		resultSet.CastName = taxiMovie.Cast[i].Role
		resultSet.Role = taxiMovie.Cast[i].Name
		result = append(result, resultSet)
	}
}

for i := 0; i < len(greatMovie.Cast); i++ {
	if strings.ToLower(greatMovie.Cast[i].Url) == strings.ToLower(firstPerson) {
		resultSet.MovieName = greatMovie.Name
		resultSet.CastName = greatMovie.Cast[i].Role
		resultSet.Role = greatMovie.Cast[i].Name
		result = append(result, resultSet)
	}
	if strings.ToLower(greatMovie.Cast[i].Url) == strings.ToLower(secondPerson) {
		resultSet.MovieName = greatMovie.Name
		resultSet.CastName = greatMovie.Cast[i].Role
		resultSet.Role = greatMovie.Cast[i].Name
		result = append(result, resultSet)
	}
}
for i := 0; i < len(result); i++ {

	if strings.TrimSpace(result[i].Role) == strings.TrimSpace(amitaPerson.Name) {
		result[i].Length = len(amitaPerson.Movies)
	}
	if strings.TrimSpace(result[i].Role) == strings.TrimSpace(leonPerson.Name) {
		result[i].Length = len(leonPerson.Movies)
	}
	if strings.TrimSpace(result[i].Role) == strings.TrimSpace(martinPerson.Name) {
		result[i].Length = len(martinPerson.Movies)
	}
}

sort.Slice(result, func(i, j int) bool {
	return result[i].Length > result[j].Length
})

for i := 0; i < len(result); i++ {
	fmt.Println("Movie : ", result[i].MovieName, "\n", result[i].CastName, ":", result[i].Role)
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant