继承

来源:互联网 发布:西门子工业软件怎么样 编辑:程序博客网 时间:2024/06/05 03:34

        封装的知识点基本结束了,现在开始学习继承(extends),留下今天学习足迹:

public class ATM {
public Properties pro=new Properties();
public User currentUser;
public ATM(){
try{
pro.load(new FileReader("src\\com\\lovo\\atmTest\\bank.txt"));
}catch(Exception e){
System.out.print("文件未找到");
}
currentUser=login();
if(currentUser==null){
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}
while(true){
String x=JOptionPane.showInputDialog(null,"1、存款\n2、取款\n3、查询余额\n4、转账\n5、改密\n6、取卡\n请选择:\n");
int item=Integer.parseInt(x);
switch(item){
case 1:
saveMoney();
break;
case 2:
getMoney();
break;
case 3:
showMoney();
break;
case 4:
changeMoney();
break;
case 5:
changePwd();
break;
case 6:
JOptionPane.showMessageDialog(null, "谢谢使用");
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "请输入1--6");
break;
}
}

}
/**
* 改密
*/
public void changePwd() {
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", newPwd+"");
saveFile();
JOptionPane.showMessageDialog(null, "改密成功,请重新登录");
login();

}
/**
* 转账
*/
public void changeMoney() {
String code=JOptionPane.showInputDialog("请输入转账账户");
String a=pro.getProperty(code+".money");
if(a==null){
JOptionPane.showMessageDialog(null, "账户错误");
return;
}
String m=JOptionPane.showInputDialog("请输入转账金额");
int mm=Integer.parseInt(m);
if(mm>currentUser.money){
JOptionPane.showMessageDialog(null, "账户余额不足");
return;
}
currentUser.money-=mm;
int aa=Integer.parseInt(a);
aa+=mm;
pro.setProperty(currentUser.code+".money", currentUser.money+"");
pro.setProperty(code+".money", aa+"");
saveFile();
showMoney();
}
/**
* 显示余额
*/
public void showMoney() {
String money=pro.getProperty(currentUser.code+".money");
JOptionPane.showMessageDialog(null,"账户余额为:"+Integer.parseInt(money));

}
/**
* 取钱
*/
public void getMoney() {
String m=JOptionPane.showInputDialog("请输入取款金额");
int mm=Integer.parseInt(m);
if(mm>currentUser.money){
JOptionPane.showMessageDialog(null, "账户余额不足");
}
currentUser.money-=mm;
pro.setProperty(currentUser.code+".money", currentUser.money+"");
saveFile();
showMoney();
}
/**
* 存钱
*/
public void saveMoney() {
String m=JOptionPane.showInputDialog("请输入存款金额");
int mm=Integer.parseInt(m);
currentUser.money+=mm;
pro.setProperty(currentUser.code+".money", currentUser.money+"");
saveFile();
showMoney();
}
/**
* 存款
*/
public void saveFile(){
try{
pro.store(new FileWriter("src\\com\\lovo\\atmTest\\bank.txt"),null);
}catch(Exception e){
System.out.print("文件未找到");
}
}
/**
* 登录
* @return 登录状况,如果失败,返回null
*/
public User login(){
for(int i=0;i<3;i++){
String userCode=JOptionPane.showInputDialog(null,"请输入帐号");
String userPwd=JOptionPane.showInputDialog(null,"请输入密码");

String p=pro.getProperty(userCode+".pwd");
if(userPwd.equals(p)){
JOptionPane.showMessageDialog(null, "登陆成功");
User u=new User();
u.code=userCode;
u.pwd=userPwd;
u.money=Integer.parseInt(pro.getProperty(userCode+".money"));
return u;
}
JOptionPane.showMessageDialog(null, "帐号或密码错误");
}
return null;
}
}

0 0