Пример #1
0
 public static AirbnbMain GetInstance()
 {
     if (instance == null)
     {
         instance = new AirbnbMain();
     }
     return(instance);
 }
Пример #2
0
        public bool SaveData()
        {
            if (fileName == null || fileName.Length == 0)
            {
                return(false);
            }
            else
            {
                try
                {
                    // Open up a new streamwriter to save our file with
                    StreamWriter writer = new StreamWriter(fileName);

                    // For each district in the districts array
                    for (int i = 1; i < districts.Length; i++)
                    {
                        // Get district from the instance of AirbnbMain
                        District district = AirbnbMain.GetInstance().GetDistricts()[i];

                        writer.WriteLine(district.GetName());
                        writer.WriteLine(district.GetNumInCollection());
                        // if district doesnt have 0 neighbourhoods
                        if (district.GetNumInCollection() != 0)
                        {
                            Neighbourhood[] neighbourhoods = district.GetNeighbourhoods();
                            // For each neighbourhood in te district
                            for (int y = 0; y < neighbourhoods.Length; y++)
                            {
                                Neighbourhood neighbourhood = neighbourhoods[y];

                                if (neighbourhood.GetName() == null)
                                {
                                    writer.WriteLine("Error in name");
                                }
                                else
                                {
                                    writer.WriteLine(neighbourhood.GetName());
                                }
                                // Write number of neighbourhoods to new line
                                writer.WriteLine(neighbourhood.GetNumInCollection());

                                Property[] properties = neighbourhood.GetProperties();
                                // For each property in the neighbourhood
                                for (int z = 0; z < neighbourhood.GetNumInCollection(); z++)
                                {
                                    Property property = properties[z];

                                    // Write fields to line from relevant 'Get' calls

                                    writer.WriteLine(property.GetId());
                                    writer.WriteLine(property.GetName());
                                    writer.WriteLine(property.GetHostId());
                                    writer.WriteLine(property.GetHostName());
                                    writer.WriteLine(property.GetNumHostProperties());
                                    writer.WriteLine(property.GetLatitude());
                                    writer.WriteLine(property.GetLongitude());
                                    writer.WriteLine(property.GetRoomType());
                                    writer.WriteLine(property.GetPrice());
                                    writer.WriteLine(property.GetMinNightsStay());
                                    writer.WriteLine(property.GetAvailableDaysPerYear());
                                }
                            }
                        }
                    }

                    // Close the writer to avoid errors;
                    writer.Close();
                }
                catch (FileNotFoundException e) // Catch file not found error, can't find file
                {
                    Debug.WriteLine("Unable to find the data file. " + e.Message);
                    MessageBox.Show("Unable to find the data file. " + e.Message);
                }
                catch (IOException e) // Catch IOException, something went't wrong while handling the file
                {
                    Debug.WriteLine("Error occurred while handling data file. " + e.Message);
                    MessageBox.Show("Error occurred while handling data file. " + e.Message);
                }

                return(true);
            }
        }