C# 在课堂上寻求指导
我目前正在从事一个项目,我被要求为一家小企业(在这里是一家小电影院)制作一个程序,在那里我可以记录和打印各种产品和客户的收据 到目前为止,我拥有的是一系列代码,记录客户想要订购什么 我如何制作另一个类来存储和计算价格,并向控制台打印收据的样子 我不是问答案,只是一些指导C# 在课堂上寻求指导,c#,class,methods,C#,Class,Methods,我目前正在从事一个项目,我被要求为一家小企业(在这里是一家小电影院)制作一个程序,在那里我可以记录和打印各种产品和客户的收据 到目前为止,我拥有的是一系列代码,记录客户想要订购什么 我如何制作另一个类来存储和计算价格,并向控制台打印收据的样子 我不是问答案,只是一些指导 namespace RecieptApp { class Reciept { public void Main() { double DoubleDisco
namespace RecieptApp
{
class Reciept
{
public void Main()
{
double DoubleDiscount;
int IntFoodOrdered;
int IntDrinkOrdered;
double DoubleFoodSize;
double DoubleDrinkSize;
int ACustomer;
int BCustomer;
string CustomerDescription;
string FoodDescription;
string DrinkDescription;
Console.Write("How many adults: ");
ACustomer = Console.Read();
Console.Write("How many kids: ");
BCustomer = Console.Read();
while (true)
{
Console.Write("Are you a government employee, current or retired military, or classified disabled? (Please answer yes or no) :");
string input1 = Console.ReadLine();
if (input1 == "yes")
{
DoubleDiscount = .15;
CustomerDescription = "Special Discount";
}
else
{
break;
}
}
while (true)
{
Console.WriteLine("Would you like to order some popcorn?");
string FoodInput1 = Console.ReadLine();
if (FoodInput1 == "yes")
{
Console.WriteLine("How many would you like?");
int.TryParse(Console.ReadLine(), out IntFoodOrdered);
while (true)
{
Console.WriteLine("And would you like that in small or large?");
string FoodInput2 = Console.ReadLine();
if (FoodInput2 == "small")
{
DoubleFoodSize = 3.75;
FoodDescription = "S";
}
else
{
DoubleFoodSize = 6.75;
FoodDescription = "L";
}
}
}
else
{
break;
}
}
while (true)
{
Console.WriteLine("Would you like to order a drink?");
string DrinkInput1 = Console.ReadLine();
if (DrinkInput1 == "yes")
{
Console.WriteLine("How many would you like?");
int.TryParse(Console.ReadLine(), out IntDrinkOrdered);
while (true)
{
Console.WriteLine("And Would you like that in small or large?");
string DrinkInput2 = Console.ReadLine();
if (DrinkInput2 == "small")
{
DoubleDrinkSize = 2.75;
DrinkDescription = "S";
}
else
{
DoubleDrinkSize = 5.75;
DrinkDescription = "L";
}
}
}
}
//This is where the other class would go in
//I just dont know how to go about it so that it would minimized the amount of code I have //to write
RecieptList Items = new RecieptList();
Items.AddProduct(ACustomer);
Items.AddProduct(BCustomer);
Items.AddProduct(CustomerDescription);
Items.AddProduct(FoodDescription);
Items.AddProduct(DrinkDescription);
}
}
}
如果您想要创建一个类,那么您可能需要创建一个表示通用产品的类,然后使用一个方法从该产品获取详细信息 既然我没有更好的事要做,我就把你变成了一个傻瓜。它过于简化了,但是它满足了您对非常特定的情况的需求,并且它使用了一个对象 我相信你足够聪明,可以使用一些概念来满足你的特定需求
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class Product
{
public string description;
public double price;
//normally you want to use eitehr an int for price(representing cents) or a decimal type, instead of double
//just using a double here for the sack of simplicity
// you'll also want to use properties instead of variable fields, but i'm using variable fields for simplicity
// constructor
public Product(string description, double price)
{
this.description = description;
this.price = price;
}
public string GetDetailLine()
{
// google "String.Format" for more information,
// basically it replaces things in the {0} and {1}, with the other parameters
// the ":c" in {1:c} part means format as currency
return String.Format("You ordered {0}. It costs: {1:c}", description, price);
}
}
class Program
{
static void Main(string[] args)
{
List<Product> products = new List<Product>();
Console.Write("Do You Want a Soda? ");
string input = Console.ReadLine();
//the .ToUpper() part makes it upper case
if (input.ToUpper() == "YES")
{
Product soda = new Product("soda", 2.50);
products.Add(soda);
}
double total = 0;
for(int i = 0; i<products.Count; i++)
{
Console.WriteLine(products[i].GetDetailLine());
total += products[i].price;
}
//you can also use Console.Writeline like String.Format
Console.WriteLine("Your Total is: {0:c}", total);
// this is just to pause in-case you're debugging from visual studio
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序2
{
公共类产品
{
公共字符串描述;
公开双价;
//通常,您希望使用整数表示价格(表示美分)或十进制类型,而不是双精度
//为了简单起见,这里只使用了一个double
//您还需要使用属性而不是变量字段,但为了简单起见,我使用变量字段
//建造师
公共产品(字符串描述,双倍价格)
{
this.description=描述;
这个价格=价格;
}
公共字符串GetDetailLine()
{
//谷歌“String.Format”获取更多信息,
//基本上,它用其他参数替换{0}和{1}中的内容
//{1:c}部分中的“:c”表示货币形式
return String.Format(“您订购了{0},成本:{1:c}”,描述,价格);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
列表产品=新列表();
控制台。写下(“你想要苏打水吗?”);
字符串输入=Console.ReadLine();
//.ToUpper()部分使用大写形式
if(input.ToUpper()=“是”)
{
产品苏打=新产品(“苏打”,2.50);
添加(苏打水);
}
双倍合计=0;
对于(int i=0;i('e'之前的i',除了'c'之后的i'),您至少需要添加一个'price'变量,该变量随每次购买一起添加到。这不需要在另一个类中。您可以查看一些想法。如果您能掌握它,它将为类、对象和方法提供漂亮的指南。:-)抱歉,我是C#的新手,但对于这种型号,如果我想使用多个产品,我如何才能将其添加到列表中?@NghiaLe只需使用products.add()
method againI尝试过Console.Write(“多少成年人?”);int.TryParse(Console.ReadLine(),out AdultTicket);产品成人票=新产品(“成人票”,7.75);产品。添加(成人票);但调试时不允许我继续。@n首先,代码更适合您的问题,而不是注释。其次,adultTicket
在声明时不大写,但在使用时大写。我感谢您的患者Sam,但日志中说“Argument1:无法从“int”转换为”ConsoleApplication2.Product“。我将成人票声明为int,以统计订购了多少张票。我这样问是因为我想对所有的产品都这样做。