第九天 企业进销存系统

深入面向对象


案例:企业进销存系统

  1. 接口:在Java中,接口是实现可插入特性的保证。定义接口的关键字是interface,实现接口的关键字是implements,一个类可以实现多个接口,接口之间的继承支持多重继承。
  2. 接口和抽象类的异同
    关键字对比
    接口:声明接口: interface + 接口名(命名方式同类),实现:创建实现类 + implements + 接口名();实现类:必须重写接口未实现的方法。

抽象类:声明抽象类:abstract + class +类名 。同时必须声明抽象方法:abstract +void +方法名();子类:继承抽象类的子类必须重写抽象方法。

  1. 类/类和类/接口之间的关系
    • IS-A关系:继承/实现
    • HAS-A关系:关联/聚合(聚集)/合成
    • USE-A关系:依赖
  2. UML:统一建模语言(标准的图形化符号)
    • 类图:描述类以及类和类之间关系的图形符号。
    • 用例图:捕获需求。
    • 时序图:描述对象交互关系。
  3. 面向对象的设计原则
    • 单一职责原则(SRP):类的设计要做到高内聚,一个类只承担单一的职责(不做不该它做的事情)。
    • 开闭原则(OCP):软件系统应该接受扩展(对扩展开放),不接受修改(对修改关闭)。要符合开闭原则:抽象是关键,封装可变性。
    • 依赖倒转原则(DIP):面向接口编程。声明变量的引用类型,声明方法的参数类型,声明方法的返回类型时,尽可能使用抽象类型而不是具体类型。
    • 里氏替换原则(LSP):用子类型替换父类型没有任何问题,用父类型替换子类型通常都是不行的。
    • 接口隔离原则(ISP):接口要小而专,不能大而全。
    • 合成聚合复用原则(CARP):优先考虑用强关联关系复用代码,而不是用继承关系复用代码。
    • 迪米特法则(LoD):对象之间尽可能少的发生联系。
package com.lovoinfo.ui;
/**
/*登录器与主窗口分段隐藏
*/
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
/*登录窗口
@SuppressWarnings("serial")
public class LoginFrame extends JDialog {
    private JLabel uidLabel, pwdLabel;
    private JTextField uidField;
    private JPasswordField pwdField;
    private JButton loginButton, cancelButton;

    private List userList = new ArrayList();//用户容器

    public LoginFrame() {
        this.setTitle("用户登录");
        this.setSize(300, 180);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        // this.setDefaultCloseOperation(HIDE_ON_CLOSE);//隐藏窗口

        initComponents();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    private boolean checkLogin(String username, String password) {
        for(User user : userList) {
            if(user.getUsername().equals(username)) {
                return user.getPassword().equals(password);
            }
        }

        return false;
    }

    private void initComponents() {
        userList.add(new User("admin", "123456"));
        userList.add(new User("hellokitty", "1qaz2wsx"));
        userList.add(new User("jack", "123123"));

        uidLabel = new JLabel("用户名: ");
        pwdLabel = new JLabel("密码: ");
        uidField = new JTextField();
        pwdField = new JPasswordField();
        loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String username = uidField.getText().trim();
                String password = new String(pwdField.getPassword());

                if(checkLogin(username, password)) {
                    LoginFrame.this.setVisible(false);
                    new MainFrame(LoginFrame.this).setVisible(true);
                }
                else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误", "错误", 0);
                }
            }
        });
        cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        this.setLayout(null);
        uidLabel.setBounds(40, 20, 50, 30);
        pwdLabel.setBounds(40, 60, 50, 30);
        uidField.setBounds(100, 25, 150, 20);
        pwdField.setBounds(100, 65, 150, 20);
        loginButton.setBounds(80, 100, 60, 30);
        cancelButton.setBounds(160, 100, 60, 30);

        this.add(uidLabel);
        this.add(pwdLabel);
        this.add(uidField);
        this.add(pwdField);
        this.add(loginButton);
        this.add(cancelButton);
    }

}

用户类

package com.lovoinfo.ui;

public class User {
    private String username;
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

}

主窗口

package com.lovoinfo.ui;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.lovoinfo.biz.impl.UserServiceImpl;
import com.lovoinfo.domain.User;

/**
 * 注册窗口(单例类)
 * @author jackfrued
 *
 */
@SuppressWarnings("serial")
public class RegInternalFrame extends JInternalFrame {

    private class ButtonHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == regButton) {
                String username = uidField.getText().trim();
                String password = new String(pwdField.getPassword());
                String email = emailField.getText().trim();
                User user = new User(username, password, email);
                user.setUserService(new UserServiceImpl());
                if(user.register()) {
                    JOptionPane.showMessageDialog(null, "注册成功!");
                    reset();
                    RegInternalFrame.this.setVisible(false);
                }
                else {
                    JOptionPane.showMessageDialog(null, "注册失败!", "错误提示",  0);
                }
            }
            else if(e.getSource() == resetButton) {
                reset();
            }
        }
    }

    private static Font defaultFont = new Font("微软雅黑", Font.PLAIN, 14);

    private String[] labelNames = {"用户名: ", "密码: ", "确认密码: ", "电子邮箱: "};
    private JLabel[] labels = new JLabel[4];
    private JTextField uidField, emailField;
    private JPasswordField pwdField, rePwdField;
    private JButton regButton, resetButton;

    private RegInternalFrame() {
        super("用户注册", false, true, false, false);

        this.setSize(270, 300);
        this.setLocation(50, 50);
        this.setVisible(true);
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);

        initComponents();
    }

    private static RegInternalFrame instance = new RegInternalFrame();

    public static RegInternalFrame getInstance() {
        return instance;
    }

    private void initComponents() {
        this.setLayout(null);

        for(int i = 0; i < labels.length; i++) {
            labels[i] = new JLabel(labelNames[i]);
            labels[i].setBounds(20, 30 + 48 * i, 70, 24);
            this.add(labels[i]);
        }

        uidField = new JTextField();
        uidField.setBounds(100, 30, 120, 24);
        this.add(uidField);

        pwdField = new JPasswordField();
        pwdField.setBounds(100, 78, 120, 24);
        this.add(pwdField);

        rePwdField = new JPasswordField();
        rePwdField.setBounds(100, 126, 120, 24);
        this.add(rePwdField);

        emailField = new JTextField();
        emailField.setBounds(100, 174, 150, 24);
        this.add(emailField);

        ActionListener handler = new ButtonHandler();

        regButton = new JButton("注册");
        regButton.setBounds(60, 230, 70, 24);
        regButton.addActionListener(handler);
        this.add(regButton);

        resetButton = new JButton("重置");
        resetButton.setBounds(150, 230, 70, 24);
        resetButton.addActionListener(handler);
        this.add(resetButton);

        // 为窗口上的所有控件设置统一的字体
        for(Component c : this.getContentPane().getComponents()) {
            c.setFont(defaultFont);
        }
    }

    private void reset() {
        // 为窗口上的所有控件设置统一的字体
        for(Component c : RegInternalFrame.this.getContentPane().getComponents()) {
            if(c instanceof JTextField) {
                ((JTextField) c).setText("");
            }
            else if(c instanceof JPasswordField) {
                ((JPasswordField) c).setText("");
            }
        }
    }

}
package com.lovoinfo.ui;

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class SysManPanel extends JPanel {

    private class MouseHandler extends MouseAdapter {
        private int index;

        public MouseHandler(int index) {
            this.index = index;
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            labels[index].setIcon(new ImageIcon(buttonRollImageNames[index]));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            labels[index].setIcon(new ImageIcon(buttonImageNames[index]));
        }

        @Override
        public void mousePressed(MouseEvent e) {
            labels[index].setIcon(new ImageIcon(buttonDownImageNames[index]));
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            labels[index].setIcon(new ImageIcon(buttonRollImageNames[index]));
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            RegInternalFrame frame = RegInternalFrame.getInstance();
            if(mainFrame.containsInternalFrame(frame)) {
                mainFrame.showInternalFrame(frame);
            }
            else {
                mainFrame.addInternalFrame(frame);
            }
        }

    }

    private static String[] buttonImageNames = {
        "操作员管理.png", "更改密码.png", "权限管理.png"
    };
    private static String[] buttonRollImageNames = {
        "操作员管理_roll.png", "更改密码_roll.png", "权限管理_roll.png"
    };
    private static String[] buttonDownImageNames = {
        "操作员管理_down.png", "更改密码_down.png", "权限管理_down.png"
    };

    private JLabel[] labels = new JLabel[3];

    private MainFrame mainFrame;

    public SysManPanel(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        this.setLayout(new FlowLayout(FlowLayout.LEFT));

        for(int i = 0; i < labels.length; i++) {
            labels[i] = new JLabel(new ImageIcon(buttonImageNames[i]));
            labels[i].addMouseListener(new MouseHandler(i));
            this.add(labels[i]);
        }
    }
}
package com.lovoinfo.ui;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;

@SuppressWarnings("serial")
public class MyDesktopPane extends JDesktopPane {
    private static ImageIcon bgImage = new ImageIcon("welcome.jpg");

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bgImage.getImage(), 0, 0, this.getWidth(), this.getHeight(), null);
    }

}

用户类及服务类


package com.lovoinfo.domain;

import com.lovoinfo.biz.UserService;

public class User {
    private String username;
    private String password;
    private String email;

    private UserService userService;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public boolean login() {
        return userService.doLogin(this);
    }

    public boolean register() {
        return userService.doRegister(this);
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    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 String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

package com.lovoinfo.biz;

import com.lovoinfo.domain.User;

/**
 * 用户业务逻辑接口
 * @author jackfrued
 *
 */
public interface UserService {

    /**
     * 登录
     * @param user 用户对象
     * @return 登录成功返回true否则返回false
     */
    public boolean doLogin(User user);

    /**
     * 注册
     * @param user 用户对象
     * @return 注册成功返回true否则返回false
     */
    public boolean doRegister(User user);
}

package com.lovoinfo.biz.impl;

import java.util.ArrayList;
import java.util.List;

import com.lovoinfo.biz.UserService;
import com.lovoinfo.domain.User;

/**
 * 用户业务逻辑实现类
 * @author jackfrued
 *
 */
public class UserServiceImpl implements UserService {
    private static List userList = new ArrayList();

    static {
        userList.add(new User("admin", "123456"));
        userList.add(new User("jack", "abcdefg"));
    }

    public UserServiceImpl() {
    }

    @Override
    public boolean doLogin(User user) {
        User temp = findUserByUsername(user.getUsername());
        if(temp != null) {
            return temp.getPassword().equals(user.getPassword());
        }
        return false;
    }

    @Override
    public boolean doRegister(User user) {
        User temp = findUserByUsername(user.getUsername());
        if(temp == null) {
            userList.add(user);
            return true;
        }
        return false;
    }

    // 根据用户名查找用户
    private User findUserByUsername(String username) {
        for(User temp : userList) {
            if(temp.getUsername().equals(username)) {
                return temp;
            }
        }
        return null;
    }

}

你可能感兴趣的:(面向对象,抽封继态,案例,接口,抽象类,类图,系统,登录器,贴图)