I need help with optimizing a piece of code I've written in C#, which is very sloppy right now. I needed to write code that tracks the amount of money a hospital has made at the end of the day, depending on the amount of patients.
Here are some conditions and details:
Code has to display the cost for the patient and the amount of money made at the end of the day:
When a radio image and a consultation is done in one time by the same patient, there is a 25% price reduction off the price of a radio image.
- When a patient does a consultation, a blood check and an injection. That person gets 10$ after paying tax.
- The injections can be either 30ml, 50ml or 60ml. The prices are proportionate to the quantity of product injected. (for example: Someone aged 20 that takes a 50ml shot has to pay 25$)
- Taxes are 15%
int client;
int clientfinal = 0;
double injprix = 0;
double consprix = 0;
double imgradprix = 0;
double analyzeprix = 0;
double prixtot = 0;
double prixclient = 0;
double prixfinal = 0;
int injtaille = 0;
string cons, inj, imgrad, analyze;
Console.WriteLine("Combien a t-il de client aujourd'hui?");
client = (Convert.ToInt32(Console.ReadLine()));
if (client > 0)
do //un client
{
Console.WriteLine("Quel est l'age du patient?");
client = Convert.ToInt32(Console.ReadLine());
if (client < 12)
{
client = 1;
}
if (client >= 12 || client <= 18)
{
client = 2;
}
if (client >= 19 || client <= 65)
{
client = 3;
}
if (client > 65)
{
client = 4;
}
Console.WriteLine("La personne a t-elle choisit une consultation?");
cons = Convert.ToString(Console.ReadLine()).ToLower();
Console.WriteLine("La personne a t-elle choisit une image radio?");
imgrad = Convert.ToString(Console.ReadLine()).ToLower();
Console.WriteLine("La personne a t-elle choisit une analyze de sang?");
analyze = Convert.ToString(Console.ReadLine()).ToLower();
Console.WriteLine("La personne a t-elle choisit une injection?");
inj = Convert.ToString(Console.ReadLine()).ToLower();
if (inj == "oui")
{
Console.WriteLine("Quel est la taille de l'injection? (30 - 50 - 60) ");
injtaille = Convert.ToInt32(Console.ReadLine());
}
switch (client)
{
case 1:
consprix = 25;
imgradprix = 55;
analyzeprix = 28;
injprix = 0;
break;
case 2:
consprix = 32;
imgradprix = 65;
analyzeprix = 32;
switch (injtaille)
{
case 30:
injprix = 13;
break;
case 50:
injprix = (650 / 30);
break;
case 60:
injprix = (780 / 30);
break;
default:
Console.WriteLine("Taille d'injection inconnue.");
break;
}
break;
case 3:
consprix = 40;
imgradprix = 70;
analyzeprix = 40;
switch (injtaille)
{
case 30:
injprix = 13;
break;
case 50:
injprix = (750 / 30);
break;
case 60:
injprix = (900 / 30);
break;
default:
Console.WriteLine("Taille d'injection inconnue.");
break;
}
break;
case 4:
consprix = 30;
imgradprix = 60;
analyzeprix = 35;
switch (injtaille)
{
case 30:
injprix = 13;
break;
case 50:
injprix = (600 / 30);
break;
case 60:
injprix = (720 / 30);
break;
default:
Console.WriteLine("Taille d'injection inconnue.");
break;
}
break;
}
//Fin Switch
if (imgrad == "non")
{
imgradprix = 0;
}
if (cons == "non")
{
consprix = 0;
}
if (analyze == "non")
{
analyzeprix = 0;
}
if (inj == "non")
{
injprix = 0;
}
if (imgrad == "oui" || cons == "oui")
{
imgradprix = imgradprix * 0.75;
}
prixclient = consprix + imgradprix + analyzeprix + injprix;
prixclient = prixclient * 1.15;
if (cons == "oui" || analyze == "oui" || inj == "oui")
{
prixclient = prixclient - 10;
}
prixtot += prixclient;
clientfinal++;
prixfinal = prixtot;
Console.WriteLine("Prix du patient " + prixclient);
} while (clientfinal != client);
Console.WriteLine("Le prix final est" + prixfinal);
}
}
}
float
/double
types, as those will accumulate rounding errors! \$\endgroup\$