using System;

public class Drink { protected int id; protected int num; protected double price;

public Drink(int id, int num, double price)
{
    this.id = id;
    this.num = num;
    this.price = price;
}

public virtual void GetPrice()
{
    double total = num * price;
    Console.WriteLine("{0} {1}", id, Math.Round(total, 1));
}

}

public class Tea : Drink { private int local;

private const double serviceCharge1 = 0.5;
private const double serviceCharge2 = 0.2;

public Tea(int id, int num, double price, int local) : base(id, num, price)
{
    this.local = local;
}

public override void GetPrice()
{
    double total;
    if (local == 1)
    {
        total = num * price * (1 + serviceCharge1);
    }
    else
    {
        total = num * price * (1 + serviceCharge2);
    }
    Console.WriteLine("{0} {1}", id, Math.Round(total, 1));
}

}

public class Coffee : Drink { private int process;

private const double serviceCharge1 = 1.0;
private const double serviceCharge2 = 0.2;

public Coffee(int id, int num, double price, int process) : base(id, num, price)
{
    this.process = process;
}

public override void GetPrice()
{
    double total;
    if (process == 1)
    {
        total = num * price * (1 + serviceCharge1);
    }
    else
    {
        total = num * price * (1 + serviceCharge2);
    }
    Console.WriteLine("{0} {1}", id, Math.Round(total, 1));
}

}

public class Milk : Drink { public Milk(int id, int num, double price) : base(id, num, price) { }

public override void GetPrice()
{
    double total = num * price;
    Console.WriteLine("{0} {1}", id, Math.Round(total, 1));
}

}

public class Program { public static void Main() { Drink[] drinks = new Drink[10]; int index = 0;

    while (true)
    {
        string line = Console.ReadLine();
        if (line == "0")
        {
            break;
        }
        string[] s = line.Split();
        int type = int.Parse(s[0]);
        int id = int.Parse(s[1]);
        int num = int.Parse(s[2]);
        double price = double.Parse(s[3]);

        if (id < 100 || id > 999)
        {
            Console.WriteLine("Drink ID error.");
            continue;
        }
        if (num <= 0)
        {
            Console.WriteLine("Drink number error.");
            continue;
        }
        if (price < 0)
        {
            Console.WriteLine("Drink price error.");
            continue;
        }

        Drink drink;
        switch (type)
        {
            case 1:
                int local = int.Parse(s[4]);
                if (local != 1 && local != 2)
                {
                    Console.WriteLine("Drink type error.");
                    continue;
                }
                drink = new Tea(id, num, price, local);
                break;
            case 2:
                int process = int.Parse(s[4]);
                if (process != 1 && process != 2)
                {
                    Console.WriteLine("Drink type error.");
                    continue;
                }
                drink = new Coffee(id, num, price, process);
                break;
            case 3:
                drink = new Milk(id, num, price);
                break;
            default:
                Console.WriteLine("Drink type error.");
                continue;
        }
        drinks[index++] = drink;
    }

    for (int i = 0; i < index; i++)
    {
        drinks[i].GetPrice();
    }
}

}

C# 饮料商店:茶、咖啡和牛奶的计算服务费

原文地址: https://www.cveoy.top/t/topic/nTtb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录