C# 饮料销售系统:基于多态的茶、咖啡和牛奶价格计算
using System;
namespace ConsoleApp1 { class Program { static void Main(string[] args) { Drink[] drinks = new Drink[10]; int i = 0; while (true) { string[] input = Console.ReadLine().Split(' '); if (input[0] == "0") { break; } int type = int.Parse(input[0]); int id = int.Parse(input[1]); int num = int.Parse(input[2]); double price = double.Parse(input[3]); if (type == 1) { int region = int.Parse(input[4]); if (id >= 100 && id <= 999 && num >= 0 && price >= 0) { if (region == 1) { drinks[i] = new Tea(id, num, price, region); Console.WriteLine(drinks[i].GetID() + " " + Math.Round(drinks[i].CalcPrice(), 1)); } else { drinks[i] = new Tea(id, num, price); Console.WriteLine(drinks[i].GetID() + " " + Math.Round(drinks[i].CalcPrice(), 1)); } } else if (id < 100 || id > 999) { Console.WriteLine("Drink ID error."); } else if (num < 0) { Console.WriteLine("Drink number error."); } else { Console.WriteLine("Drink price error."); } } else if (type == 2) { int process = int.Parse(input[4]); if (id >= 100 && id <= 999 && num >= 0 && price >= 0) { if (process == 1) { drinks[i] = new Coffee(id, num, price, process); Console.WriteLine(drinks[i].GetID() + " " + Math.Round(drinks[i].CalcPrice(), 1)); } else { drinks[i] = new Coffee(id, num, price); Console.WriteLine(drinks[i].GetID() + " " + Math.Round(drinks[i].CalcPrice(), 1)); } } else if (id < 100 || id > 999) { Console.WriteLine("Drink ID error."); } else if (num < 0) { Console.WriteLine("Drink number error."); } else { Console.WriteLine("Drink price error."); } } else if (type == 3) { if (id >= 100 && id <= 999 && num >= 0 && price >= 0) { drinks[i] = new Milk(id, num, price); Console.WriteLine(drinks[i].GetID() + " " + Math.Round(drinks[i].CalcPrice(), 1)); } else if (id < 100 || id > 999) { Console.WriteLine("Drink ID error."); } else if (num < 0) { Console.WriteLine("Drink number error."); } else { Console.WriteLine("Drink price error."); } } else { Console.WriteLine("Drink type error."); } i++; } } }
class Drink
{
protected int id, num;
protected double price;
public Drink(int id, int num, double price)
{
this.id = id;
this.num = num;
this.price = price;
}
public virtual double CalcPrice()
{
return num * price;
}
public int GetID()
{
return id;
}
}
class Tea : Drink
{
private int region;
private const double serviceCharge1 = 0.5;
private const double serviceCharge2 = 0.2;
public Tea(int id, int num, double price) : base(id, num, price)
{
region = 0;
}
public Tea(int id, int num, double price, int region) : base(id, num, price)
{
this.region = region;
}
public override double CalcPrice()
{
double total = base.CalcPrice();
if (region == 1)
{
total *= (1 + serviceCharge1);
}
else
{
total *= (1 + serviceCharge2);
}
return total;
}
}
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) : base(id, num, price)
{
process = 0;
}
public Coffee(int id, int num, double price, int process) : base(id, num, price)
{
this.process = process;
}
public override double CalcPrice()
{
double total = base.CalcPrice();
if (process == 1)
{
total *= (1 + serviceCharge1);
}
else
{
total *= (1 + serviceCharge2);
}
return total;
}
}
class Milk : Drink
{
public Milk(int id, int num, double price) : base(id, num, price)
{
}
}
}
原文地址: https://www.cveoy.top/t/topic/nTuO 著作权归作者所有。请勿转载和采集!