主要分为商户和用户,将他们注册的账号和密码存在文件中,用IO流去读取
package com.kd.dao;
public interface MerchantsDao {
public boolean mlogin(String userName, String passwd);
public boolean mregist(String userName, String passwd) throws Exception;
}
package com.kd.dao;
public interface UserDao {
public boolean ulogin(String userName,String passwd);
public boolean uregister(String userName, String passwd) throws Exception;
}
package com.kd.dao.impl;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import com.kd.dao.MerchantsDao;
import com.kd.dao.UserDao;
public class DaoImpl implements UserDao, MerchantsDao {
@Override
public boolean mregist(String userName, String passwd) throws Exception {
Properties properties = new Properties();
properties.setProperty(userName, passwd);
properties.store(new FileWriter("Merchants.txt", true), "商家");
return true;
}
@Override
public boolean mlogin(String userName, String passwd) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("Merchants.txt"));
Set<Object> set = properties.keySet();
for (Object key : set) {
if (userName.equals(key) && passwd.equals(properties.get(key))) {
System.out.println("登录成功!!");
return true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean uregister(String userName, String passwd) throws Exception {
Properties properties = new Properties();
properties.setProperty(userName, passwd);
properties.store(new FileWriter("User.txt", true), "顾客");
return true;
}
@Override
public boolean ulogin(String userName, String passwd) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("User.txt"));
Set<Object> set = properties.keySet();
for (Object key : set) {
if (userName.equals(key) && passwd.equals(properties.get(key))) {
System.out.println("登录成功!!");
return true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
package com.kd.view;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import com.kd.dao.impl.DaoImpl;
public class LoginView {
Frame frame = new Frame("潮流前线");
public LoginView() {
init();
}
public void init() {
frame.setBounds(500, 700, 250, 200);
Label userlabel = new Label("用户名:");
Label passwdlabel = new Label(" 密 码:");
TextField userText = new TextField(20);
TextField passwdText = new TextField(20);
JRadioButton jrb1 = new JRadioButton(" 商 户");
JRadioButton jrb2 = new JRadioButton(" 顾 客");
JButton loginButton = new JButton("登 录");
JButton registButton = new JButton("注 册");
FlowLayout layout = new FlowLayout();
frame.setLayout(layout);
frame.add(userlabel);
frame.add(userText);
frame.add(passwdlabel);
frame.add(passwdText);
frame.add(jrb1);
frame.add(jrb2);
frame.add(loginButton);
frame.add(registButton);
frame.setVisible(true);
registButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
RegistView registView = new RegistView();
}
});
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String usertext = userText.getText();
String passwdtext = passwdText.getText();
if (jrb1.isSelected()) {
boolean result = new DaoImpl().mlogin(usertext, passwdtext);
if (result) {
frame.dispose();
MerchantsView merchantsView = new MerchantsView();
}
} else {
boolean result = new DaoImpl().ulogin(usertext, passwdtext);
if (result) {
frame.dispose();
UserView userView = new UserView();
}
}
}
});
windowLis();
}
public void windowLis() {
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
});
}
}
package com.kd.view;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import com.kd.dao.MerchantsDao;
import com.kd.dao.UserDao;
import com.kd.dao.impl.DaoImpl;
public class RegistView {
MerchantsDao merchants = new DaoImpl();
UserDao user = new DaoImpl();
Frame frame = new Frame("潮流前线用户注册");
public RegistView() {
init();
}
public void init() {
frame.setBounds(500, 700, 300, 200);
Label userlabel = new Label("注册用户名:");
Label passwdlabel = new Label("注 册 密 码:");
TextField userText = new TextField(20);
TextField passwdText = new TextField(20);
JRadioButton jrb1 = new JRadioButton("商 户");
JRadioButton jrb2 = new JRadioButton("顾 客");
Label label = new Label(" ");
JButton button = new JButton("确 认");
JButton button2 = new JButton("返 回");
FlowLayout layout = new FlowLayout();
frame.setLayout(layout);
frame.add(userlabel);
frame.add(userText);
frame.add(passwdlabel);
frame.add(passwdText);
frame.add(jrb1);
frame.add(label);
frame.add(jrb2);
frame.add(button);
frame.add(button2);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String usertext = userText.getText();
String passwdtext = passwdText.getText();
boolean result;
if (jrb1.isSelected()) {
try {
result = merchants.mregist(usertext, passwdtext);
if (result) {
System.out.println("注册成功!!");
} else {
System.out.println("注册失败,请重新注册!!");
}
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
try {
result = user.uregister(usertext, passwdtext);
if (result) {
System.out.println("注册成功!!");
} else {
System.out.println("注册失败,请重新注册!!");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
// try {
// new DaoImpl().regist(usertext, passwdtext);
// } catch (Exception e1) {
// e1.printStackTrace();
// }
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
LoginView loginView = new LoginView();
}
});
frame.setVisible(true);
windowLis(frame);
}
public void windowLis(Frame frame) {
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
}
}
package com.kd.test;
import com.kd.view.LoginView;
public class Test {
public static void main(String[] args) {
LoginView loginView = new LoginView();
}
}
回头再补一个用数据库写的登录注册,哈哈,新手上路请多关照!!