加载和保存类实例数组 [英] Load and Save Array of Class instances

查看:101
本文介绍了加载和保存类实例数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当我创建Cars数组时,在我关闭应用程序时将其删除.我想知道是否有一种方法可以使我每次打开应用程序时都可以保存并加载一系列汽车?我是编码新手,所以请对我轻松一点

Currently when i create the array of Cars, it is deleted when i close the application. I was wondering if there was a way i could save and load the array of cars every time i opened the app? I am new to coding so please go easy on me lol

namespace CarPark
{
    class Program
    {
        public static System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\user\Google Drive\ICT Computer Programming\CarPark\CarPark\array.txt");

        static void Main(string[] args)
        { 
            bool displayMenu = true;
            
            while (displayMenu)
            {
                displayMenu = MainMenu();
            }

            Program.file.Close();
        }

        public static bool MainMenu()
        {
            Console.Clear();
            Console.WriteLine("Please choose an option: ");
            Console.WriteLine("1) Store a new Car");
            Console.WriteLine("2) Car Code look up");
            Console.WriteLine("3) List all Car Codes");
            Console.WriteLine("4) Delete Car entry");
            Console.WriteLine("5) Search cars");
            Console.WriteLine("6) Modify car");
            Console.WriteLine("E) Exit");

            string entry = Console.ReadLine();
            if (entry == "1")
            {
                Car.StoreCar(); return true;
            }
            else if (entry == "2")
            {
                Car.CarDetails(); return true;
            }
            else if (entry == "3")
            {
                Car.CarIDs(); return true;
            }
            else if (entry == "4")
            {
                Car.DeleteCar(); return true;
            }
            else if (entry == "5")
            {
                carSearch(); return true;
            }
            else if (entry == "6")
            {
                Modify.Mod(); return true;
            }
            else if (entry == "E" || entry == "e")
                return false;
            else
                return true;
        }

        private static void carSearch()
        {
            Console.Clear();
            Console.WriteLine("What would you like to search for?");
            Console.WriteLine("1)Search Car make");
            Console.WriteLine("2)Search Car years");
            Console.WriteLine("3)Search Car reg");
            Console.WriteLine("4)Exit");
            string entry = Console.ReadLine();

            if (entry == "1")
                Search.Make();
            else if (entry == "2")
                Search.Year();
            else if (entry == "3")
                Search.Reg();
            else if (entry == "4") { }
            else { }
         }
    }


    class Car
    {
        public static Car[] cars = new Car[10001];

        public int Code { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public string Colour { get; set; }
        public string Reg { get; set; }
        public DateTime Time { get; set; }

        public Car(int code, string make, string model, int year, string colour, string reg, DateTime time)
        {
            Code = code;
            Make = make;
            Model = model;
            Year = year;
            Colour = colour;
            Reg = reg;
            Time = time;
        }

        public static void StoreCar()
        {
            int code = CarCode();
            Console.Clear();
            Console.WriteLine("Please enter the details of your car: (entry code - {0})", code);
            Console.Write("\nMake of car: ");
            string make = Console.ReadLine();

            Console.Write("\nModel of car: ");
            string model = Console.ReadLine();

            Console.Write("\nYear of car: ");
            string yr = Console.ReadLine();
            int year;
            Int32.TryParse(yr, out year);

            Console.Write("\nColour of car: ");
            string colour = Console.ReadLine();
            Console.Write("\nRegistration of car: ");
            string reg = Console.ReadLine();

            DateTime time = DateTime.Now;

            Car newCar = new Car(code,
                                    make.ToUpper().Replace(" ", ""),
                                    model.ToUpper().Replace(" ", ""),
                                    year,
                                    colour.ToUpper().Replace(" ", ""),
                                    reg.ToUpper().Replace(" ", ""),
                                    time);

            cars[code] = newCar;

            Console.WriteLine("\nCar successfully saved under ID {0}, at {1}", code, time);
            Console.WriteLine("\nPress Enter to return to Main Menu.");
            Console.ReadLine();
            

            string car = cars[code].Code + " " + cars[code].Make + " " + cars[code].Model + " " + cars[code].Year.ToString()+ " " + cars[code].Colour + " " + cars[code].Reg;
            
            Program.file.WriteLine(car);
            

        }

        public static void CarDetails()
        {
            Console.Clear();
            Console.Write("Please enter the entry code for the car you are looking for: ");
            string ent = Console.ReadLine();
            int code;
            Int32.TryParse(ent, out code);

            if (cars[code] == null)
                Console.WriteLine("Invalid Car Code!");
            else
            {
                Console.Write("\nCar Code: {0}", cars[code].Code);
                Console.Write("\nCar Make: {0}", cars[code].Make);
                Console.Write("\nCar Model: {0}", cars[code].Model);
                Console.Write("\nCar Year: {0}", cars[code].Year);
                Console.Write("\nCar Colour: {0}", cars[code].Colour);
                Console.Write("\nCar Reg: {0}", cars[code].Reg);
                Console.Write("\nTime Car was entered: {0}", cars[code].Time);
            }

            Console.WriteLine("\n\nPress Enter to return to Main Menu.");
            Console.ReadLine();
        }

        public static void CarIDs()
        {
            Console.Clear();
            Console.WriteLine("List of all stored cars: \n");
            int id = 1;
            for (int i = 0; i < cars.Length; i++)
            {
                if (cars[i] == null)
                { }
                else
                {
                    Console.WriteLine("{0}) ID {1}, {2}, {3}, {4}, {5}, {6}  Entered: {7}",
                        id,
                        cars[i].Code,
                        cars[i].Make,
                        cars[i].Model,
                        cars[i].Year,
                        cars[i].Colour,
                        cars[i].Reg,
                        cars[i].Time);
                    id++;
                }
            }
            Console.WriteLine("\nPress Enter to return to Main Menu.");
            Console.ReadLine();
        }

        public static void DeleteCar()
        {
            bool display = true;
            while (display)
            {
                Console.Clear();
                Console.Write("Please enter the ID of the car you want to delete: ");
                string input = Console.ReadLine();
                int code;
                Int32.TryParse(input, out code);
                Console.WriteLine("\nAre you sure you want to delete car with ID {0}?" +
                    "\n\n(Hit Y for yes/N to Cancel/R to choose another car)", code);

                ConsoleKeyInfo ent = Console.ReadKey();

                if (ent.KeyChar == 'y')
                {
                    Console.Clear();
                    cars[code] = null;
                    Console.WriteLine("\nCar Deleted!");
                    display = false;
                }
                else if (ent.KeyChar == 'r')
                    display = true;
                else if (ent.KeyChar == 'n')
                {
                    Console.Clear();
                    Console.WriteLine("\nCancelled!");
                    display = false;
                }
                else
                {
                    Console.WriteLine("\nInvalid Entry! Press any key.");
                    Console.ReadKey();
                }
            }

            Console.WriteLine("\nPress Enter to return to Main Menu.");
            Console.ReadLine();
        }

        private static int CarCode()
        {
            Random myRandom = new Random();
            int code = myRandom.Next(1, 10001);

            while (cars[code] != null)
                code = myRandom.Next(1, 10001);
            
            return code;
        }

    }

    class Search
        {

        public static void Reg()
        {
            Console.Clear();
            Console.WriteLine("What reg are you looking for? ");
            string ent = Console.ReadLine();
            ent = ent.ToUpper().Replace(" ", "");

            for (int i = 0; i < Car.cars.Length; i++)
            {
                if (Car.cars[i] == null)
                {}
                else if (Car.cars[i].Reg == ent)
                    Console.WriteLine("\nID {0}, {1}, {2}, {3}  Entered: {4}", Car.cars[i].Code, Car.cars[i].Make, Car.cars[i].Model, Car.cars[i].Reg, Car.cars[i].Time);
            }

            Console.WriteLine("\nPress Enter to return to Main Menu.");
            Console.ReadLine();
        }

        public static void Make()
        {
            Console.Clear();
            Console.WriteLine("What make car are you looking for? ");
            string ent = Console.ReadLine();
            ent = ent.ToUpper().Replace(" ", "");

            for (int i = 0; i < Car.cars.Length; i++)
            {
                if (Car.cars[i] == null)
                {}
                else if (Car.cars[i].Make == ent)
                    Console.WriteLine("\nID {0}, {1}, {2}, {3}  Entered: {4}", Car.cars[i].Code, Car.cars[i].Make, Car.cars[i].Model, Car.cars[i].Reg, Car.cars[i].Time);
            }

            Console.WriteLine("\nPress Enter to return to Main Menu.");
            Console.ReadLine();
        }

        public static void Year()
        {
            Console.Clear();
            Console.WriteLine("What year car are you looking for? ");
            string inp = Console.ReadLine();
            int ent;
            Int32.TryParse(inp, out ent);

            for (int i = 0; i < Car.cars.Length; i++)
            {
                if (Car.cars[i] == null)
                { }
                else if (Car.cars[i].Year == ent)
                    Console.WriteLine("\nID {0}, {1}, {2}, {3}  Entered: {4}", Car.cars[i].Code, Car.cars[i].Make, Car.cars[i].Model, Car.cars[i].Reg, Car.cars[i].Time);
            }

            Console.WriteLine("\nPress Enter to return to Main Menu.");
            Console.ReadLine();
        }
    }

    class Modify
    {
        public static int c;

        public static void Mod()
        {
            Console.Clear();

            while (true)
            {
                Console.Write("Enter the Car Code of the car you want to modify: (0 to Exit)");
                Int32.TryParse(Console.ReadLine(), out c);

                if (c == 0)
                    break;
                else if (Car.cars[c] == null)
                    Console.WriteLine("There is no car with ID of {0}\n", c);
                else break;
            }

            Console.Clear();
            Console.WriteLine("What would you like to change about his car?");
            Console.WriteLine("0) Exit");
            Console.WriteLine("1) Make");
            Console.WriteLine("2) Model");
            Console.WriteLine("3) Year");
            Console.WriteLine("4) Colour");
            Console.WriteLine("5) Reg");
            Console.WriteLine("\n\nID {0}, {1}, {2}, {3}, {4}, {5}  Entered: {6}",
                        Car.cars[c].Code,
                        Car.cars[c].Make,
                        Car.cars[c].Model,
                        Car.cars[c].Year,
                        Car.cars[c].Colour,
                        Car.cars[c].Reg,
                        Car.cars[c].Time);

            string entry = Console.ReadLine();
            if (entry == "1")
                Make();
            else if (entry == "2")
                Model();
            else if (entry == "3")
                Year();
            else if (entry == "4")
                Colour();
            else if (entry == "5")
                Reg();
            else if (entry == "0") { }
            else
            {
                Console.WriteLine("Invalid Option, Hit Enter to return to Main Menu");
                Console.ReadLine();
            }
        }

        public static void Make()
        {
            Console.Clear();
            Console.Write("Enter the New Make for this car: ");
            Car.cars[c].Make = Console.ReadLine().ToUpper().Replace(" ", "");

            Console.WriteLine("\nThe make of car {0} has now been changed to {1}.", c, Car.cars[c].Make);
            Console.ReadLine();
        }

        public static void Model()
        {
            Console.Clear();
            Console.Write("Enter the New Model for this car: ");
            Car.cars[c].Model = Console.ReadLine().ToUpper().Replace(" ", "");

            Console.WriteLine("\nThe Model of car {0} has now been changed to {1}.", c, Car.cars[c].Model);
            Console.ReadLine();
        }

        public static void Year()
        {
            Console.Clear();
            Console.Write("Enter the New Make for this car: ");
            int x;
            Int32.TryParse(Console.ReadLine(), out x);
            Car.cars[c].Year = x;

            Console.WriteLine("\nThe Year of car {0} has now been changed to {1}.", c, Car.cars[c].Year);
            Console.ReadLine();
        }

        public static void Colour()
        {
            Console.Clear();
            Console.Write("Enter the New Colour for this car: ");
            Car.cars[c].Colour = Console.ReadLine().ToUpper().Replace(" ", "");

            Console.WriteLine("\nThe Colour of car {0} has now been changed to {1}.", c, Car.cars[c].Colour);
            Console.ReadLine();
        }

        public static void Reg()
        {
            Console.Clear();
            Console.Write("Enter the New Reg for this car: ");
            Car.cars[c].Reg = Console.ReadLine().ToUpper().Replace(" ", "");

            Console.WriteLine("\nThe Reg of car {0} has now been changed to {1}.", c, Car.cars[c].Reg);
            Console.ReadLine();
        }
    }
}

推荐答案

在关闭应用程序时删除数组的原因是因为数组存储在其中一个数组的内存中堆栈或堆.或更简单地说:它存储在您计算机的RAM中.当您关闭应用程序时,此内存为 已清除,数组中的数据也消失了.

The reason your array is being deleted when you close the application is because your array is being stored in memory on either the stack or the heap. Or more simply said: it is stored in your computers RAM. When you close the application, this memory is cleared and the data in your array is gone.

您要使用的是一个(SQL)数据库,用于存储信息,即使您关闭该应用程序也是如此.这样,您可以在重新打开应用程序时再次检索数据.

一些有用的链接:

与数据库通信
https://msdn.microsoft.com/zh-CN /library/bb882660(v=vs.110).aspx

使用ADO.NET创建一个简单的数据应用程序
https://msdn.microsoft.com/zh-CN/library/jj943772.aspx

初学者教程
如何将SQL数据库连接到C#程序
https://www.codeproject. com/Articles/823854/How-to-connect-SQL-Database-to-your-Csharp-progra

What you want to use is a (SQL) database where the information is stored, even when you close the application. This way you can retrieve the data again when re-opening the application.

Some helpful links:

Communicating with the Database
https://msdn.microsoft.com/en-us/library/bb882660(v=vs.110).aspx

Create a simple data application by using ADO.NET
https://msdn.microsoft.com/en-us/library/jj943772.aspx

How to connect SQL Database to your C# program, beginner's tutorial
https://www.codeproject.com/Articles/823854/How-to-connect-SQL-Database-to-your-Csharp-progra


这篇关于加载和保存类实例数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆