記事ディレクトリ
- Javaサービスインターフェイス実装クラス
-
- 1.ステータスサービスインターフェイス実装クラスStatusServiceImplを作成します
- ユニットテストStatusServiceImpl
- (1)テストメソッドtestFindStatusById()を記述します
- (2)テストメソッドtestUpdateStatus()を記述します
- 2.学生サービスインターフェイス実装クラスを作成します
- ユニットテストStudentServiceImpl
- (1)テストメソッドtestFindStudentsByName()を記述します
- (2)テストメソッドtestAddStudent()を記述します
- (3)テストメソッドtestDeleteStudentById()を記述します
- (4)テストメソッドtestDeleteStudentsByClass()を記述します
- (5)テストメソッドtestDeleteStudentsByDepartment()を記述します
- (6)テストメソッドtestUpdateStudent()を記述します
- (7)テストメソッドtestFindStudentById()を記述します
- (8)テストメソッドtestFindAllStudents()を記述します
- (9)テストメソッドtestFindStudentsByClass()を記述します
- (10)テストメソッドtestFindStudentsByDepartment()を記述します
- (11)テストメソッドtestFindRowsBySex()を記述します
- (12)テストメソッドtestFindRowsByClass()を記述します
- (13)テストメソッドtestFindRowsByDepartment()を記述します
- 3.ユーザーサービスインターフェイス実装クラスを作成します
- ユニットテストUserServiceImpl
- (1)テストメソッドtestLogin()を記述します
- (2)テストメソッドtestIsUsernameExisted()を記述します
- (3)テストメソッドtestAddUser()を記述します
- (4)テストメソッドtestDeleteUserById()を記述します
- (5)テストメソッドtestUpdateUser()を記述します
- (6)テストメソッドtestFindUserById()を記述します
- (7)テストメソッドtestFindAllUsers()を記述します
- アプリケーションクラスを作成する
- ウィンドウインターフェイスを作成する
Javaサービスインターフェイス実装クラス
前回のトレーニングでは、対応する4つのサービスインターフェイスが確立され、学校のようなサービスインターフェイス実装クラスが作成されました。このトレーニングでは、引き続きサービスインターフェイス実装クラスを確立します。
1.ステータスサービスインターフェイス実装クラスStatusServiceImplを作成します
サブパッケージimplofserviceに実装クラスを作成します。
詳細なコードは次のとおりです。
ユニットテストStatusServiceImpl
テストパッケージにテストクラスを作成します。
(1)テストメソッドtestFindStatusById()を記述します
コードは次のとおりです。
操作結果は次のとおりです。
(2)テストメソッドtestUpdateStatus()を記述します
実行結果は次のとおりです。
2.学生サービスインターフェイス実装クラスを作成します
詳細なコードは次のとおりです。
package net.lyq.student.service.impl;
import net.lyq.student.bean.Student;
import net.lyq.student.dao.StudentDao;
import net.lyq.student.dao.impl.StudentDaoImpl;
import net.lyq.student.service.StudentService;
import java.util.List;
import java.util.Vector;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl();
@Override
public int addStudent(Student student) {
return studentDao.insert(student);
}
@Override
public int deleteStudentById(String id) {
return studentDao.deleteById(id);
}
@Override
public int deleteStudentsByClass(String clazz) {
return studentDao.deleteByClass(clazz);
}
@Override
public int deleteStudentsByDepartment(String department) {
return studentDao.deleteByDepartment(department);
}
@Override
public int updateStudent(Student student) {
return studentDao.update(student);
}
@Override
public Student findStudentById(String id) {
return studentDao.findById(id);
}
@Override
public List<Student> findStudentByName(String name) {
return studentDao.findByName(name);
}
@Override
public List<Student> findStudentsByClass(String clazz) {
return studentDao.findByClass(clazz);
}
@Override
public List<Student> findStudentsByDepartment(String department) {
return studentDao.findByDepartment(department);
}
@Override
public List<Student> findAllStudents() {
return studentDao.findAll();
}
@Override
public Vector findRowsBySex() {
return studentDao.findRowsBySex();
}
@Override
public Vector findRowsByClass() {
return studentDao.findRowsByClass();
}
@Override
public Vector findRowsByDepartment() {
return studentDao.findRowsByDepartment();
}
}
ユニットテストStudentServiceImpl
(1)テストメソッドtestFindStudentsByName()を記述します
(2)テストメソッドtestAddStudent()を記述します
動作結果:
(3)テストメソッドtestDeleteStudentById()を記述します
実行結果:
データシート:
(4)テストメソッドtestDeleteStudentsByClass()を記述します
(5)テストメソッドtestDeleteStudentsByDepartment()を記述します
(6)テストメソッドtestUpdateStudent()を記述します
(7)テストメソッドtestFindStudentById()を記述します
(8)テストメソッドtestFindAllStudents()を記述します
(9)テストメソッドtestFindStudentsByClass()を記述します
(10)テストメソッドtestFindStudentsByDepartment()を記述します
(11)テストメソッドtestFindRowsBySex()を記述します
(12)テストメソッドtestFindRowsByClass()を記述します
(13)テストメソッドtestFindRowsByDepartment()を記述します
3.ユーザーサービスインターフェイス実装クラスを作成します
package net.lyq.student.service.impl;
import net.lyq.student.bean.User;
import net.lyq.student.dao.UserDao;
import net.lyq.student.dao.impl.UserDaoImpl;
import net.lyq.student.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
/**
* 声明用户数据访问对象
*/
private UserDao userDao = new UserDaoImpl();
@Override
public int addUser(User user) {
return userDao.insert(user);
}
@Override
public int deleteUserById(int id) {
return userDao.deleteById(id);
}
@Override
public List<User> findAllUsers() {
return userDao.findAll();
}
@Override
public User findUserById(int id) {
return userDao.findById(id);
}
@Override
public User login(String username, String password) {
return userDao.login(username, password);
}
@Override
public int updateUser(User user) {
return userDao.update(user);
}
@Override
public boolean isUsernameExisted(String username) {
return userDao.isUsernameExisted(username);
}
}
ユニットテストUserServiceImpl
(1)テストメソッドtestLogin()を記述します
動作結果:
(2)テストメソッドtestIsUsernameExisted()を記述します
動作結果:
ユーザー名を変更して、実行結果を変更します
(3)テストメソッドtestAddUser()を記述します
動作結果:
(4)テストメソッドtestDeleteUserById()を記述します
動作結果:
(5)テストメソッドtestUpdateUser()を記述します
動作結果:
(6)テストメソッドtestFindUserById()を記述します
動作結果:
(7)テストメソッドtestFindAllUsers()を記述します
操作結果:
上記はすべてのサービスインターフェースクラスのすべてのテストクラスです
アプリケーションクラスを作成する
net.lyq.studentパッケージにアプリサブパッケージを作成し、Applicationクラスを作成します
クラスに示されているようにコードを記述します。3つのウィンドウがまだ作成されていないため、エラーが発生します。
3つのウィンドウクラスを格納するためのguiパッケージを作成します。作成が終了したら
、アプリケーションを再度確認します。
ウィンドウインターフェイスを作成する
上記のように、図のように3つのウィンドウを作成すると、ウィンドウクラスを記述できます。
1.メインインターフェイスウィンドウMainFrameを作成します
package net.lyq.student.gui;
import net.lyq.student.app.Application;
import net.lyq.student.bean.Status;
import net.lyq.student.service.StatusService;
import net.lyq.student.service.impl.StatusServiceImpl;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MainFrame extends JFrame {
/**
* 构造方法
* @param title
*/
public MainFrame(String title){
super(title);
initGUI();
}
private Status status;//状态对象
private StatusService statusService;//状态服务对象
/**
* 初始化图形界面
*/
public void initGUI(){
//创建状态服务对象
statusService = new StatusServiceImpl();
//获取状态对象
status = statusService.findStatusById(1);
//设置窗口尺寸
setSize(800,640);
//设置窗口可见
setVisible(true);
setLocationRelativeTo(null);
//设置窗口标题
setTitle("学生信息管理系统"+status.getVersion());
//设置窗口默认关闭方式
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//注册窗口监听器,创建窗口适配器,编写事件处理代码
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exitSystem();
}
});
}
/**
* 退出系统,询问用户是否退出
*/
private void exitSystem() {
int choice = JOptionPane.showConfirmDialog(this,"您是否要退出系统",
"学生信息管理系统",JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_NO_OPTION){
System.exit(0);
}else{
//卸载当前窗口
dispose();
//重新显示主窗口
Application.mainFrame = new MainFrame("学生信息管理系统"+status.getVersion());
}
}
public static void main(String[] args) {
Application.mainFrame = new MainFrame("");
}
}
2.ユーザーログインウィンドウLoginFrameを作成します
package net.lyq.student.gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import net.lyq.student.app.Application;
import net.lyq.student.bean.Status;
import net.lyq.student.bean.User;
import net.lyq.student.service.StatusService;
import net.lyq.student.service.UserService;
import net.lyq.student.service.impl.StatusServiceImpl;
import net.lyq.student.service.impl.UserServiceImpl;
public class LoginFrame extends JFrame{
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JButton btnRegister;
private JPanel panel,panel1,panel2,panel3;
public LoginFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定");
btnCancel = new JButton("取消");
btnRegister = new JButton("注册");
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel3.add(btnRegister);
panel.setLayout(new GridLayout(3,1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnRegister.setMnemonic(KeyEvent.VK_R);
txtPassword.setEchoChar('*');
setSize(250, 200);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
login();
}
});
btnOK.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
});
txtUsername.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtUsername.requestFocus();
}
});
txtPassword.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtPassword.requestFocus();
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
btnRegister.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Application.loginFrame.setVisible(false);
Application.registerFrame = new RegisterFrame("用户注册");
}
});
}
private void login() {
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
UserService userService = new UserServiceImpl();
User user = userService.login(username, password);
if (user != null) {
Application.loginFrame.setVisible(false);
StatusService statusService = new StatusServiceImpl();
Status status = statusService.findStatusById(1);
Application.id = user.getId();
Application.username = user.getUsername();
Application.password = user.getPassword();
JOptionPane.showMessageDialog(null, "欢迎使用学生信息管理系统" + status.getVersion(),"用户登录",JOptionPane.INFORMATION_MESSAGE);
Application.mainFrame = new MainFrame("");
Application.loginFrame.dispose();
}else {
Application.loginFrame.setVisible(false);
JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入","用户登录",JOptionPane.ERROR_MESSAGE);
Application.loginFrame.setVisible(true);
txtUsername.selectAll();
txtPassword.selectAll();
txtUsername.requestFocus();
}
}
public static void main(String[] args) {
Application.loginFrame = new LoginFrame("用户登录");
}
}
このコードでは、RegisterFrameでエラーが発生し、クラスを変更する必要があります