2012-03-28 21 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クラスです。

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

私は、データベースから追​​加したユーザーを削除したいと思いますが、私は削除クラスに文字列を渡す持っているとして、その、それを取っていない理由を私は知りません。

+0

へようこそSOのUserオブジェクトとそれを呼んでいた、正しい得ることができました! 1)出力、コードスニペット、コードにはコードの書式を使用してください。 2)すぐにより良い助けを得るために、[SSCCE](http://sscce.org/)を投稿してください。 3) 'public class Delete extends JFrame'はおそらく' JDialog'を拡張するべきです。 –

+0

こんにちは、ありがとう、しかし、私はコードでは、そのちょうど私は私が追加したものを削除することはできません。ええ、次回にこのことを覚えておいてください。 –

+0

*「次回はこのことを念頭に置いてください」*今回は質問を編集できます。さもなければ、私は次の時間に答えることを見ます。 –

答えて

0

フィールドとメソッドが小文字で始まる方法については、特にJava Code Conventionsに従ってください。コードを読みやすくすることができます。

また、質問を明確にするようにしてください。何がうまくいきませんか?エラーメッセージとは何か、または期待される動作と実際の動作とは何ですか?

​​

最初の行は、一般的には間違った縫い目( "=ユーザー" 何を意味するのでしょうか?) しかし、もっと重要:

public boolean removeUser(String useridln) 

優先mothodは、文字列を期待しますユーザーオブジェクトではないため、代わりにuserID.getText()を渡すと動作します。

これは問題でしたか? 「検索された値を削除する」とも言われていますが、検索フィールドは表示されません。

+0

おかげで、私のせいもなくなりました。でも、ありがとう。 –

+0

@モハメドアリは問題ありません。あなたの質問に答える場合は、左側の[チェックインサイン]をクリックして回答を受け入れることができます([here](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-答 - 仕事))。また、あなたの質問にすでに答えられている場合は、自分で回答を投稿する必要はありません。 – tim

0

は、私は私ではなく、ユーザーのID

User u = db.getUser(userID.getText());    
    db.removeUser(userID.getText()); 
関連する問題