1 Attachment(s)
[RESOLVED] c# Hotel Check-In / Check-Out System using objects
EDIT: See Second Post
Basically I am trying to create a program that allows someone to check in new guests to rooms that are available.
This is what I have so far to create my guests and rooms and add the rooms to a list.
I am wanting to know how I can loop through each room in the "AllRooms" List and check whether its property of booked is true or false and check the room type against a combobox which has the 5 types of room in and then display all the room numbers that are not booked and are of that room type in a list box on the right hand side - See screenshot of design. All help appreciated.
Room Types are: Single Room, Double Room, Queen Suite, King Suite, Penthouse Suite
Attachment 126231
RoomClass
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hotel_Check_In
{
public class Room
{
public static List<Room> AllRooms;
public int RoomNumber { get { return this.RoomNumber; } set { this.RoomNumber = value; } }
public string RoomType { get { return this.RoomType; } set { this.RoomType = value; } }
public Boolean booked { get { return this.booked; } set { this.booked = value; } }
public Room(int roomNumber)
{
RoomNumber = roomNumber; //adds room Number to room number property
}
public Room(int roomNumber, string roomType)
{
RoomNumber = roomNumber;
RoomType = roomType;
}
public void AddRooms(int noOfRooms, string roomType) //Add new Rooms (method for adding rooms to list on load)
{
if (AllRooms.Count == 0) //check if any rooms exist already
{
for (int i = 1; i <= noOfRooms; i++)
{
Room newRoom = new Room(i);
AllRooms.Add(newRoom);
newRoom.RoomType = roomType;
newRoom.booked = false;
}
} else {
for (int i = AllRooms.Count + 1; i <= noOfRooms; i++) //if rooms exist then +1 onto current room total so no room has the same number
{
Room newRoom = new Room(i);
AllRooms.Add(newRoom);
newRoom.RoomType = roomType;
newRoom.booked = false;
}
}
}
}
}
GuestClass
Code:
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 Name {get {return this.Name;} set {this.Name = value;}}
public int rNumber {get {return this.rNumber;} set {this.rNumber = value;}}
public DateTime arrivalDateTime { get { return this.arrivalDateTime; } set { this.arrivalDateTime = value; } }
public DateTime leavingDateTime { get { return this.leavingDateTime; } set { this.leavingDateTime = value; } }
public static List<Guest> AllGuests;
public Guest()
{ }
public Guest(string name, Room allocatedRoom)
{
Name = name;
rNumber = allocatedRoom.RoomNumber;
}
public static void CheckIn(string GuestName, Room room, DateTime checkinDateTime, DateTime LeavingDateTime)
{
Guest newGuest = new Guest(GuestName, room);
}
}
}
Cannot implicitly convert type(missing a cast?)
So after a few hours I have finally come up with a way to do what I wanted to achieve with a Tuple type ( I think anyway )
Would someone please be able to tell me why I am getting this error this function Ive created?
Code:
Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Tuple<int,string>>' to 'System.Tuple<int,string>'. An explicit conversion exists (are you missing a cast?)
Code:
public Tuple<int,string> checkBookedRooms()
{
List<int> RoomNumList = new List<int>();
List<string> RoomTypeList = new List<string>();
int i = 0;
foreach(Room room in AllRooms)
{
if(room.booked==false)
{
RoomNumList.Add(room.RoomNumber);
RoomTypeList.Add(room.RoomType);
i++;
if(i == AllRooms.Count)
{
var roomNumbersAvailable = new int[RoomNumList.Count]; RoomNumList.ToArray();
var roomTypesAvailable = new string[RoomTypeList.Count]; RoomTypeList.ToArray();
var RoomsAvailable = roomNumbersAvailable.Zip(roomTypesAvailable, Tuple.Create);
return RoomsAvailable;
i = 0;
}
}
}
}
Re: c# Hotel Check-In / Check-Out System using objects
After hours of looking through documentation I finally found my solution, Hashtables.
For future reference this is my answer to my question:
Code:
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;
}