这周又学习了很多只是,主要也复习巩固了之前所学的,下面就跟大家分享一下以前的一些项目:
/**
* 用户类
* @author Administrator
*
*/
public class User {
/**
* 账号
*/
public String code;
/**
* 密码
*/
public String pwd;
/**
* 余额
*/
public int money;
public User(String c,String p,int m){
code=c;
pwd =p;
money=m;
}
public User(){
}
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import javax.swing.JOptionPane;
public class ATM {
public Properties pro = new Properties();
/**
* 当前登录账号
*/
public User currentuser;
/**
* 构造方法
*/
public ATM() {
try {
pro.load(new FileReader("ATM.txt"));
} catch (Exception e) {
System.out.println("未找到文件");
}
currentuser = login();
if (currentuser == null) {
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}
while (true) {
String x = JOptionPane.showInputDialog(null,
"1、存款\n2、取款\n3、余额\n4、转账\n5、改密\n6、退出");
int b = Integer.parseInt(x);
switch (b) {
case 1:
savemoney();
break;
case 2:
getmoney();
break;
case 3:
showmoney();
break;
case 4:
zhuanmoney();
break;
case 5:
updatepwd();
break;
case 6:
System.exit(0);
default:
JOptionPane.showMessageDialog(null, "请输入1~6的选项");
break;
}
}
}
/**
* 改密
*/
public void updatepwd() {
String oldpwd = JOptionPane.showInputDialog(null, "请输入原密码");
if (oldpwd.equals(currentuser.pwd) == false) {
JOptionPane.showMessageDialog(null, "原密码输入错误");
return;
}
String newpwd = JOptionPane.showInputDialog(null, "请输入新密码");
String repwd = JOptionPane.showInputDialog(null, "请确认新密码");
if (repwd.equals(newpwd) == false) {
JOptionPane.showMessageDialog(null, "密码确认错误");
return;
}
currentuser.pwd = newpwd;
pro.setProperty(currentuser.code + ".pwd", currentuser.pwd);
saveFile();
JOptionPane.showMessageDialog(null, "密码修改成功");
}
/**
* 转账
*/
public void zhuanmoney() {
String newcode = JOptionPane.showInputDialog(null, "请输入转账账号");
String newmoney = JOptionPane.showInputDialog(null, "请输入转入金额");
int n = Integer.parseInt(newmoney);
if (n > currentuser.money) {
JOptionPane.showMessageDialog(null, "账户余额不足");
return;
}
String x = pro.getProperty(newcode + ".money");
if (x == null) {
JOptionPane.showMessageDialog(null, "账号不存在");
return;
}
currentuser.money -= n;
int money = Integer.parseInt(x);
money += n;
pro.setProperty(currentuser.code + ".money", currentuser.money + "");
pro.setProperty(newcode + ".money", money + "");
saveFile();
JOptionPane.showMessageDialog(null, "转账成功");
showmoney();
}
/**
* 余额
*/
public void showmoney() {
JOptionPane.showMessageDialog(null, currentuser.money);
}
/**
* 取款
*/
public void getmoney() {
String z = JOptionPane.showInputDialog(null, "请输入取款金额");
int n = Integer.parseInt(z);
if (currentuser.money < n) {
JOptionPane.showMessageDialog(null, "余额不足");
return;
}else{
currentuser.money -= n;
}
pro.setProperty(currentuser.code + ".money", currentuser.money + "");
saveFile();
showmoney();
}
/**
* 存款
*/
public void savemoney() {
String y = JOptionPane.showInputDialog(null, "请输入存款金额");
int n = Integer.parseInt(y);
currentuser.money += n;
pro.setProperty(currentuser.code + ".money", currentuser.money + "");
saveFile();
showmoney();
}
/**
* 保存文件
*/
public void saveFile() {
try {
pro.store(new FileWriter("ATM.txt"), null);
} catch (Exception e) {
System.out.println("未找到文件");
}
}
/**
* 登陆
*
* @return 当前登录账号或null
*/
public User login() {
for (int i = 0; i < 3; i++) {
String usercode = JOptionPane.showInputDialog(null, "请输入账号");
String userpwd = JOptionPane.showInputDialog(null, "请输入密码");
String value = pro.getProperty(usercode + ".pwd");
if (userpwd.equals(value)) {
User u = new User();
u.code = usercode;
u.pwd = userpwd;
u.money = Integer
.parseInt(pro.getProperty(usercode + ".money"));
return u;
} else {
JOptionPane.showMessageDialog(null, "账号或密码错误");
}
}
return null;
}
public class ATMtest {
public static void main(String[] args) {
ATM a = new ATM();
}
}