谢谢!请求java大神进行帮忙,帮助我写一下代码,可以吗?
(1)创建四个包,xxx.bean、xxx.dao、xxx.dao.impl、xxx.test,其中xxx代表你的姓名的拼音缩写(全部小写字母);
(2)在xxx.bean中创建Users类,该类中定义username、password属性并封装。
(3)在xxx.dao包中创建接口UsersDao,定义一个方法checkUser(Users user),Users为xxx.bean包中的类,返回一个boolean类型的值。
(4)在xxx.dao.impl包中创建一个类UsersDaoImpl,实现xxx.dao包中的接口UsersDao,实现接口的checkUser()方法,判断User对象的用户名和密码是否满足指定要求,如果满足则返回true,否则返回false。
(5)在xxx.test包中创建测试类,测试类的名字使用姓名拼音以及作业序号,在主方法中创建UserDaoImpl类的对象,创建Users类的对象并为所有属性赋值,调用UserDaoImpl对象的checkUser()方法,如果返回值为true则输出用户名和密码正确,并输出该对象的所有属性值,如果返回值为false,则输出用户名或密码错误。 展开
结构:
代码:
1.user实体类:
package zhangsan.bean;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername( String username ) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword( String password ) {
this.password = password;
}
public User( String username, String password ) {
super();
this.username = username;
this.password = password;
}
public String toString() {
return "User [password=" + password + ", username=" + username + "]";
}
}
2.dao层接口
package zhangsan.dao;
import zhangsan.bean.User;
public interface Usersdao {
public boolean checkUser(User user);
}
3.dao层实现类
package zhangsan.dao.impl;
import zhangsan.bean.User;
import zhangsan.dao.Usersdao;
public class UsersDaoImpl implements Usersdao {
private User user;
public UsersDaoImpl( User user ) {
super();
this.user = user;
}
public boolean checkUser( User user ) {
if(this.user.getUsername().equals( user.getUsername() )&&this.user.getPassword().equals( user.getPassword() )){
return true;
}
return false;
}
}
4.测试类
package zhangsan.test;
import java.util.Scanner;
import zhangsan.bean.User;
import zhangsan.dao.impl.UsersDaoImpl;
public class TestUser {
/**
* @MethodName main
* @Description TODO
* @param args
* @return void
* @Date 2020-4-7
*/
public static void main( String[] args ) {
UsersDaoImpl usersDaoImpl = new UsersDaoImpl( new User( "zhangsan", "111111" ) );
Scanner scanner = new Scanner( System.in );
System.out.println("请输入账号:");
String username = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
User user = new User( username, password );
if(usersDaoImpl.checkUser( user )){
System.out.println(user);
}else{
System.out.println("账号或密码错误");
}
}
}