2016-10-10 43 views
-1

我想建立一個項目,我可以管理客戶,所以我可以查看信息並能夠添加或刪除客戶。在「DisplayMainMenu()」上按2可以顯示客戶信息嗎?然後它要求您的ID顯示客戶信息?我試圖在406線上做到這一點,但我無法做到。我猜我必須從列表中獲取數組?如果是這樣,我怎麼能改善我的代碼?有沒有辦法在這個菜單項目上顯示Customers信息?

的Program.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace LabMenu 
{ 
public static class Program 
{ 
    public static PreferredCustomer[] preferredCustomers; 
    public static Customer customer; 

    public static string firstName { get; set; } 
    public static string lastName { get; set; } 
    public static double flashlight { get; private set; } 
    public static double iphone { get; private set; } 
    public static double printer { get; private set; } 
    public static double laptop { get; private set; } 
    public static double playstation { get; private set; } 


    static void Main(string[] args) 
    { 
     GetPreferredCustomers(@"CustomerInfo.txt"); // 
     DisplayMainMenu(); 
    } 

    //the method below uses text file to add elements to the array 
    static void GetPreferredCustomers (string path) 
    { 
     List<string> customers = new List<string>(); 
     int count = 0; 
     using (StreamReader sr = new StreamReader(path)) 
     { 
      while (sr.Peek() >= 0) 
      { 
       count++; 
       customers.Add(sr.ReadLine()); 
      } 
     } 
     preferredCustomers = new PreferredCustomer[count]; 
     for(int i = 0; i < count; i++) 
     { 
      string[] info = customers[i].Split(':'); 
      PreferredCustomer pc = new PreferredCustomer(info[0], info[1], info[2], info[3], info[4], Int32.Parse(info[5]), Boolean.Parse(info[6])); 
      preferredCustomers[i] = pc; //the array prefferedCustomers contains values 
     } 

    } 
    public static void DisplayMainMenu() 
    { 
     do 
     { 
      Console.Clear(); 
      Console.WriteLine("Welcome to the Menu!"); 
      Console.WriteLine("0. Quit"); 
      Console.WriteLine("1. Make An Order"); 
      Console.WriteLine("2. Manage Customers"); 

      switch (ConsoleHelper.ReadInt32(0, 2)) 
      { 
       case 0: return; 
       case 1: DisplayCustomersMenu(); break; 
       case 2: GetData(); break; 
      }; 

     } while (true); 
    } 

    private static void DisplayCustomersMenu() 
    { 
     do 
     { 
      Console.Clear(); 

      Console.WriteLine("Make An Order"); 

      Console.WriteLine($"What is your first name?"); 
      firstName = Console.ReadLine(); 
      Console.WriteLine($"What is your last name?"); 
      lastName = Console.ReadLine(); 

      string strTarget = String.Format("Hello, {0} {1}.", firstName, lastName); 

      Console.WriteLine("{0}{1}{0}", 
           Environment.NewLine, strTarget); 

      Console.WriteLine("1) Select Customer"); 
      Console.WriteLine("0) Return to Manage Customers"); 

      switch (ConsoleHelper.ReadInt32(0, 3)) 
      { 
       case 0: return; 
       case 1: 
        { 
         string enteredID = ""; 

         do 
         { 
          Console.WriteLine("Enter the customer ID: "); 
          enteredID = Console.ReadLine(); 

          //var id = ConsoleHelper.ReadInt32("Invalid customer ID", 0, Int32.MaxValue); 

          foreach (var id in preferredCustomers) 
          { 
           if (id.CustomerID == enteredID) 
           { 
            DisplayCustomerMenu(customer); 
           } 
           else 
           { 
            Console.WriteLine("You are not in the database"); 
            break; 
           } 
          } 
          //return new Customer(); 

         } while (true); 

        } 
       }; 
     } while (true); 
    } 


    public static void DisplayCustomerMenu(Customer customer) 
    { 
     do 
     { 
      Console.Clear(); 

      Console.WriteLine("Manage Customers"); 

      Console.WriteLine($"Custumer name, ID and current order"); 


      Console.WriteLine("1) Add to Order"); 
      Console.WriteLine("2) Remove from Order"); 
      Console.WriteLine("3) Finalize Order"); 
      Console.WriteLine("0) Return to Manage Customers"); 

      switch (ConsoleHelper.ReadInt32(0, 3)) 
      { 
       case 0: return; 
       case 1: AddToOrder(customer); break; 
       case 2: RemoveFromOrder(customer); break; 
       case 3: FinalizeOrder(customer); break; 
      }; 
     } while (true); 
    } 


    public static void AddToOrder(Customer id) 
    { 
      Console.Clear(); 

      int numberOfInputForFlashlight = 0; 
      int numberOfInputForIphone = 0; 
      int numberOfInputForPrinter = 0; 
      int numberOfInputForLaptop = 0; 
      int numberOfInputForPlaystation = 0; 

      int myint = -1; 

     while (myint != 0) 
     { 
      string group; 

      Console.WriteLine("Add To Order"); 
      Console.WriteLine("1) Flashlight"); 
      Console.WriteLine("2) iPhone 7"); 
      Console.WriteLine("3) Printer"); 
      Console.WriteLine("4) Dell Laptop"); 
      Console.WriteLine("5) Playstation 4"); 
      Console.WriteLine("6) View Total"); 

      Console.WriteLine("[Press 0 to quit]"); 

      group = Console.ReadLine(); 
      myint = Int32.Parse(group); 

      switch (myint) 
      { 
       case 0: 
        break; 
       case 1: 
        double input1; 
        string inputString1; 
        Console.WriteLine("How many flashlights do you want?"); 
        inputString1 = Console.ReadLine(); 
        input1 = Convert.ToDouble(inputString1); 
        flashlight += input1; 
        numberOfInputForFlashlight++; 
        break; 
       case 2: 
        double input2; 
        string inputString2; 
        Console.WriteLine("How many iPhone 7 do you want?"); 
        inputString2 = Console.ReadLine(); 
        input2 = Convert.ToDouble(inputString2); 
        iphone += input2; 
        numberOfInputForIphone++; 
        break; 
       case 3: 
        double input3; 
        string inputString3; 
        Console.WriteLine("How many Printers do you want?"); 
        inputString3 = Console.ReadLine(); 
        input3 = Convert.ToDouble(inputString3); 
        printer += input3; 
        numberOfInputForPrinter++; 
        break; 
       case 4: 
        double input4; 
        string inputString4; 
        Console.WriteLine("How many Dell Laptops do you want?"); 
        inputString4 = Console.ReadLine(); 
        input4 = Convert.ToDouble(inputString4); 
        laptop += input4; 
        numberOfInputForLaptop++; 
        break; 
       case 5: 
        double input5; 
        string inputString5; 
        Console.WriteLine("How many Playstion 4 do you want?"); 
        inputString5 = Console.ReadLine(); 
        input5 = Convert.ToDouble(inputString5); 
        playstation += input5; 
        numberOfInputForPlaystation++; 
        break; 
       case 6: 
        Console.WriteLine("Flashlist quantity is {0}", flashlight.ToString("F")); 
        Console.WriteLine("iPhone 7 quantity is {0}", iphone.ToString("F")); 
        Console.WriteLine("Printer quantity is {0}", printer.ToString("F")); 
        Console.WriteLine("Dell Laptop quantity is {0}", laptop.ToString("F")); 
        Console.WriteLine("Playstation 4 quantity is {0}", playstation.ToString("F")); 
        Console.WriteLine("Flashlight total is {0}", (flashlight * 15.00).ToString("C")); 
        Console.WriteLine("iPhone 7 total is {0}", (iphone * 700.00).ToString("C")); 
        Console.WriteLine("Printer total is {0}", (printer * 80.00).ToString("C")); 
        Console.WriteLine("Dell Laptop total is {0}", (laptop * 500.00).ToString("C")); 
        Console.WriteLine("Playstation 4 total is {0}", (playstation * 380.00).ToString("C")); 
        break; 

       default: 
        Console.WriteLine("Incorrect input", myint); 
        break; 
      } 
     } 

    } 

    public static void FinalizeOrder(Customer customer) 
    { 
     do 
     { 
      Console.Clear(); 
      return; 
     } while (true); 
    } 

    static void RemoveFromOrder(Customer customer) 
    { 
     do 
     { 
      Console.Clear(); 

      Console.WriteLine("Remove From Order:"); 
      Console.WriteLine("1) Flashlight total is {0}", (flashlight * 15.00).ToString("C")); 
      Console.WriteLine("2) iPhone 7 total is {0}", (iphone * 700.00).ToString("C")); 
      Console.WriteLine("3) Printer total is {0}", (printer * 80.00).ToString("C")); 
      Console.WriteLine("4) Dell Laptop total is {0}", (laptop * 500.00).ToString("C")); 
      Console.WriteLine("5) Playstation 4 total is {0}", (playstation * 380.00).ToString("C")); 

      switch (ConsoleHelper.ReadInt32(0, 5)) 
      { 
       case 0: break; 
       case 1: 
        double input1; 
        string inputString1; 
        Console.WriteLine("How many flashlights do you want to remove?"); 
        inputString1 = Console.ReadLine(); 
        input1 = Convert.ToDouble(inputString1); 
        flashlight -= input1; 
        break; 
       case 2: 
        double input2; 
        string inputString2; 
        Console.WriteLine("How many iPhone 7 do you remove?"); 
        inputString2 = Console.ReadLine(); 
        input2 = Convert.ToDouble(inputString2); 
        iphone -= input2; 
        break; 

       case 3: 
        double input3; 
        string inputString3; 
        Console.WriteLine("How many Printers do you remove?"); 
        inputString3 = Console.ReadLine(); 
        input3 = Convert.ToDouble(inputString3); 
        printer -= input3; 
        break; 
       case 4: 
        double input4; 
        string inputString4; 
        Console.WriteLine("How many Dell Laptops do you remove?"); 
        inputString4 = Console.ReadLine(); 
        input4 = Convert.ToDouble(inputString4); 
        laptop += input4; 
        break; 
       case 5: 
        double input5; 
        string inputString5; 
        Console.WriteLine("How many Playstion 4 do you remove?"); 
        inputString5 = Console.ReadLine(); 
        input5 = Convert.ToDouble(inputString5); 
        playstation += input5; 
        break; 

      }; 

     } while (true); 
    } 

    public static void GetData() 
    { 
     using (var reader = new StreamReader("CustomerInfo.txt")) 
     { 
      var index = 0; 
      preferredCustomers = new PreferredCustomer[5]; 
      while (!reader.EndOfStream) 
      { 
       var line = reader.ReadLine().Split(':'); 
       var name = line[0]; 
       var address = line[1]; 
       var phone = line[2]; 
       var id = line[3]; 
       var email = line[4]; 
       var spentAmount = Convert.ToInt32(line[5]); 
       var onEmailList = Convert.ToBoolean(line[6]); 
       preferredCustomers[index] = new PreferredCustomer(name, address, phone, id, email, spentAmount, 
        onEmailList); 
       index++; 
      } 
      GetID(); 
     } 
    } 


    public static void UpdateData() 
    { 
     using (var writer = new StreamWriter("CustomerInfo2.txt")) 
     { 
      for (var i = 0; i < 5; i++) 
      { 
       var name = preferredCustomers[i].CustomerName; 
       var address = preferredCustomers[i].Address; 
       var phone = preferredCustomers[i].Phone; 
       var id = preferredCustomers[i].CustomerID; 
       var email = preferredCustomers[i].CustomerID; 
       var updatedSpentAmount = preferredCustomers[i].CalcAmount(); 
       var onEmailList = preferredCustomers[i].OnEmailList; 
       writer.WriteLine("{0}:{1}:{2}:{3}:{4}:{5}:{6}", 
       name, address, phone, id, email, updatedSpentAmount, onEmailList == true ? "true" : "false"); 
      } 
     } 
    } 

    public static int GetID() 
    { 
     //id the user enters as input 
     string enteredID = ""; 

     do 
     { 
      var customerIndex = 0; 
      Console.Write("Please enter user ID: "); 
      enteredID = Console.ReadLine(); 

      foreach (var id in preferredCustomers) 
      { 
       if (id.CustomerID == enteredID) 
       { 
        return customerIndex; 
       } 
       else 
       { 
        customerIndex++; 
       } 
      } 

      Console.Write("ID does not exist. "); 

     } while (true); 
    } 

    public static string UserChoice() 
    { 
     var userChoice = Console.ReadLine(); 

     while (userChoice != "1" && userChoice != "2"); 
     { 
      Console.WriteLine("Wrong Entry. Try Again."); 
      DisplayMenu(); //Not working 
      userChoice = Console.ReadLine(); 
     } 

     return userChoice; 
    } 

    public static void DisplayMenu() 
    { 
     Console.WriteLine("1. Display Customer Info"); 
     Console.WriteLine("2. Update Customer Info"); 
     Console.Write("Please enter your choice: "); 
    } 
} 
} 

PreferredCustomers.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace LabMenu 
{ 
public class PreferredCustomer : Customer 
{ 
    public PreferredCustomer(string name, string address, string phone, string id, 
     string email, int spentAmount, bool onEmailList) 
     : base (name, address, phone, id, email, spentAmount, onEmailList) 
    { 
     DiscountLevel = SetDiscountLevel(); 
    } 

    public readonly decimal DiscountLevel; 

    public decimal SetDiscountLevel() 
    { 
     int range = SpentAmount/500; 
     switch (range) 
     { 
      case 0: 
       return 0; 

      case 1: 
       return 0.05m; 

      case 2: 
       return 0.05m; 

      case 3: 
       return 0.08m; 

      default: 
       return 0.1m; 


     } 
    } 

    public double GetDiscount() 
    { 
     return SpentAmount * (double)DiscountLevel; 
    } 

    public override double CalcAmount() 
    { 
     return base.CalcAmount() - GetDiscount(); 
    } 

    public override string ToString() 
    { 
     return 
      String.Format(
       "CustomerID: {0}\nCustomer Name: {1}\nCustomerAddress: {2}\n" + 
       "Customer Phone: {3} \nCustomer Email: {4}" + 
       "Customer Spending: {5:C2}\nCustomer On Email List: {6}", 
       CustomerID, CustomerName, Address, Phone, CustomerEmail, SpentAmount, OnEmailList 
       ); 
    } 

} 
} 

Customer.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace LabMenu 
{ 
public class Customer : Person 
{ 
    public Customer(string name, string address, string phone, string id, string email, int spentAmount, 
     bool onEmailList) 
     : base(name, address, phone) 
    { 
     CustomerID = id; 
     CustomerEmail = email; 
     SpentAmount = spentAmount; 
     OnEmailList = onEmailList; 
    } 

    public string CustomerID { get; set; } 
    public string CustomerEmail { get; set; } 
    public int SpentAmount { get; set; } 
    public bool OnEmailList { get; set; } 

    public virtual double CalcAmount() 
    { 
     return SpentAmount; 
    } 
} 
} 

CustomerInfo.txt

Alex Hernandez:123 10th st, Allen, TX 78714:972-555-0000:A0000001:[email protected]:2500:true 
Albert Gomez:456 15th st, Austin, TX 78504:512-456-1000:A0000002:[email protected]:2500:true 
Jose Martinez:2004 44th st, Washington, DC 20001:202-456-2222:A0000003:[email protected]:495:false 
Joseph Olivas:123 16th st, Washington, DC 20002:202-555-6666:A0000004:[email protected]:1200:true 
Pablo Cortez:777 2th st, Houston, TX 77002:832-100-2000:A0000005:[email protected]:1750:false 
+0

是否所有這些代碼都與您遇到的問題相關?你能否編輯你的問題只包括與你的問題相關的代碼。這將使我們的生活變得更加簡單:) –

+0

我們已經告訴過您(前一個問題)不要發佈您的整個代碼,而只是與您的問題相關的[mcve]。所有這些信息只是分散注意力 – Steve

+0

你是什麼意思顯示客戶信息?你想只打印出所有的屬性到控制檯的客戶對象? –

回答

0

嘗試這樣的事情。 我不明白爲什麼你需要GetID()和方法UserChoise()永遠不會被調用。 用這個替換GetID()方法並添加ShowCustromerInfo()。

public static void GetID() 
{ 
    //id the user enters as input 
    string enteredID = ""; 

    do 
    { 
     Console.Write("Please enter user ID: "); 
     enteredID = Console.ReadLine(); 
     bool isFound = false; 

     foreach (var customer in preferredCustomers) 
     { 
      if (customer.CustomerID == enteredID) 
      { 
       isFound = true; 
       ShowCustromerInfo(custromer); 
       return; 
      } 
     } 

     if (!isFound) 
      Console.Write("ID does not exist. "); 

    } while (true); 
} 

public static void ShowCustromerInfo(PreferredCustomer custromer) 
{ 
    Console.WriteLine("{0}:{1}:{2}:{3}:{4}:{5}:{6}", 
    custromer.CustomerName, custromer.Address, custromer.Phone, custromer.CustomerID, custromer.CustomerID, custromer.CalcAmount(), custromer.OnEmailList == true ? "true" : "false"); 
} 
+0

感謝您的幫助! ShowCustomerInfo崩潰,但我能夠得到它的工作。 – Alex

0

我改變ShowCustomerInfo這個代碼,我想知道是否有添加或刪除用戶,好像他們是陣列的方法?例如,他們是5個客戶,如果要刪除1個客戶,是否有辦法做到這一點?

public static void ShowCustomerInfo(PreferredCustomer customer) 
    { 
     using (var reader = new StreamReader("CustomerInfo.txt")) 
     { 
      var index = 0; 
      preferredCustomers = new PreferredCustomer[5]; 
      while (!reader.EndOfStream) 
      { 
       var line = reader.ReadLine().Split(':'); 
       var name = line[0]; 
       var address = line[1]; 
       var phone = line[2]; 
       var id = line[3]; 
       var email = line[4]; 
       var spentAmount = Convert.ToInt32(line[5]); 
       var onEmailList = Convert.ToBoolean(line[6]); 
       preferredCustomers[index] = new PreferredCustomer(name, address, phone, id, email, spentAmount, 
        onEmailList); 
       index++; 

       Console.WriteLine("{0}:{1}:{2}:{3}:{4}:{5}:{6}", 
        name, address, phone, id, email, spentAmount,onEmailList); 
      } 
     } 
    } 
相關問題