U
U
Uncle Bogdan2021-08-02 17:11:07
code review
Uncle Bogdan, 2021-08-02 17:11:07

How to make this code more acceptable?

There are several classes that are stored on the components of a computer. I check everywhere what kind of component I insert into the computer. In the store, I check what component the player wants to buy. How to get rid of it?

The code
using UnityEngine;
using TMPro;

public class Buy : MonoBehaviour
{
    public Money Money;

    [SerializeField] Motherboard _currentMotherboard;
    [SerializeField] Power _currentPower;
    [SerializeField] Hdd _currentHdd;
    [SerializeField] Processor _currentProcessor;
    [SerializeField] Cooler _currentCooler;
    [SerializeField] Ram _currentRam;

    [Header("Motherboard info")]
    [SerializeField] GameObject _motherboardCanvas;

    [SerializeField] TMP_Text _motherboardPriceText;
    [SerializeField] TMP_Text _motherboardSocket;
    [SerializeField] TMP_Text _motherboardName;

    [Header("Power info")]
    [SerializeField] GameObject _powerCanvas;

    [SerializeField] TMP_Text _powerPriceText;
    [SerializeField] TMP_Text _powerCharge;
    [SerializeField] TMP_Text _powerName;

    [Header("Hdd info")]
    [SerializeField] GameObject _hddCanvas;

    [SerializeField] TMP_Text _hddPriceText;
    [SerializeField] TMP_Text _hddMemoryAmount;
    [SerializeField] TMP_Text _hddName;

    [Header("Processor info")]
    [SerializeField] GameObject _processorCanvas;

    [SerializeField] TMP_Text _processorPriceText;
    [SerializeField] TMP_Text _processorCoresAmount;
    [SerializeField] TMP_Text _processorThreads;
    [SerializeField] TMP_Text _processorSocket;
    [SerializeField] TMP_Text _processorName;

    [Header("Cooler info")]
    [SerializeField] GameObject _coolerCanvas;

    [SerializeField] TMP_Text _coolerPriceText;
    [SerializeField] TMP_Text _coolerCold;
    [SerializeField] TMP_Text _coolerName;

    [Header("Ram info")]
    [SerializeField] GameObject _ramCanvas;

    [SerializeField] TMP_Text _ramPriceText;
    [SerializeField] TMP_Text _ramMemoryAmount;
    [SerializeField] TMP_Text _ramName;

    public void RemoveAll()
    {
        _currentMotherboard = null;
        _currentPower = null;
        _currentHdd = null;
        _currentProcessor = null;
        _currentCooler = null;
        _currentRam = null;
    }

    public void buy(RaycastHit  hit)
    {
        if (hit.collider.GetComponent<Motherboard>())
        {
            ShowInformation(hit.collider.GetComponent<Motherboard>());
        }

        if (hit.collider.GetComponent<Power>())
        {
            ShowInformation(hit.collider.GetComponent<Power>());
        }

        if (hit.collider.GetComponent<Hdd>())
        {
            ShowInformation(hit.collider.GetComponent<Hdd>());
        }

        if (hit.collider.GetComponent<Processor>())
        {
             ShowInformation(hit.collider.GetComponent<Processor>());
        }

        if (hit.collider.GetComponent<Cooler>())
        {
            ShowInformation(hit.collider.GetComponent<Cooler>());
        }

        if (hit.collider.GetComponent<Ram>())
        {
            ShowInformation(hit.collider.GetComponent<Ram>());
        }
    }

    public void CompleteBuy()
    {
        if(_currentMotherboard)
        {
            if(_currentMotherboard.Price <= Money.Amount)
            {
                Money.Amount -= _currentMotherboard.Price;
                Money.RefreshValue();
            }
        }
        else if (_currentPower)
        {
            if (_currentPower.Price <= Money.Amount)
            {
                Money.Amount -= _currentPower.Price;
                Money.RefreshValue();
            }
        }
        else if(_currentHdd)
        {
            if (_currentHdd.Price <= Money.Amount)
            {
                Money.Amount -= _currentHdd.Price;
                Money.RefreshValue();
            }
        }
        else if(_currentProcessor)
        {
            if (_currentProcessor.Price <= Money.Amount)
            {
                Money.Amount -= _currentProcessor.Price;
                Money.RefreshValue();
            }
        }
        else if (_currentCooler)
        {
            if (_currentCooler.Price <= Money.Amount)
            {
                Money.Amount -= _currentCooler.Price;
                Money.RefreshValue();
            }
        }
        else if (_currentRam)
        {
            if (_currentRam.Price <= Money.Amount)
            {
                Money.Amount -= _currentRam.Price;
                Money.RefreshValue();
            }
        }
    }

    private void ShowInformation(Motherboard motherboard)
    {
        _motherboardSocket.text = "Сокет: " + motherboard.SocketName;
        _motherboardName.text = "Название: " + motherboard.Name;
        _motherboardPriceText.text = "Цена: " + motherboard.Price + " руб.";

        _currentMotherboard = motherboard;

        _motherboardCanvas.SetActive(true);
    }

    private void ShowInformation(Power power)
    {
        _powerCharge.text = "Мощность: " + power.Charge + "W";
        _powerName.text = "Название: " + power.Name;
        _powerPriceText.text = "Цена: " + power.Price + " руб.";

        _currentPower = power;

        _powerCanvas.SetActive(true);
    }

    private void ShowInformation(Hdd hdd)
    {
        _hddMemoryAmount.text = "Количество памяти: " + hdd.MemoryAmount + "GB";
        _hddName.text = "Название: " + hdd.Name;
        _hddPriceText.text = "Цена: " + hdd.Price + " руб.";

        _currentHdd = hdd;

        _hddCanvas.SetActive(true);
    }

    private void ShowInformation(Processor processor)
    {
        _processorName.text = "Название: " + processor.Name;
        _processorCoresAmount.text = "Количество ядер: " + processor.CoresCount;
        _processorThreads.text = "Частота процессора: " + processor.Threads + "GHZ";
        _processorSocket.text = "Сокет процессора: " + processor.SocketName;
        _processorPriceText.text = "Цена: " + processor.Price + " руб.";

        _currentProcessor = processor;

        _processorCanvas.SetActive(true);
    }

    private void ShowInformation(Cooler cooler)
    {
        _coolerName.text = "Название: " + cooler.Name;
        _coolerCold.text = "Охлаждение: " + cooler.Cold + " градусов";
        _coolerPriceText.text = "Цена: " + cooler.Price + " руб.";

        _currentCooler = cooler;

        _coolerCanvas.SetActive(true);
    }

    private void ShowInformation(Ram ram)
    {
        _ramMemoryAmount.text = "Количество памяти: " + ram.MemoryAmount + " GB";
        _ramName.text = "Название: " + ram.Name;
        _ramPriceText.text = "Цена: " + ram.Price + " руб.";

        _currentRam = ram;

        _ramCanvas.SetActive(true);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Voland69, 2021-08-03
@motkot

The classes Motherboard, Power, etc. allocate common behavior to the parent class (for example, Component) and also do the ShowInformation method with a Component type parameter.
CheckItem is similar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question