我试过搜索和阅读低谷(类似的声音)问题,但我似乎无法找到解决方案。
我目前正在制作原型来控制一些带文字输入的灯。 文本输入已经正常工作,它可以正确地过滤输入,只选择相关部分。
无论如何,为了让它变得有用,我现在需要首先创建我家的物品,其中包含所有楼层,其中包含所有房间,其中包含所有演员(灯等)。
这些是所有这些对象的片段,从上到下,因为它们应该是彼此堆叠的(代码还没有完成,也没有优化,我知道我可以对所有if-else语句使用switch-case):
import java.util.ArrayList;
public class Building {
String Name;
ArrayList<floor> floor = new ArrayList<floor>();
public Building(String Name){
this.Name = Name;
}
public boolean checkFloor(String Name){
int count = floor.size();
boolean ret = false;
while(count < floor.size()){
if (floor.get(count).Name == Name){
count = floor.size();
ret = true;
}
count++;
}
return(ret);
}
public void newFloor(String Name){
Name = Name.toLowerCase();
if (Name == "keller" || Name == "basement"){
floor basement = new floor("basement");
floor.add(basement);
}
else if (Name == "eg"){
floor ground = new floor("eg");
floor.add(ground);
}
else if (Name == "1og"){
floor first = new floor("first");
floor.add(first);
}
else if (Name == "2og"){
floor second = new floor("second");
floor.add(second);
}
else if (Name == "3og"){
floor third = new floor("third");
floor.add(third);
}
}
public void newRoom(String Floor, String Name){
}
}
public class floor {
String Name;
ArrayList<room> room = new ArrayList<room>();
transcoder transcode = new transcoder();
public floor(String Name){
this.Name = Name;
}
public boolean checkRoom(String Name){
int count = room.size();
boolean ret = false;
while(count < room.size()){
if (room.get(count).Name == Name){
count = room.size();
ret = true;
}
count++;
}
return(ret);
}
public void newRoom(String mName){
String Name = transcode.getRoom(mName);
if( Name == "ground"){
room ground = new room("ground");
room.add(ground);
}
else if ( Name == "basement"){
room basement = new room("basement");
room.add(basement);
}
else if ( Name == "first"){
room first = new room("first");
room.add(first);
}
else if ( Name == "second"){
room second = new room("second");
room.add(second);
}
}
}
public class room {
String Name;
ArrayList<Actor> Actors = new ArrayList<Actor>();
public room(String Name){
this.Name = Name;
}
public void addActor(String Name, int Type, String Address, int Channel, boolean Dim){
Actors.add(new Actor(Name, Type, Address, Channel, Dim));
}
public void removeActor(String Name){
int count = 0;
while (count <= Actors.size()){
if (Actors.get(count).Name == Name){
Actors.remove(count);
count = Actors.size();
}
count++;
}
}
public boolean containsActor(String Name){
int count = 0;
boolean ret = false;
while (count < Actors.size()){
if (Actors.get(count).Name == Name){
ret = true;
count = Actors.size();
}
count++;
}
return(ret);
}
public String getAddress(String Name){
int count = 0;
String ret = "leer";
while (count < Actors.size()){
if (Actors.get(count).Name == Name){
ret = Actors.get(count).Address;
count = Actors.size();
}
count++;
}
return(ret);
}
public int getType(String Name){
int count = 0;
int ret = 0;
while (count < Actors.size()){
if (Actors.get(count).Name == Name){
ret = Actors.get(count).Type;
count = Actors.size();
}
count++;
}
return(ret);
}
}
public class Actor {
String Name;
String Address;
int Channel;
int Type;
boolean Dim;
int On; //muss noch deklariert werden!
int Off;
public Actor(String Name, int Type, String Address, int Channel, boolean Dim){
this.Name = Name;
this.Type = Type;
this.Address = Address;
this.Channel = Channel;
this.Dim = Dim;
}
}
我现在在Mainclass中尝试做的是创建一个带有地板,房间和一些演员的新建筑。代码如下:
System.out.println("Gebäudename eingeben(egal): ");
String Name = user_input.nextLine();
Building Building = new Building(Name);
System.out.println("Stockname eingeben(eg): ");
Name = user_input.nextLine();
Building.newFloor(Name);
System.out.println("Raumname eingeben(wohnen): ");
Name = user_input.nextLine();
Building.floor
现在在Building.floor的最后,我还没有看到我现在如何在对象楼层添加新房间。我很感激任何帮助,因为我还不太喜欢Java,可能缺少明显的部分。
答案 0 :(得分:1)
在您的Building类中,使用getter和setter。你有一个floor
变量,但没有办法得到它。将此getter方法添加到Building
public ArrayList<floor> getFloors(){
return floor;
}
然后,而不是说Building.floor
,你可以说类似
int floorToGet = (what ever floor in the ArrayList you want);
Building.getFloors.get(floorToGet).newRoom("New Room Name");
另外,如果我对你的编码风格和一些建议提出一些建议。按照惯例,所有类都以大写字母开头,即Building
而不是building
你在某些类中执行此操作,而不是其他类。此外,如果您想要使用Building.floor
这样的语法,那么最好使用该变量static
。否则为变量创建getter / setter并实例化一个新的构建对象。而且按照惯例,变量名称开始小写,即在这种情况下Building building = new Building()
,你最初看起来就像访问一个实际上不是静态的静态变量。
如果您熟悉这个概念,最后一个建议。如果您将一些类子类化,它可能会使数据访问变得更加容易。我的意思是,建筑物包含地板,楼层包含房间。因此,如果不是混淆,Building
将是您的主要课程,然后是Floors extends Building
和Room extends Floor
。
从逻辑角度来看,我认为这更有意义。但希望这会有所帮助。
答案 1 :(得分:1)
要回答您的问题,最简单的方法是将新房间添加到建筑物的楼层:
Building.floor.get(0).newRoom(Name);
floor
是一个ArrayList,在第一个索引上调用get()
将允许您调用newRoom()
。
使您的代码更具可读性/可理解性的一些建议:
将floor
更改为Floor
,将room
更改为Room
,将您的班级名称大写。
将ArrayList
的名称从floor
更改为floors
。
ArrayList
的名称从room
更改为rooms
。String
变量以存储用户输入。ArrayList
。我要说的最重要的事情是getFloor()
类的Building
方法。
像这样(假设你进行了上述修改):
public Floor getFloor(String floorName){
Floor correctFloor = null;
for(Floor floor : floors) {
if(floor.Name == floorName) {
correctFloor = floor;
}
}
return correctFloor;
}
然后你而不是
System.out.println("Gebäudename eingeben(egal): ");
String Name = user_input.nextLine();
Building Building = new Building(Name);
System.out.println("Stockname eingeben(eg): ");
Name = user_input.nextLine();
Building.newFloor(Name);
System.out.println("Raumname eingeben(wohnen): ");
Name = user_input.nextLine();
Building.floor.get(0).newRoom(Name);
你可以(再次假设上述变化)
System.out.println("Gebäudename eingeben(egal): ");
String buildingName = user_input.nextLine();
Building building = new Building(buildingName);
System.out.println("Stockname eingeben(eg): ");
String floorName = user_input.nextLine();
Floor floor = new Floor(floorName);
building.newFloor(floorName);
System.out.println("Raumname eingeben(wohnen): ");
String roomName = user_input.nextLine();
building.getFloor(floorName).newRoom(roomName);
这些只是建议,有很多方法可以做到这一点〜祝你好运!