下面的类是我的删除类,我想从数据库中删除用户,我有一个Add类和Search类,它们共享同一个数据库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();
}
}
下面的类是我的User类,带有返回方法
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 :(得分:0)
请尝试遵循Java Code Conventions,尤其是有关字段和方法如何以小写字母开头的规则。它会使你的代码更容易阅读。
另外,尝试更清楚地表达您的问题。究竟什么不起作用?什么是错误消息或预期行为与实际行为的对比是什么?
User u = User db.getUser(userID.getText()); db.removeUser(u);
第一行接缝错误(“=用户”是什么意思?) 但更重要的是:
public boolean removeUser(String useridln)
mothod需要一个String,而不是User Object,所以如果你传递userID.getText(),它应该可以工作。
这是你的问题吗?我不确定你还提到“删除搜索值”,但我看不到搜索字段。
答案 1 :(得分:0)
我设法变得正确,我用User对象而不是用户的id
来调用它 User u = db.getUser(userID.getText());
db.removeUser(userID.getText());