首页 > 解决方案 > 我无法显示另一个包中的 ArrayList 中的对象

问题描述

我是初学者,我尝试解决一个作业,我有一个java控制台项目,我应该为每个类创建一个包,在这个包内,它必须有Class模型,一个接口和一个实现所有方法的类接口,这意味着每个包将总共有 3 个类。现在我有 2 个包,第一个包含:Room.java、IRoom.java、RoomT.java 第二个包包含应用程序类 App.java 问题是我在应用程序类中有一个菜单可以让我进入房间子菜单或退出。房间子菜单有:

  1. 在 RoomT.java 的 ArrayList 中保存房间对象
  2. 显示 ArrayList 中的所有对象。
  3. 返回主菜单。

如果我回到主菜单我重新进入房间子菜单,当我选择显示我保存的所有对象时,ArrayList 是空的,我不知道为什么?如何解决这个问题。课程如下;

房间.java

/**
 *
 * @author ******
 */
public class Room {
    private String id;
    private float price;
    
    public Room(){}
    public Room(String id, float price){
        this.id = id;
        this.price = price;
    }
    
    public String toString(){
    return " ID         :"+this.id+"\n"
           +" Price      : $"+this.price;
    }
}

IRoom.java


/**
 *
 * @author *******
 */
public interface IRoom {
    void save();
    void display();
}

RoomT.java


import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author *****
 */
public class RoomT implements IRoom{
    Scanner input = new Scanner(System.in);
    Scanner num = new Scanner(System.in);
    
    public ArrayList<Room> dbRoom = new ArrayList<>();
    Room room = new Room();
    
    public void save(){
        System.out.println("Id : ");
        String id = input.nextLine();
        System.out.println("Price : ");
        float price = num.nextFloat();
        room = new Room(id, price);
        dbRoom.add(room);
    }
    
    public void display(){
        if(dbRoom.isEmpty()){
            System.out.println("Room not saved yet!!!");
        }else{
            for(Room room: dbRoom){
                System.out.println("____________________________________________");
                System.out.println(room.toString());
                System.out.println("____________________________________________");
            }
        }
    }
}

应用程序.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hotel.app;
import com.hotel.room.*;
import java.util.Scanner;

/**
 *
 * @author *****
 */
public class App {
    public static void main(String[] args) {
        int choice;
        Scanner input = new Scanner(System.in);
        
        do{
            System.out.println("1. For Room Menu. ");
            System.out.println("0. For quitt programs.=. ");
            choice = input.nextInt();
            
            switch(choice){
                case 0 : 
                    System.exit(0);
                    break;
                case 1 :
                    Scanner num = new Scanner(System.in);
                    RoomT room = new RoomT();
                    int choicech;
                    do{
                        System.out.println("_______________ROOM MENU_________________________");
                        System.out.println(" 1. Save a room.");
                        System.out.println(" 2. Display all rooms.");
                        System.out.println(" 0. <-back to main menu.");
                        System.out.println("_____________________________________________________");

                        choicech = num.nextInt();

                        switch(choicech){
                            case 0 :
                                 choicech = 5;
                                 break;
                            case 1 : room.save();
                                break;
                            case 2 : room.display();
                                break;
                            default : System.out.println("Incorrect Choice!!!.");
                            break;
                        }

                    }while(choicech<3);
                    break;
                default : 
                    System.out.println("Incorrect!!");
                    break;
            }
        }while(choice<2);
    }
}

结果结果 IMG

标签: javaarraylistpackage

解决方案


推荐阅读