【问题标题】:Rooms and Guests (object add to another object?)房间和客人(对象添加到另一个对象?)
【发布时间】:2015-07-09 01:49:22
【问题描述】:

基本上,我正在创建一个酒店登记系统,我想创建房间 1-100,然后当我为客人创建一个新对象时,如果你得到我,我想将该客人分配给某个房间对象。这是迄今为止我为客人和房间类所做的。

编辑:很抱歉没有说清楚我在问什么。如果可能的话,我想知道如何使用leadName 属性将访客对象连接到房间对象?此外,存储所有这些房间及其所有财产(例如入住其中的客人等)的最佳方式是什么?

房间等级:

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

namespace Hotel_Check_In
{
 public class Room
    {
     public int RoomNumber;
     public string RoomType;
     public string SmokingAllowed;
     public Boolean booked;
     private Guest LeadName;

     public Room()
     { }
    }
}

嘉宾班:

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

namespace Hotel_Check_In
{
    public class Guest
    {
       public string LeadName;
       public int roomNumber;

       public Guest(string name, int roomN)
       {
          LeadName = name;
          roomNumber = roomN;
       }
    }    
}

【问题讨论】:

  • 那么,问题是什么?
  • 请edit您的问题添加有关您遇到问题或需要帮助的信息。请描述您的代码的预期行为与您观察到的情况以及您自己无法找到原因的原因/位置。

标签: c# object


【解决方案1】:

看看有没有帮助

namespace Hotel_Check_In
{
    public class Room
    {
     public static List<Room> rooms = null;
     public int RoomNumber;
     public string RoomType;
     public string SmokingAllowed;
     public Boolean booked;
     private Guest LeadName;

     public Room(int numberOfRooms)
     {
         for(int i = 1; i <= numberOfRooms ; i++)
         {
             Room newRoom = new Room();
             rooms.Add(newRoom);
             newRoom.RoomNumber = i;
         }
     }

    }

}

【讨论】:

  • 仅供参考,代码 sn-p 功能适用于 JavaScript、HTML 等中的可执行代码 sn-ps。
  • 您是否考虑过使用 SQL Server 数据库进行存储?您可以从 msdn.com 免费下载 Express 版本。
【解决方案2】:

试试这个以获得领先优势。我将 Guest 设为公开而不是私有。

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

namespace Hotel_Check_In
{
    class Program
    {
        static void Main(string[] args)
        {
            string lead = "JohnSmith";
            Room room = Room.rooms.AsEnumerable()
                .Where(x => x.LeadName.LeadName == lead).FirstOrDefault();
        }
    }
    public class Room
    {
        public static List<Room> rooms = null;
        public int RoomNumber;
        public string RoomType;
        public string SmokingAllowed;
        public Boolean booked;
        public Guest LeadName;

        public Room(int numberOfRooms)
        {
            for (int i = 1; i <= numberOfRooms; i++)
            {
                Room newRoom = new Room();
                rooms.Add(newRoom);
                newRoom.RoomNumber = i;
            }
        }

    }
    public class Guest
    {
        public string LeadName;
        public int roomNumber;

        public Guest(string name, int roomN)
        {
            LeadName = name;
            roomNumber = roomN;
        }
    }    
}

【讨论】:

    【解决方案3】:

    彻底改了,用房间号用Hashtables连接房间和客人。

    public static Hashtable checkBookedRooms(string Rtype)
         {
             Hashtable roomsAvailable = new Hashtable();
             int i = 0;
    
            foreach(Room room in AllRooms)
            {
                i++;
                if(room.booked==false && room.RoomType == Rtype)
                {
                    roomsAvailable.Add(room.RoomNumber, room.RoomType);
                }  
                if(i >= AllRooms.Count)
                {
                    i = 0;
                    return roomsAvailable;
    
                }
            }
            Console.WriteLine("Room.checkBookedRooms() Returned Null");
            return null;
         }
    

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多