Untitled

 avatar
unknown
plain_text
2 years ago
3.3 kB
8
Indexable
package main

import (
	"fmt"
	"time"
)

type Product struct {
	name   string
	price  float32
	amount int
}

type Client struct {
	name     string
	products []Product
}

func main() {

	Pedro := (Client{
		name: "Pedro",
		products: []Product{
			{
				name:   "cereal",
				price:  2.5,
				amount: 3,
			},
			{
				name:   "jabones",
				price:  4.5,
				amount: 5,
			},
			{
				name:   "leche",
				price:  1.90,
				amount: 2,
			},
		},
	})

	Jorge := Client{
		name: "Jorge",
		products: []Product{
			{
				name:   "ceral",
				price:  8.90,
				amount: 4,
			},
			{
				name:   "jabones",
				price:  3,
				amount: 1,
			},
			{
				name:   "leche",
				price:  5.5,
				amount: 1,
			},
		},
	}
	fmt.Println("------------------------------")
	oneCashier(Pedro, Jorge)

	fmt.Println("------------------------------")
	TwoCashier(Pedro, Jorge)
}

func oneCashier(firstClient Client, secondClient Client) {
	finalPriceFirstClient, timeFirstClient := productProcessing(firstClient)
	finalPriceSecondClient, timeSecondClient := productProcessing(secondClient)
	fmt.Println("Cliente", firstClient.name, "venta ", finalPriceFirstClient, " Tiempo empleado: ", timeFirstClient)
	fmt.Println("Cliente", secondClient.name, "venta ", finalPriceSecondClient, " Tiempo empleado: ", timeSecondClient)
	fmt.Println("El tiempo total empleado en procesar ambas ordenes es ", timeFirstClient+timeSecondClient)
}

func productProcessing(client Client) (finalPrice float32, timeN int) {

	for i := 0; i < len(client.products); i++ {

		wait := client.products[i].amount * 1000
		time.Sleep(time.Duration(wait) * time.Millisecond)

		fmt.Println("Procesando producto ", i, "del cliente ", client.name)
		finalPrice = finalPrice + client.products[i].price*float32(client.products[i].amount)
		timeN = timeN + client.products[i].amount
	}

	return
}

func TwoCashier(firstClient Client, secondClient Client) {
	firstcanal1 := make(chan int)
	firstcanal2 := make(chan float32)

	secondcanal1 := make(chan int)
	secondcanal2 := make(chan float32)

	go productProcessingChannel(firstClient, firstcanal1, firstcanal2)
	go productProcessingChannel(secondClient, secondcanal1, secondcanal2)

	timeFirstClient := <-firstcanal1
	finalPriceFirstClient := <-firstcanal2
	timeSecondClient := <-secondcanal1
	finalPriceSecondClient := <-secondcanal2

	fmt.Println("Cliente", firstClient.name, "Venta ", finalPriceFirstClient, " Tiempo Empleado: ", timeFirstClient)
	fmt.Println("Cliente", secondClient.name, "Venta ", finalPriceSecondClient, " Tiempo Empleado: ", timeSecondClient)
	fmt.Println("El tiempo total empleado en procesar ambas ordenes es ", max(timeFirstClient, timeSecondClient))

}

func productProcessingChannel(client Client, canal1 chan int, canal2 chan float32) {

	var finalPrice float32
	var timeN int

	for i := 0; i < len(client.products); i++ {
		wait := client.products[i].amount * 1000
		time.Sleep(time.Duration(wait) * time.Millisecond)
		fmt.Println("Procesando producto ", i, "del cliente ", client.name)
		finalPrice = finalPrice + client.products[i].price*float32(client.products[i].amount)
		timeN = timeN + client.products[i].amount
	}

	canal1 <- timeN
	canal2 <- finalPrice

}
Editor is loading...