2012-12-06 15 views
2

我目前正在制作一个项目,要求我为小企业制作节目(在这种情况下是一个小电影院),我可以在这里为各种产品和客户录制和打印收据。寻找班级指导

我到目前为止是一系列记录客户想要订购的代码。

我该如何着手制作另一个存储并计算价格的课程,并向控制台输出收据的样子?

我不是要求答案,只是一些指导。

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); 

     } 
    } 
} 
+2

('e'在'e'之前,'c'后面除外)。至少,您需要添加一个'price'变量,并在每次购买时添加。那不需要在另一个类 – mcalex

+0

。你可以看看[这篇文章](http://social.msdn.microsoft.com/Forums/en/windowsgeneraldevelopmentissues/thread/0b802bd6-25ea-4a1f-8cb1-a1fabbb05356)的一些想法。如果你可以得到一个[Headfirstc#](http://www.headfirstlabs.com/books/hfcsharp/),它可以为类,对象,方法提供美丽的指导。 :-) – bonCodigo

回答

1

如果你想使一个类时,你可能会想使一个类,将代表一个通用的产品,然后有一个方法,从该产品获得详细信息行

既然我没有更好的办法,我已经让你成为SSCCE。它过于简化,但它满足您对特定情况的要求,并使用一个对象。

我相信您足够聪明,可以使用一些概念来满足您的特定需求。

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(); 
     } 
    } 
} 
+0

对不起,我是C#的新手,但是对于这个模型,如果我想使用多个产品,我该如何将它添加到列表中? –

+0

@NghiaLe再次使用'products.Add()'方法 –