Оптимизировать код - C# (185850)
Формулировка задачи:
Препод сказал оптимизировать код, а что изменить вообще не знаю
public class Test
{
public double a;
public string b;
public Int64 c;
public Test(double a, string b,Int64 c)
{
a = this.a;
b = this.b;
c = this.c;
}
public double Sam (double a,string b,Int64 c)
{
Int64 f=0;
double s = 0;
if ((b == "Samsung") || (b == "Apple"))
{
if (b == "Samsung")
{
f = 20000;
if (a >= (f * c))
{
if (c > 2) s = 15;
s = (f * c) / 100 * s;
s = a - (((f * c)/100) - s);
Console.WriteLine("Ваша сдача состовляет " + s);
}
else { Console.WriteLine("У вас недостаточно денег"); throw new ArgumentOutOfRangeException("Error"); }
}
if (b == "Apple")
{
f = 25000;
if (a >= (f * c))
{
if (c > 2) s = 15;
s = (f * c) / 100 * s;
s = a - ((f * c) - s);
Console.WriteLine("Ваша сдача состовляет " + s);
}
else Console.WriteLine("У вас недостаточно денег");
}
}
else Console.WriteLine("У нас нет таких телефонов");
return s;
}Решение задачи: «Оптимизировать код»
textual
Листинг программы
public class Test
{
public double a;
public string b;
public Int64 c;
public Test(double a, string b, Int64 c)
{
a = this.a;
b = this.b;
c = this.c;
}
public double Sam(double a, string b, Int64 c)
{
Int64 f;
double s = 0;
if (b == "Samsung") f = 20000;
else if (b == "Apple") f = 25000;
else throw new ArgumentException("У нас нет таких телефонов");
if (a >= (f * c))
{
if (c > 2) s = 15;
s = (f * c) / 100 * s;
s = a - (((f * c) / 100) - s);
}
else throw new InvalidOperationException("У вас недостаточно денег");
return s;
}
}