2012-01-02 49 views
1

我的程序中有一個搜索功能,用於查找我的數據列表中的姓名或個人。這可以找到該人的姓名,但我無法獲得我需要的其他信息。我猜這是因爲我沒有宣佈什麼。我不確定如何繼續。搜索功能問題

第一類持有我的方法。

public String searchName(String guest) 
{ 
    for (Booking s : bookings) 
    { 
     if (s.getGuest().equals(guest)) 
     return guest + " is in room " + roomID + "\n"; 
    } 
    return guest + " is not booking in at the hostel"; 
} 

我的第二類是我的GUI調用方法

else if (item.equals("Find Room")) 
{ 
    String name = JOptionPane.showInputDialog(this, 
        "Enter Guests Name", 
        "Find Room", 
        JOptionPane.QUESTION_MESSAGE); 
    output.setText(hostel.searchName(name)); 
} 

我能夠尋找一個人的名字,如果它是正確的,將被發現並繪製消息到我的JTextArea成功。它不會顯示roomID,但它只是提出null。我很困惑,我沒有得到任何錯誤消息,並且null通常與ints而非Strings相關聯?

完整的代碼,如果需要GUI類

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class GUI extends JFrame implements ActionListener 
{ 
private HostelFile hostel; 
private JPanel textOutput; 
private JScrollPane scroller; 
private JButton listFreeButton = new JButton("List Free Rooms"); 
private JButton newBookingButton = new JButton("New Booking"); 
private JButton cancelBookingButton = new JButton("Cancel Booking"); 
private JButton listAllButton = new JButton("List All Bookings"); 
private JButton allRoomsButton = new JButton("List All Room Details"); 
private JButton findRoomButton = new JButton("Find Room"); 
private JButton exitButton = new JButton("Exit System"); 
private JTextArea output = new JTextArea(30,30); 

public GUI(String hostelName) 
{ 
    super(hostelName); 
    hostel = new HostelFile(hostelName); 
    makeFrame(); 
    showFrame(); 
} 

public void showFrame()  
{ 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    setSize(800,600); 
    setVisible(true); 
} 

public void makeFrame()  
{ 
    JPanel buttonPanel = new JPanel();    
    buttonPanel.setLayout(new GridLayout(10,10,10,10)); 
    buttonPanel.setBorder(BorderFactory.createEtchedBorder());   
    buttonPanel.add(listFreeButton); 
    buttonPanel.add(newBookingButton); 
    buttonPanel.add(cancelBookingButton); 
    buttonPanel.add(listAllButton); 
    buttonPanel.add(allRoomsButton); 
    buttonPanel.add(findRoomButton); 
    buttonPanel.add(exitButton); 

    textOutput = new JPanel(); 
    textOutput.setLayout(new BorderLayout()); 
    textOutput.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 

    output.setEditable(false); 
    scroller = new JScrollPane(output);  
    textOutput.add(scroller); 

    add(buttonPanel,BorderLayout.WEST); 
    add(textOutput,BorderLayout.CENTER); 

    listFreeButton.addActionListener(this); 
    newBookingButton.addActionListener(this); 
    cancelBookingButton.addActionListener(this); 
    listAllButton.addActionListener(this); 
    allRoomsButton.addActionListener(this); 
    findRoomButton.addActionListener(this); 
    exitButton.addActionListener(this); 
} 

public void actionPerformed(ActionEvent ae)  
{ 
    String item = ae.getActionCommand(); 

    if (item.equals("List Free Rooms")) 
    { 
    } 
    else if (item.equals("New Booking")) 
    { 
     AddBooking bookingGUI = new AddBooking(this); 
    } 
    else if (item.equals("Cancel Booking")) 
    { 
    } 
    else if (item.equals("List All Bookings")) 
    { 
     output.setText(hostel.getAllBookings()); 
    } 
    else if (item.equals("List All Room Details")) 
    { 
    } 
    else if (item.equals("Find Room")) 
    { 
     String name = JOptionPane.showInputDialog(this, 
            "Enter Guests Name", 
            "Find Room", 
            JOptionPane.QUESTION_MESSAGE); 
     output.setText(hostel.searchName(name)); 
    } 
    else if (item.equals("Exit System")) 
    { 
     hostel.saveListBookings(); 
     System.exit(0); 
    } 
} 


public void addBooking(String roomID, String roomType, String guest) 
{ 
    output.setText(hostel.addBooking(roomID,roomType,guest)); 
} 
} 

的方法

import java.util.*; 
public class ListBookings 
{ 
private String hostelName; 
private ArrayList<Booking> bookings; 
private String roomID; 
private String roomType; 
private boolean ensuite; 
private String guest; 
private String nights; 
private String booked; 
/** 
    * constructs objects for GUI 
    */ 

public ListBookings(String hostelName) 
{ 
    this.hostelName = hostelName; 
    bookings = new ArrayList<Booking>(); 
} 

public String getHostelName() 
{ 
    return hostelName; 
} 

public String addBooking(String roomID, String roomType, String guest) 
{ 
    if (roomID.equals("")) 
     return "Error Please Entre Room ID"; 

    else if (roomType.equals("")) 
     return "Error Please Entre Room Type"; 

    else if (guest.equals("")) 
     return "Error Please Entre Guest Name"; 

    bookings.add(new Booking(roomID,roomType,guest)); 
    return "Room " + roomID + " " + roomType + " Has Been Booked For " + guest; 
} 

public String getAllBookings() 
{ 
    if (bookings.size() > 0) 
    { 
     String allBookings = ""; 
     for (Booking s : bookings) 
     { 
      allBookings = allBookings + s.getRoomID() + " " + s.getRoomType() + " " + s.getGuest() + "\n"; 
     } 
     return allBookings; 
    } 
    else 
    { 
     return "Bookings are Empty"; 
    } 
} 

public String searchName(String guest) 
{ 
    for (Booking s : bookings) 
    { 
     if (s.getGuest().equals(guest)) 
     return guest + " is in room " + roomID + "\n"; 
    } 
    return guest + " is not booking in at the hostel"; 
} 

public String deleteBooking(String roomID) 
{ 
    int index = 0; 
    for (Booking s : bookings) 
    { 
     if (s.getRoomID().equals(roomID)) 
     { 
      return "Room ID: " + roomID + " Room Type: " + roomType + " Guest: " + guest; 
      //students.remove(index); 
      //return name + " removed from module " + moduleName + "\n"; 
     } 
     index++; 
    } 
    return " Cannot find " + roomID + "\n"; 
} 
} 

全碼HostelFile

import java.util.*; 
import java.io.*; 
public class HostelFile extends ListBookings 
{ 
public HostelFile(String hostelName) 
{ 
    super(hostelName); 
    readListBookings(hostelName); 
} 

private void readListBookings(String fileName) 
{ 
    String roomID; 
    String roomType; 
    String guest; 
    try 
    { 
     Scanner fileScanner = new Scanner(new File (fileName + ".txt")); 
     while (fileScanner.hasNext()) 
     { 
      roomID = fileScanner.next(); 
      roomType = fileScanner.next(); 
      guest = fileScanner.next(); 
      addBooking(roomID,roomType,guest); 
     } 
     fileScanner.close(); 
    } 
    catch (IOException e) 
    { 
     System.out.println("File not found"); 
    } 
} 


public void saveListBookings() 
{ 
    String fileName = getHostelName() +".txt" ; 
    try { 
     PrintWriter print = new PrintWriter( 
        new BufferedWriter( 
          new FileWriter(fileName))); 

     print.println(getAllBookings()); 
     print.close(); 
    } 
    catch (IOException iox) { 
     System.out.println("Problem writing " + fileName); 
    } 
}    
} 

回答

0

它沒有意義的VAR roomID可以用於這個。預訂課程中必須有一些房產。像「s.getRoomID()」。

編輯:好的。查看所有類,很明顯,Booking有一個getRoomID()方法。

你的函數改成這樣:

public String searchName(String guest) 
{ 
    for (Booking s : bookings) 
    { 
     if (s.getGuest().equals(guest)) 
     return guest + " is in room " + s.getRoomId() + "\n"; 
    } 
    return guest + " is not booking in at the hostel"; 
} 
+0

所以如果有私人字符串roomID的insted的;我有s.getRoomID()= roomID;這可能有幫助嗎?或將this.roomID = roomID;存儲正確的變量 – 2012-01-02 16:26:59

+0

「s.getRoomId()」是我告訴你必須有一些方法返回房間,但可能帶有其他名稱。爲方法賦值(「s.getRoomId()= roomId」)沒有任何意義。不過,在編輯之前添加了類的代碼。請注意,類ListBookings中的searchName方法不一定與用「hostel.searchName(name)」調用的searchName相同。宿舍是一類HostelFile,而不是ListBookings。什麼是類HostelFile的代碼? – Pablo 2012-01-02 16:44:46

+0

HostelFile是一個讀寫文件,它將一個數組寫入一個txt文件,然後可以從中讀回數據。我將在問題部分添加內容,但我認爲這是不重要的,謝謝 – 2012-01-02 16:50:43