2012-03-28 57 views
0

下面的類是我的刪除類,我想從數據庫中刪除用戶,我有一個添加類和搜索類,他們共享相同的數據庫private Database db;無法刪除搜索值

package TakeMeOut; 
import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 


public class Delete extends JFrame implements ActionListener 


{ 

    /** {@link JTextField} where the user number is entered */ 
    private JTextField userID = new JTextField(7); 

    /** {@link JTextArea} for the client information */ 
    private JTextArea information = new JTextArea(5, 39); 

    /**{@link JButton} Search button */ 
    private JButton Deleteuser = new JButton("Delete"); 

    /** 
    * Default constructor. Create a new search panel with a {@link JTextField} for client ID and a {@link JTextArea} for detailed 
    * information on the client.. 
    */ 
    private Database db; 

    public Delete(Database db) 
     { this.db = db; 

     setLayout(new BorderLayout()); 
     setSize(450, 250); 
     setTitle("Delete Client"); 

     /** dispose of the window when the close button is clicked*/ 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel top = new JPanel(); 

     /** add the veritable of JButton to the top panel*/ 
     top.add(Deleteuser); 
     /**add the bottom panel to the bottom of the screen*/ 
     add("North", top); 

     top.add(new JLabel("Enter Client Number:")); 
     top.add(userID); 
     add("North", top); 

     JPanel middle = new JPanel(); 
     middle.setLayout(new FlowLayout()); 
     middle.add(information); 
     add("South", middle); 

     /** do not allow enduser to set the size of the screen*/ 
     //setResizable(false); 
     setResizable(false); 
     setVisible(true); 


     // listen to the button 
     Deleteuser.addActionListener(this); 
    } 

    /** 
    * delete user from database when the delete button is clicked 
    */ 
    @Override 
    public void actionPerformed(ActionEvent e) { 

     User u = (userID.getText()); 
     db.removeUser(u); 

     information.setText(u.toString() + " has been deleted");  
    } 

下面的類是我的數據庫類,它有remove方法,我試圖將它傳遞給上面的Delete類。

import java.util.*; 
public class Database 
{/**    
    * 
    * Map of users keyed on userId    
    */ 
    Map <String, User> users; 


    /** @Database   
    * the empty map which would be used to collect the users.      
    */ 
    public Database() 
{  
     users = new HashMap<String, User>(); 

} 



    /** 
    * Type for checking users 
    */ 
    public static void main(String [] args){ 
      new Database(); 
      }  

    /** check if the UserID is already in use if so return false, else 
    * add key pair to the Map. 
    * USERID will be key of map 
    *  @ 
    */ 
    public boolean addUser(User userIn) 
    { 

     String keyIn = userIn.getUSERID(); 
     if (users.containsKey(keyIn)) 
     { 
      return false; 
     } 
     else 

     { 
      users.put(keyIn, userIn); 
      return true; 
     } 
    } 



    /** 
    * @param remove the user with the given useridIn, from the Map 
    * check if user was removed and does not equal to no null 
    * @return If the user is not removed return false 
    * 
    * */ 
    public boolean removeUser(String useridln) 
    { 
     if (users.remove(useridln)!= null) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    /** 
    * return the number of users in the Map collection 
    * 
    * 
    * */ 
    public int getTotalNumberOfUsers() 
    { 
     return users.size(); 
    } 


    /** return the user with the given userid or null if no such user 
    * */ 
    public User getUser (String useridIn) 
    { 
     return users.get(useridIn); 
    } 



    /** return the set of users in the collection 
    * set is used to store the set of users and to get the set of keys. 
    * iterate through the keys and put each value in the userSetn and return the set of users 
    * 
    * 
    * */ 
    public Set<User> getAllUsers() 
    { 

     Set<User> userSet = new HashSet<User>(); 
     Set<String> theKeys = users.keySet(); 

     for (String userid : theKeys) 
     { 
      User theUser = users.get(userid); 
      userSet.add(theUser); 
     } 
     return userSet; 


     } 

    public String toString(){ 

     return users.toString(); 
    } 
} 

下面的類是我的用戶類,並返回方法

public class User { 

    /**declared attributes */ 
    private String username; 
    private String gender; 
    private String age; 
    public String userid; 

    /** User constructor with four types of string objects and the declared methods */ 
     public User(String usernameIn, String genderIn, String ageIn, String useridIn) { 

    /* declared methods*/ 
     username = usernameIn; 
     gender = genderIn; 
     age = ageIn; 
     userid = useridIn; 
    } 

     /** 
     * 
     * @return 
     */ 
    public String getUsername() { 
     return username; 
    } 

/** 
* 
* @return 
*/ 
    public String getGender() { 
     return gender; 
    } 

    /** 
    * 
    * @return 
    */ 
    public String getAge() { 
     return age; 
    } 

    /** 
    * 
    * @return 
    */ 
    public String getUSERID() { 
     return userid; 
     } 


    /** 
    * ToString return the customized values 
    */ 
    public String toString() 
    { 
     return"  "+ username +"  " + gender + "  " + age + " \n"; 
    } 
    } 

在添加類,我可以添加用戶。

User u = new User(Inputusername.getText(), selection , age.getText(), inputuserid.getText()); 
db.addUser(u); 

我想從數據庫中刪除所添加的用戶,但是,我不知道爲什麼它不把它作爲我有字符串傳遞給刪除類。

+0

歡迎SO調用它! 1)請使用代碼格式輸出,代碼片段和代碼。 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 3)'public class Delete extends JFrame'應該擴展'JDialog'。 – 2012-03-28 01:53:09

+0

嗨,謝謝,但我確實在代碼中,只是我無法刪除我添加的內容。是的,下次你會記住這一點 – 2012-03-28 01:59:44

+0

*「是的,下次會記住這一點」*這次你可以編輯你的問題。否則,我會考慮回答 - 下次。 – 2012-03-28 02:09:51

回答

0

請嘗試關注Java Code Conventions,特別是關於字段和方法如何以小寫字母開頭的規則。它會讓你的代碼更容易閱讀。

此外,嘗試制定你的問題更清晰。什麼不工作?什麼是錯誤信息,或者預期行爲與實際行爲有什麼關係?

​​

第一線縫錯在一般(什麼是 「=用戶」 是什麼意思?) 但更重要的是:

public boolean removeUser(String useridln) 

的評判需要一個字符串,不是一個用戶對象,所以它應該工作,如果你傳遞userID.getText()來代替。

這是你的問題?我不確定,因爲您還提到「刪除搜索值」,但我看不到搜索字段。

+0

謝謝隊友,我的錯不能讓自己清楚,但是謝謝 – 2012-03-28 15:43:49

+0

@穆罕默德阿里沒問題。如果您的問題得到解答,您可以點擊左側的「檢查標誌」接受答案(如[此處]所述(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-答案工作))。另外,如果您的問題已經得到解答,則自己發佈答案並不是真的必要;) – tim 2012-03-28 15:58:35

0

我設法得到正確的,我是用用戶對象,而不是用戶的ID

User u = db.getUser(userID.getText());    
    db.removeUser(userID.getText());