如何使用构造函数将带有输入值的固定值传递到数组列表中?

时间:2017-06-03 21:40:48

标签: java inheritance arraylist

我正在尝试为要从构造函数传递到ArrayList的对象传递两个值,我试图弄清楚如何传递在构造函数中具有固定值的速率值,另一个类是也继承自。 obj需要一个int,double(int是通过输入)。

public static HotelRoom roomNormal()
    {
        int room1to299;
        double rate;
        HotelRoom obj = null;

        room1to299 = Integer.parseInt(JOptionPane.showInputDialog("Enter room number"));
        rate = obj.getRate();
        obj = new HotelRoom(room1to299, rate);
        JOptionPane.showMessageDialog(null,"--Rooms Booked out--\n\n\n"
                + "Room No. " + room1to299
                + "Nightly Rate $" + obj.getRate());
        return obj;
    }

这是HotelRoom类

public class HotelRoom 
{
    private int roomNo;
    private double rate;

    public HotelRoom(int roomNo, double rate) 
    {
        this.roomNo = roomNo;
        this.rate = rate;
        if(roomNo < 300)
            this.rate = 69.95;
        else
            this.rate = 89.95;
    }

    public int getRoomNo() 
    {
        return roomNo;
    }

    public void setRoomNo(int roomNo) 
    {
        this.roomNo = roomNo;
    }

    public double getRate() 
    {
        return rate;
    }

    public void setRate(double rate) 
    {
        this.rate = rate;
    }

    @Override
    public String toString() 
    {
        return "Room No.: " + roomNo + ", Rate: " + rate + '\n';
    } 


    public double increaseRate(double surcharge)
    {
        return (this.rate + surcharge);
    }
}

这是继承自HotelRoom

的类
public class Suite extends HotelRoom
{
    private double surcharge = 40.00;

    public Suite(double surcharge, int roomNo, double rate) 
    {
        super(roomNo, rate);
        this.surcharge = surcharge;
    }

    public double getSurcharge() 
    {
        return surcharge;
    }

    public void setSurcharge(double surcharge) 
    {
        this.surcharge = surcharge;
    }

    @Override
    public String toString() 
    {
        return super.toString() + "Surcharge: " + surcharge + 'n';
    }
}

这是迄今为止的主要方法

public static void main(String[] args) 
    {
       ArrayList<HotelRoom> hrs = new ArrayList<>();
       int userSelect = menu();
       while(userSelect != 4)
       {
           switch(userSelect)
           {
               case 1:
                   subMenu();
                   break;
               case 2:
                   roomBooked();
                   break;
               case 3:
                   roomBookedAll();
                   break;
               default:
                   JOptionPane.showMessageDialog(null, "Invalid input");
           }
       }


    }
    public static int menu()
    {
        String selectMenu = "--HOTEL RENTAL SYSTEM--\n\n"
                + "1. Choose a room type\n"
                + "2. Room rates information\n"
                + "3. Rooms currently booked\n\n"
                + "4. Exit";
        int select = Integer.parseInt(JOptionPane.showInputDialog(selectMenu));
        return select;
    }
    public static void subMenu()
    {
        String subMenu = "1. Normal Hotel room\n"
                + "2. Suite\n\n"
                + "Choose room type";
        int select = Integer.parseInt(JOptionPane.showInputDialog(subMenu)); 
        switch(select)
        {  
            case 1:
                roomNormal();
                break;
            case 2:
                roomSuite();
                break;
            default:
                break;
        }
    }

    public static HotelRoom roomNormal()
    {
        int room1to299;
        double rate;
        HotelRoom obj = null;

        room1to299 = Integer.parseInt(JOptionPane.showInputDialog("Enter room number"));
        rate = obj.getRate();
        obj = new HotelRoom(room1to299, rate);
        JOptionPane.showMessageDialog(null,"--Rooms Booked out--\n\n\n"
                + "Room No. " + room1to299
                + "Nightly Rate $" + obj.getRate());
        return obj;
    }

1 个答案:

答案 0 :(得分:0)

您的问题有点模糊,所以我假设错误出现在您提供的第一个代码块中,即roomNormal()方法。话虽这么说,您的HotelRoom对象obj需要先进行初始化才能访问getRate()方法。否则,它将抛出空指针异常。

解决方案:rate = 4.5代替rate = obj.getRate()