代码C#有问题#

时间:2015-09-13 23:56:44

标签: c#

这是租赁房间的简单控制台应用程序。我有方法Bill的问题,当我测试app时它总是0。我有多个预订天数与房间的价格,但somethig是错误的。有人能帮助我吗?

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";
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

您的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);