Question

The class below is my delete class, i want to delete the users from the database, i have a Add class and Search class, their share the same database 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");    
    }

the class below is my database class, which has the remove method and i am trying to pass it through to the Delete class above.

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();
    }
}

the class below is my User class, with the return methods

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";
    }
  }

In the Add class i can add users.

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

I would like to delete the added user from the database but, i do not know why its not taking it as i have pass the string to the delete class.

Was it helpful?

Solution

Please try to follow the Java Code Conventions, especially the rules about how fields and methods start with a lowercase letter. It would make your code a lot easier to read.

Also, try to formulate your question clearer. What exactly is not working? What are the error messages or what is the expected behaviour vs the actual behaviour?

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

The first line seams wrong in general (what does "= User" mean?) But more importantly:

    public boolean removeUser(String useridln) 

The mothod expects a String, not a User Object, so it should work if you pass userID.getText() instead.

Was this your problem? I am unsure as you also mention "deleting searched value", but I cannot see a search field.

OTHER TIPS

I managed to get correct, i was calling it with the User object instead of the user's id

 User u =  db.getUser(userID.getText());             
  db.removeUser(userID.getText()); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top