简体   繁体   English

很难确定我的设计的起点

[英]Having a hard time determining the starting point of my design

For my CS course I have to program a hotel checkin/checkout system. 对于我的CS课程,我必须编制一个酒店登记/结账系统。 The program must be able to check people in and out. 该计划必须能够检查人员进出。 The programm assigns the first available room to a guest upon checkin. 该计划在入住时为客人分配第一个可用房间。 If no room is free, it will say so as well. 如果没有免费房间,它也会这么说。 The Hotel has four rooms. 酒店有四个房间。

In the assignment it says there need to be 4 classes: Assignment5, Hotel, Room, & Guest. 在作业中,它说需要有4个班级:Assignment5,Hotel,Room和Guest。

  1. Assignment5 class is for the interaction with the user. Assignment5类用于与用户交互。

  2. Hotel class has four rooms and all methods for operating the rooms. 酒店类有四个房间和所有操作房间的方法。

  3. Room class has 1 guest. 客房类有1位客人。 If the rooms is empty a guest can check in. If the guest is leaving the room needs to be emptied. 如果客房是空的,客人可以办理入住手续。如果客人要离开,则需要清空房间。

  4. Guest class: the guest has a firstname and a last name. 访客类:访客具有名字和姓氏。

In the menu there need to be 4 options: 1 show status of rooms available and occupation. 在菜单中需要有4个选项:1显示可用房间和职业的状态。 2 check in option 3 check out option 4 end program option. 2检查选项3检查选项4结束程序选项。

OK, so I know I should make my assignment for myself. 好的,所以我知道我应该为自己完成任务。 However, I don't know what it is, but starting with assignments I have great problems cutting the thing up in smaller pieces. 但是,我不知道它是什么,但从作业开始,我有很大的问题切割小块的东西。 Also this assignment is to learn working with different classes and I don't really understand which sequence of steps i should take in this case. 另外这个任务是学习使用不同的类,我真的不明白在这种情况下我应该采取哪些步骤。

Can someone help me getting started by giving some tips? 有人可以通过提供一些提示来帮助我开始吗? Have been staring at my screen for hours now and just thought I could use some little insights to get me started. 我一直盯着我的屏幕好几个小时,只是觉得我可以用一些小小的见解让我开始。 Any help is greatly apprecieted! 任何帮助都非常赞赏!

OK thnks for the help so far. 好了,到目前为止帮忙了。

* *First of all, MANY thanks for all your help, you guys are great! * *首先,非常感谢你的帮助,你们真棒! Have been at it for 7 hours straight now and still stuck. 已经连续7个小时了,仍然卡住了。 My problem now is that it doesn't compile. 我现在的问题是它不能编译。 It says: 它说:

Java:28: checkIn(Gast) in Hotel cannot be applied to (java.lang.String)
                hotel.checkIn("Guido");
                                     ^
1 error

And maybe, can someone look if this way i put it now is a little bit on the right path? 也许,有人看看我现在这样做的方式是在正确的道路上吗? I do thank JavaGeek for his program, but i want to learn it by doing myself. 我感谢JavaGeek的计划,但我想通过自己来学习它。

up until now I have the following: 到目前为止,我有以下内容:

   import java.util.Scanner;

public class bopgave5 {

    public static void main(String[] args) {

        boolean opnieuw = false;

        do {
            int invoer = menu();

            if (invoer == 2){

                Hotel hotel = new Hotel();
                hotel.checkIn("Guido");

                opnieuw = true;
            }

            else if (invoer == 4)
                opnieuw = false;

            else
            opnieuw = true;
        }
        while (opnieuw == true);

    }

    public static int menu (){
        Scanner sc = new Scanner(System.in);


        System.out.println("MENU:   [1] Statusoverzicht");
        System.out.println("    [2] Check-in");
        System.out.println("    [3] Check-out");
        System.out.println("    [4] Einde");
        System.out.print("Uw invoer: ");
        int invoer = sc.nextInt();  

        return invoer;

    }


}

class Hotel {

    Kamer kamer1, kamer2, kamer3, kamer4;

    Hotel (){
        kamer1 = new Kamer();
        kamer2 = new Kamer();
        kamer3 = new Kamer();
        kamer4 = new Kamer();
    }




    void checkIn(Gast nieuweGast) {


        if (kamer1.gast == null) {      
        kamer1.gast = nieuweGast;
        System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 1");
        return;
        }

        else if (kamer2.gast == null) {     
        kamer2.gast = nieuweGast;
        System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 2");
        return;
        }

        else if (kamer3.gast == null) {     
        kamer3.gast = nieuweGast;
        System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 3");
        return;
        }

        else if (kamer4.gast == null) {     
        kamer4.gast = nieuweGast;
        System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 5");
        return;
        }

        else {
        System.out.println("hotel is vol");
        return;
        }


    }                        

}

class Kamer {

    Gast gast;

    Kamer() {
        gast = null;
    }


}

class Gast {
    String naam;

    Gast(String nieuweNaam) {
        naam = nieuweNaam;
    }
}

So you have 4 classes. 所以你有4个班级。

In the assignment it says there need to be 4 classes: Assignment5, Hotel, Room, & Guest . 在作业中,它说需要有4个班级: Assignment5,Hotel,Room和Guest

With the division of responsibility as such: 责任分工如下:

Assignment5 class is for the interaction with the user . Assignment5类用于与用户交互

Hotel class has four rooms and all methods for operating the rooms . 酒店类有四个房间和所有操作房间的方法。 (extra emphasis: "rooms" is plural) (特别强调:“房间”是复数)

Room class has 1 guest. 客房类有1位客人。 If the rooms is empty a guest can check in. If the guest is leaving the room needs to be emptied. 如果客房是空的,客人可以办理入住手续。如果客人要离开,则需要清空房间。 (or in other word, operating a single room) (或换句话说,操作单个房间)

Guest class: the guest has a firstname and a last name. 访客类:访客具有名字和姓氏。

First, you'd probably want to identify the "state" that each object would have. 首先,您可能想要确定每个对象所具有的“状态”。 IOW, you need to determine the attributes/instance fields that each object have. IOW,您需要确定每个对象具有的属性/实例字段。 Let's start with an example: Guest class: the guest has a firstname and a last name . 让我们从一个例子开始: Guest类:guest有一个名字和一个姓氏 . Do the same for all the other classes. 对所有其他类做同样的事情。

Next, you want to identify the methods that will be needed. 接下来,您需要确定所需的方法。 Let's start with another example: Room class has 1 guest. 让我们从另一个例子开始: Room class有1位客人。 If the rooms is empty a guest can check in . 如果客房是空的,客人可以办理入住手续 If the guest is leaving the room needs to be emptied . 如果客人离开房间需要清空 . On top of that, you'll need some constructors for each class. 最重要的是,每个类都需要一些构造函数。

Next, for each method, you want to find out the arguments that the method needs, and their return values, if they need to return a value. 接下来,对于每个方法,如果需要返回值,则需要查找方法所需的参数及其返回值。 For example, for a check in method, you'll need the Room and a Guest; 例如,对于入住方法,您需要房间和客人; and you'll need to check whether the room is empty before you can check in. 在您办理登机手续之前,您需要检查房间是否空了。

UPDATE : 更新

My problem now is: how can i make it work that there are 4 rooms and that after checking in 1 person, checking in a second person will put it in a different room? 我现在的问题是:如何让它工作,有4个房间,签入1人后,检查第二个人将把它放在另一个房间?

Your teacher has a good advice, break it into pieces. 你的老师有一个很好的建议,把它分成几块。

Basically, you have the problem: " Checking in the second person should put him in a different room ". 基本上,你有问题:“ 检查第二个人应该把他放在另一个房间 ”。

So, how do we break this down? 那么,我们如何打破这个呢? First, we need to find an empty room , so we need a method for that (say findEmptyRoom() ), and after we find a room that's available, we need to check in the guest into that room , so we need another method (say checkIn() ). 首先,我们需要找到一个空房间 ,所以我们需要一个方法(比如findEmptyRoom() ),在我们找到一个可用的房间后,我们需要检查客人进入那个房间 ,所以我们需要另一种方法(说checkIn() )。 So, we find an empty room, then we checked the guest into that room, then we're done. 所以,我们找到一个空房间,然后我们检查了客人进入那个房间,然后我们就完成了。

Next step, we break findEmptyRoom() . 下一步,我们打破findEmptyRoom() Our hotel has 4 rooms (let's say we store room1 , room2 , room3 , and room4 as member variables, alternatively, you can use an array if you already learn about it in class). 酒店拥有4间客房 (假设我们存储room1room2room3 ,和room4成员变量,或者,你可以使用数组,如果你已经在课堂上学习一下吧)。 To find which one of the four rooms are empty, we need to ask a room if it is empty ; 要找到四个房间中哪一个是空的,我们需要问一个房间是否空 ; so we need another method for that (say isEmpty() ). 所以我们需要另一种方法(比如isEmpty() )。 To find an empty room, ask room 1, if room1 is empty return room1 , otherwise ask room 2, if room2 is empty return room2 , otherwise ask room 3, if room3 is empty, return room3 , otherwise ask room 4, if room4 is empty, return room4 . 为了找到一个空房间,问室1个,如果room1是空回room1 ,否则问室2,如果room2是空回room2 ,否则问室3,如果room3为空,返回room3 ,否则问室4,如果room4是空,返回room4 Otherwise, all rooms are occupied. 否则,所有房间都被占用。 [note: you will need to figure out what to do if all rooms are occupied]. [注意:如果所有房间都被占用,你需要弄清楚要做什么]。 An array will make this process much easier, you just need to loop through the array, and ask if it's empty and return the room if it's empty, otherwise continue checking next room. 一个数组将使这个过程更容易,你只需要遍历数组,并询问它是否为空并返回房间,如果它是空的,否则继续检查下一个房间。

Next, we break checkIn(). 接下来,我们打破checkIn()。 So, what do checkIn() need to know to fulfill the checking in process? 那么, checkIn()需要知道什么来完成检查过程? It needs to know about two information: the room and the guest . 它需要知道两个信息: roomguest Since checkIn() is an instance method of a room class, room is implied, so we only need one parameter, ie guest . 由于checkIn()是一个房间的类的实例方法, room是隐含的,所以我们只需要一个参数,即guest So void checkIn(Guest guest) should set the guest member variable of the room. 所以void checkIn(Guest guest)应该设置房间的guest成员变量。

Next, we break isEmpty(). 接下来,我们打破isEmpty()。 How do we know if a room is empty? 我们怎么知道房间是空的? If a room has a guest inside it, then it's occupied, otherwise it's empty. 如果一个房间里面有一个客人,那么它就被占用了,否则就是空的。 So, isEmpty() checks if the guest member variable refers to a valid guest, returns true if it is valid guest, otherwise returns false. 因此, isEmpty()检查guest成员变量是否引用有效guest虚拟机,如果它是有效guest虚拟机则返回true,否则返回false。 In Java, null is often used to mark that something is not a valid object, so you can check whether the guest member variable is null (obviously, you'll need to set the guest member variable to null when a guest checked out). 在Java中, null通常用于标记某些内容不是有效对象,因此您可以检查guest成员变量是否为null (显然,当guest虚拟机签出时,您需要将guest成员变量设置为null)。

Do the same thing for the other processes. 为其他进程做同样的事情。 Break the problem down into smaller pieces until you're confident that you can work with a piece. 将问题分解成小块,直到你确信你可以使用一块。

UPDATE2: UPDATE2:

The error message you've got: 您收到的错误消息:

Java:28: checkIn(Gast) in Hotel cannot be applied to (java.lang.String)
                hotel.checkIn("Guido");

is because you're passing a String ( "Guido" ) to a function that takes a Gast argument ( void checkIn(Gast nieuweGast) {...} ). 是因为你将一个String( "Guido" )传递给一个带有Gast参数的函数( void checkIn(Gast nieuweGast) {...} )。 You should instead, pass a Gast object: 你应该传递一个Gast对象:

// declare a variable called g, with type Gast
Gast g;

// create a new Gast object, passing "Guido" as parameter 
// to Gast's constructor, and assign the new object to g
g = new Gast("Guido");

// call hotel.checkIn(Gast) function with g as the argument, 
// i.e. the Gast object we just created in the previous line
hotel.checkIn(g);

or more simply: 或更简单地说:

hotel.checkIn(new Gast("Guido"));

Short of solving this thing for you, here is what I would suggest you do. 没有为你解决这个问题,我建议你这样做。

Think of each operation that needs to be performed, and then diagram the logic that needs to take place to accomplish that operation. 考虑需要执行的每个操作,然后绘制为完成该操作而需要进行的逻辑。 Then, with a clear list of logical operations that need to take place to accomplish that task, think carefully about what components you can break that operation up into and where each of those sub-tasks should go. 然后,通过明确的完成该任务所需的逻辑操作列表,仔细考虑可以将操作分解为哪些组件以及每个子任务应该去哪里。 This will give you a good idea of what the class diagram should look like. 这将使您了解类图应该是什么样子。 Once you've diagrammed the logic, classes/methods, and implemented one part, move on to the next part. 一旦你绘制了逻辑,类/方法的图表并实现了一个部分,就转到下一部分。 At this point, you'll probably find that something wasn't structured in a way that allows it to be reused for the next task, so refactor it (modify the structure of the code) to make it more reusable. 此时,您可能会发现某些内容的结构不允许将其重用于下一个任务,因此请对其进行重构(修改代码结构)以使其更具可重用性。

For example, tackle the check-in operation. 例如,解决办理登机手续的问题。 What do you need to do first? 你需要先做什么? Well, first you need determine if there are any rooms available. 那么,首先你需要确定是否有空房。 How do you do that? 你是怎样做的? You need to ask all of the rooms (by your design, if I remember correctly) if they are available. 你需要问所有的房间(按照你的设计,如果我没记错的话)。 So, we can now identify that we need a method on the rooms to tell us that they are available. 因此,我们现在可以确定我们需要一个房间方法告诉我们它们是可用的。 Maybe tackle that first. 也许先解决这个问题。 Then the checking for a free room part. 然后检查免费房间部分。 That probably goes on the hotel class. 这可能发生在酒店课上。 You'll need to iterate over the rooms and ask them if they are free. 您需要遍历房间并询问他们是否有空。 After this, you need to decide what to do if there is a free room(s), or if there aren't any. 在此之后,如果有免费房间,或者没有空房,您需要决定该怎么办。 I guess you display an error message if there aren't? 如果没有,我猜您会显示错误消息? If there are free rooms, you need form the check-in operation. 如果有免费房间,您需要办理登机手续。 Write that. 写下来。 Rinse and repeat. 冲洗并重复。 Before long, you'll have a functioning program. 不久,你将有一个正常运作的程序。

The important thing to remember here is that there are MANY different ways to accomplish this in an object-oriented manner. 这里要记住的重要一点是,有很多不同的方法可以以面向对象的方式实现这一点。 When you are just learning OO design and the basics of programming, it is important to not get too hung up on the perfect design and to dive in. Then when you are in the middle of implementing it, I'm sure you'll say 'hey, if I do it this way it will come up much better'. 当你刚刚学习OO设计和编程的基础知识时,重要的是不要过于依赖完美的设计并潜入其中。然后当你正在实施它时,我相信你会说“嘿,如果我这样做,它会变得更好”。 I like to think of this as an iterative learning process. 我想将此视为一个迭代学习过程。

Start with the objects, the domain 从对象,域开始

Try giving the classes useful members. 尝试给类有用的成员。 What is a 'hotel' for this problem? 这个问题的“酒店”是什么? When they have useful members, what are useful operation on them, the methods? 当他们有有用的成员时,对他们有什么有用的操作,这些方法呢?

Fields: A room should have a room number. 字段:房间应该有房间号。 A room should know what guest is staying in that room. 一个房间应该知道客人住在那个房间里。 A guest should have a name. 客人应该有一个名字。 A hotel should have a collection of rooms, a name, an address? 酒店应该有一系列的房间,名字,地址? Maybe the address is useless here. 也许这里的地址没用。

Methods: A room should have a method to see if it's empty and what guest is staying there if it is not empty. 方法:一个房间应该有一个方法来查看它是否为空,如果它不是空的,那么客人留在那里。 A hotel should have a method that tells you how many rooms are available, or give you the actual rooms. 酒店应该有一种方法可以告诉您有多少房间,或者给您实际的房间。 All the guests that stay in the hotel? 所有入住酒店的客人? Etc, etc, etc. 等等等

You'll notice you will have to keep refining, changing, fixing your model to be able to meet the requirements. 你会发现你必须不断改进,改变,修复模型才能满足要求。 For example 'checking in' might need a new method in room you did not yet have. 例如,“登记入住”可能需要在您尚未拥有的房间内使用新方法。

//Guest.java
package hotelcheckinsystem;

class Guest {
  private String name;

  public String getName() {
   return name;
  }

  public Guest(String name) {
    super();
    this.name = name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

//Room.java
package hotelcheckinsystem;

class Room {
  private Guest guest;

  public Guest getGuest() {
    return guest;
  }

  public void setGuest(Guest guest) {
    this.guest = guest;
  }

  public void removeGuest() {
    guest.setName("");
  }

  public boolean isEmpty() {
    if (this.getGuest().getName().equalsIgnoreCase("")) {
      return true;
    }
    return false;
  }
}

//Hotel.java
package hotelcheckinsystem;

public class Hotel {
  private Room[] rooms = new Room[4];

  public Hotel() {
    for (int i = 0; i < 4; ++i) {
      rooms[i] = new Room();
      Guest guest = new Guest("");
      rooms[i].setGuest(guest);
    }
  }

  public void assignRoomToGuest(String name) {

    int i;
    for (i = 0; i < 4; ++i) {
      if (rooms[i].isEmpty()) {
        Guest guest = new Guest(name);
        rooms[i].setGuest(guest);
        System.out.println("Room number " + i + " assigned to " + name);
        return;
      }
    }
    if (i == 4) {
      System.out.println("No empty Rooms to assign to " + name);
    }
  }

  public void emptyRoom(int roomNo) {

    if (roomNo > 3) {
      System.out.println("please enter number between 0 to 3");
      return;
    }
    rooms[roomNo].removeGuest();
    System.out.println("Room number " + roomNo + " is empty now!.");
  }
}

//Main.java

import java.util.Scanner;
import hotelcheckinsystem.*;

public class Main {
  public static void main(String[] args) {
    Hotel hotel = new Hotel();
    while (true) {
      System.out.println("Enter the Option: ");
      System.out.println("1. Check in. \n2. Check out. \n3. Exit");
      Scanner sc = new Scanner(System.in);
      int option = sc.nextInt();
      switch (option) {
      case 1:
        System.out.println("Enter the guests name");
        hotel.assignRoomToGuest(sc.next());
        break;

      case 2:
        System.out.println("Enter the Room number");
        hotel.emptyRoom(sc.nextInt());
        break;

      case 3:
         System.exit(0);
      }
    }
  }
}

Sample Run:- 样品运行: -

Enter the Option: 输入选项:

  1. Check in. 报到。

  2. Check out. 查看。

  3. Exit 出口

1 1

Enter the guests name 输入客人姓名

empty

Room number 0 assigned to empty 房间号0指定为空

Enter the Option: 输入选项:

  1. Check in. 报到。

  2. Check out. 查看。

  3. Exit 出口

1 1

Enter the guests name 输入客人姓名

A 一种

Room number 1 assigned to A 1号房间分配给A

Enter the Option: 输入选项:

  1. Check in. 报到。

  2. Check out. 查看。

  3. Exit 出口

1 1

Enter the guests name 输入客人姓名

B

Room number 2 assigned to B 2号房间分配给B

Enter the Option: 输入选项:

  1. Check in. 报到。

  2. Check out. 查看。

  3. Exit 出口

1 1

Enter the guests name 输入客人姓名

C C

Room number 3 assigned to C 3号房间分配给C

Enter the Option: 输入选项:

  1. Check in. 报到。

  2. Check out. 查看。

  3. Exit 出口

1 1

Enter the guests name 输入客人姓名

D d

No empty Rooms to assign to D 没有空房间分配给D.

Enter the Option: 输入选项:

  1. Check in. 报到。

  2. Check out. 查看。

  3. Exit 出口

2 2

Enter the Room number 输入房间号码

1 1

Room number 1 is empty now!. 1号房间现在空了!

Enter the Option: 输入选项:

  1. Check in. 报到。

  2. Check out. 查看。

  3. Exit 出口

1 1

Enter the guests name 输入客人姓名

D d

Room number 1 assigned to D 分配给D的1号房间

Enter the Option: 输入选项:

  1. Check in. 报到。

  2. Check out. 查看。

  3. Exit 出口

3 3

I can see you already made some code but for your next similar assignment I would code the classes in the following way. 我可以看到你已经编写了一些代码,但是对于你的下一个类似的任务,我会按照以下方式编写类。

  1. Guest 客人
  2. Room 房间
  3. Hotel 旅馆
  4. Assignment5 <-- This is the most complicated one and is something you can do at last. Assignment5 < - 这是最复杂的一个,你可以做到最后。

Why? 为什么?

Guest is really easy to code. 访客非常容易编码。 When that is done go on to Room. 完成后再转到Room。 Room is gonna need the class Guest. 房间需要班级宾客。 After you finished Room go code Hotel. 完成房间后去代码酒店。 Hotel is gonna use Room. 酒店将使用房间。

From what I can understand from your (dutch)code you store a guest as a String. 根据您的(荷兰语)代码可以理解,您将guest虚拟机存储为String。 This is not the purpose of the assignment. 这不是作业的目的。 It is supposed to store a Guest object. 它应该存储一个Guest对象。

I hope this will help you. 我希望这能帮到您。

I'm joining this a little late, but the reason for the error is you are attempting to check in a String called Guido instead of a Guest (Gast) with the name field of Guido. 我加入这个有点晚了,但错误的原因是你试图检查一个名为Guido的字符串,而不是一个名字字段为Guido的Guest(Gast)。 You would need some lines like the following: 您需要一些如下所示的行:
Gast guido = new Gast("Guido");
hotel.checkIn(guido); // this was previously hotel.checkIn("Guido")

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

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