简体   繁体   English

代码 C# 有问题

[英]Something wrong with code C#

This is simple console app for rent rooms.这是用于出租房间的简单控制台应用程序。 I have a problem with method Bill, it's always 0 when I test app.我的方法 Bill 有问题,当我测试应用程序时它总是 0。 I multiple number of reserve days with price of room but somethig is wrong.我用房间价格多次预订天数,但有些错误。 Can someone help me?有人能帮我吗?

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

Your Standard/Moderate/Superior classes inherit Room, but do not call the constructor on Room that sets the price.您的 Standard/Moderate/Superior 类继承 Room,但不要在 Room 上调用设置价格的构造函数。 When you don't provide the base class constructor to call, the default constructor will be called, which doesn't set any of the options.当您不提供要调用的基类构造函数时,将调用默认构造函数,它不会设置任何选项。

You'll need to update your rooms to be more like:您需要更新您的房间,使其更像:

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

Then setup your rooms like:然后设置你的房间,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM