c#代码有问题

本文关键字:有问题 代码 | 更新日期: 2023-09-27 18:12:31

这是一个简单的控制台应用程序出租房间。我对Bill方法有一个问题,当我测试应用程序时,它总是0。我用房间价格预订了数天,但有些事情是错误的。有人能帮帮我吗?

static void Main(string[] args)
    {
        Room RoomOne = new Room(11, 1, 6, false);
        Room RoomTwo = new Room(21, 2, 5, false);
        Room RoomThree = new Room(31, 3, 9, true);
        Customer CustomerOne = new Customer("Bob Marley", "Male", 39, 1);
        Customer CustomerTwo = new Customer("Isaac Newton", "Male", 67, 2);
        Customer CustomerThree = new Customer("Frankenstein", "Male", 50, 3);
        Console.WriteLine("");
        CustomerOne.ReserveRoom();
        RoomOne.IsRoomOccupied();
        Console.WriteLine("");
        CustomerTwo.ReserveRoom();
        RoomTwo.IsRoomOccupied();
        Console.WriteLine("");
        CustomerThree.ReserveRoom();
        RoomThree.IsRoomOccupied();
        Console.ReadKey();
    }
class Customer : Room
{
    private string Name;
    private string Gendre;
    private int Age;
    private int ID;
    public Customer()
    {
    }
    public Customer(string name, string gendre, int age, int id)
    {
        Name = name;
        Gendre = gendre;
        Age = age;
        ID = id;
    }
    public void ReserveRoom()
    {
        Console.WriteLine("Welcome to Plaza Hotel!!!'nWhat kind of room would you like? We have Standard, Moderate and Superior.");
        string roomtype = Console.ReadLine();
        switch (roomtype)
        {
            case "Standard":
                    {
                        Standard RoomOne = new Standard();
                    }
                    break;
            case "Moderate":
                    {
                        Standard RoomTwo = new Standard();
                    }
                    break;
            case "Superior":
                    {
                        Standard RoomThree = new Standard();
                    }
                    break;
            default:
                {
                    Console.WriteLine("That type of room doesn't exist!!!");
                }
                break;
        }
    }
}
 class Room
{
    private int RoomNumber;
    private int FloorNumber;
    private double Price;
    private int NumberOfDaysOccupied;
    private bool Occupied = false;
    private string TypeOfRoom;
    public Room()
    {
    }
    public Room(int room, int floor,int nodo, bool occupied)
    {
        RoomNumber = room;
        FloorNumber = floor;
        NumberOfDaysOccupied = nodo;
        Occupied = occupied;
    }
    public void NumberOfRoom()
    {
        Console.WriteLine("Room number is: {0}", RoomNumber);
    }
    public void NumberOfFloor()
    {
        Console.WriteLine("Floor number is: {0}", FloorNumber);
    }
    public double Bill()
    {
        return NumberOfDaysOccupied * Price;
    }
    public void IsRoomOccupied()
    {
        if (Occupied == true)
        {
            Console.WriteLine("This type of room is occupied!!!");
        }
        else
        {
            Console.WriteLine("Your room number is: {0} on floor: {1}, reserve for: {2} and your bill is: {3}" ,RoomNumber, FloorNumber, NumberOfDaysOccupied, Bill());
        }
    }
    public class Standard : Room
    {
        public Standard()
        {
            Price = 50;
            TypeOfRoom = "Standard";
        }
    }
    public class Moderate : Room
    {
        public Moderate()
        {
            Price = 100;
            TypeOfRoom = "Moderate";
        }
    }
    public class Superior : Room
    {
        public Superior()
        {
            Price = 200;
            TypeOfRoom = "Superior";
        }
    }
}

c#代码有问题

您的Standard/Moderate/Superior类继承了Room,但不调用Room的构造函数来设置价格。当不提供要调用的基类构造函数时,将调用默认构造函数,该构造函数不设置任何选项。

你需要更新你的房间,更像:

public class Standard : Room
{
    public Standard(int room, int floor, int nodo, bool occupied)
        : base(room, floor, nodo, occupied)
    {
        Price = 50;
        TypeOfRoom = "Standard";
    }
}

然后设置你的房间:

Room standardRoom = new Standard(11, 1, 6, false);