Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有方法显示此菜单项目上的客户信息?_C# - Fatal编程技术网

C# 是否有方法显示此菜单项目上的客户信息?

C# 是否有方法显示此菜单项目上的客户信息?,c#,C#,我试图建立一个项目,我可以管理客户,所以我可以查看那里的信息,并能够添加或删除客户。是否有办法通过按“DisplayMainMenu()”上的2键来显示客户信息?然后它会要求您的ID显示客户信息?我在406号线上试过,但没能做到。我猜我必须从列表中获取数组?如果是这样,我如何改进代码 Program.cs using System; using System.Collections.Generic; using System.IO; using System.Linq; using System

我试图建立一个项目,我可以管理客户,所以我可以查看那里的信息,并能够添加或删除客户。是否有办法通过按“DisplayMainMenu()”上的2键来显示客户信息?然后它会要求您的ID显示客户信息?我在406号线上试过,但没能做到。我猜我必须从列表中获取数组?如果是这样,我如何改进代码

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: ");
    }
}
}
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@gmail.com:2500:true
Albert Gomez:456 15th st, Austin, TX 78504:512-456-1000:A0000002:bobsmith@gmail.com:2500:true
Jose Martinez:2004 44th st, Washington, DC 20001:202-456-2222:A0000003:williamc@gmail.com:495:false
Joseph Olivas:123 16th st, Washington, DC 20002:202-555-6666:A0000004:garnerp@gmail.com:1200:true
Pablo Cortez:777 2th st, Houston, TX 77002:832-100-2000:A0000005:pcorzbin@yahoo.com:1750:false
试试这样的。 我不明白为什么需要GetID()而方法UserChoise()从未被调用。 将GetID()方法替换为此方法并添加ShowCustromerInfo()


我将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);
            }
        }
    }

所有这些代码都与您遇到的问题相关吗?请编辑您的问题,使其仅包含与您的问题相关的代码。这将使我们的生活变得更加轻松:)我们已经告诉你(上一个问题)不要发布你的全部代码,而只发布与你的问题相关的代码。所有这些信息都会让人分心,显示客户信息是什么意思?您是否只想将所有属性打印到客户对象的控制台?是的,我想将所有属性打印出来。谢谢您的帮助!ShowCustomerInfo崩溃了,但我能让它工作。
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");
}
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);
            }
        }
    }