继承
2015-08-13 20:39
225 查看
封装的知识点基本结束了,现在开始学习继承(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;
}
}
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;
}
}
相关文章推荐
- 小鑫找基友 SDUT 3115
- 对你的再次认识——机房的整体分析
- Linux操作系统1——命令概览(待继续完善)
- Python - 安全替换字符串模板(safe_substitute) 详细解释
- GridView控件的光棒效应
- nodejs中两种定义函数的方法
- AndroidStudio制作的NinePatch无法使用,且R类标红
- 稀疏
- C++基础之表达式笔记
- ccf 集合竞价(只有80十分,而且还是参考大神的代码才做出来的)
- 复选框全选、批量删除
- Mac OS 链接Android USB
- Java-封装生成JSON数据和XML数据类
- 算法题: 部分A+B (15)
- Android Opengl 初学(画一个逐渐扩大的圆圈)
- hdu-1312-Red and Black
- WinCE 开发问题:不支持 Open Generic 方法的 GetParameters。
- NYOJ-1006喷水设置(一)-贪心
- 使用common-fileUpload实现文件上传
- how to show provisionning profile information