构造器和封装

来源:互联网 发布:acl限制端口 编辑:程序博客网 时间:2024/04/28 13:01




ATM机采用面向对象编程,使用本次所叙述知识点




package com.poke.bean;


import java.util.Scanner;


//ATM机
public class ATMMachine {


private UserInfo user;// 用户信息


private int cash = 100000;// ATM现金


public final int MAX_CASH = 200000;// 最大现金量


public ATMMachine() {
// 生成user对象的数据
this.user = new UserInfo("J129", "123456", 231.5);
}


// 运行---
public void run() {
this.welcome();
boolean flag = this.login();
if (flag) {
System.out.println("登录成功!");
while (true) {
int choice = this.chooseMenu();
switch (choice) {
case 1:
this.queryAccount();
break;
case 2:
this.storeMoney();
break;
case 3:
this.getMoney();
break;
case 4:
this.changePwd();
break;
case 5:
this.exitSystem();
break;
default:
System.out.println("没有该选项。");
break;
}
}
} else {
System.out.println("三次机会用完,你的卡被没收!");
}
}


// 欢迎方法
private void welcome() {
System.out.println("***************************");
System.out.println("欢迎来到我的银行");
System.out.println("           我们的宗旨是能亏就不赚 ");
System.out.println("***************************");
}


// 登录
private boolean login() {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String inputName = scan.next();
System.out.println("请输入密   码:");
String inputPwd = scan.next();
if (inputName.equals(this.user.getUsername())
&& inputPwd.equals(this.user.getPassword())) {
return true;
} else {
System.out.println("用户名密码有误!你还有" + (2 - i) + "次机会!");
}


}
return false;
}


//选择菜单
private int chooseMenu() {
Scanner scan = new Scanner(System.in);
System.out.println("请选择你要执行的操作:");
System.out.println("1、查询;2、存款;3、取款;4、修改密码;5、退出");
int choice = scan.nextInt();

return choice;
}


//查看余额
private void queryAccount() {
System.out.println("你当前的余额是:" + this.user.getAccount() + "元。");
}


//存钱
private void storeMoney() {
Scanner scanner = new Scanner(System.in);
System.out.println("你要存多少钱?");
int inPutMoney = scanner.nextInt();
if (inPutMoney > 0 && inPutMoney % 100 == 0 && (this.user.getAccount() + inPutMoney) < MAX_CASH) {
System.out.println("存钱成功,卡内余额为:" + (this.user.getAccount() + inPutMoney) + "元。");
}else {
System.out.println("存入失败!原因:1、存入金额必须为整百金额!2、存入金额超过ATM最大存款极限!");
}
}


//取钱
private void getMoney() {
System.out.println("你要取多少钱?");
Scanner scanner = new Scanner(System.in);
int outPutMoney = scanner.nextInt();
if (outPutMoney > 0 && outPutMoney <= this.user.getAccount() && outPutMoney % 100 == 0 && outPutMoney < MAX_CASH) {
System.out.println("取款成功!卡内余额为:" + (this.user.getAccount() - outPutMoney) + "元。");
} else {
System.out.println("取款失败!原因:1、取款金额必须为整百金额!2、卡内余额不足!");
}
}


//更改密码
private void changePwd() {
Scanner scanner = new Scanner(System.in);
System.out.println("输入原密码:");
String changePwd = scanner.next();
if (changePwd.equals(this.user.getPassword())) {
System.out.println("输入新密码:");
String changePwdNew = scanner.next();
System.out.println("请再次输入新密码:");
String changePwdNewAgain = scanner.next();
if (changePwdNew.equals(changePwdNewAgain)) {
//修改成功,显示修改后的密码
//System.out.println("密码成功修改为:" + changePwdNewAgain + "!");
//修改成功,不显示修改后的密码
System.out.println("密码修改成功!");
} else {
System.out.println("新密码两次输入不一致!");
}
} else {
System.out.println("原密码输入错误!");
}
}


//退出
private void exitSystem() {
System.out.println("谢谢你的使用,请收好你的财物。");
System.exit(0);
}


}






package com.poke.bean;

//用户信息
public class UserInfo {


private String username;


private String password;


private double account;


public UserInfo() {


}


//构造器
public UserInfo(String username, String password, double account) {
super();
this.username = username;
this.password = password;
this.account = account;
}


//生成get和set方法
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 double getAccount() {
return account;
}


public void setAccount(double account) {
this.account = account;
}
}



package com.poke.test;

import com.poke.bean.ATMMachine;


public class TestMain {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ATMMachine atm = new ATMMachine();
//调用运行方法
atm.run();
}


}

0 0
原创粉丝点击