基础语句学完,也可以编写一些像样的程序了,现在要做的是多加练习,巩固下知识点,打好基础,daydayup!
题目:模仿银行ATM系统,可以创建用户,存钱,转账,修改密码注销账户等操作
思路:利用面向对象编程:1,定义一个账户类Account,至少需要包含(卡号、姓名、性别、密码、余额、每次取现额度);2,定义一个ATM类,用来代表ATM系统,负责提供所有的业务需求;3,定义一个测试类Test,负责对我们开发的ATM系统进行测试。
创建一个实体类,用来记录姓名、卡号、性别、密码、余额、每次取现额度等信息。
public class Account {
private String card;
private String username ;
private char sex;
private String password;
private double money;
private double limit;
public Account() {
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getUsername() {
return username + (sex=='男'? "先生":"女士");
}
public void setUsername(String username) {
this.username = username;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getLimit() {
return limit;
}
public void setLimit(double limit) {
this.limit = limit;
}
}
通过选择1,或选择2进入系统
public void start(){
Scanner sc =new Scanner(System.in);
while (true) {
System.out.println("======欢迎来到ATM=======");
System.out.println("1,用户登陆");
System.out.println("2,用户开户");
System.out.println("请选择");
int id =sc.nextInt();
switch (id){
case 1:
//用户登陆
login();
break;
case 2:
//用户开户
Operator();
break;
default:
System.out.println("请重新输入");
}
}
}
运用Scanner输入来记入需要记录的信息,值得一提的是:由于Scanner没办法记录char变量,所以使用charAt来取第一个值。
private void Operator(){
Account acc = new Account();
System.out.println("请输入姓名");
String name = sc.next();
acc.setUsername(name);
while (true) {
System.out.println("请输入性别(男/女)");
char sex = sc.next().charAt(0);
if (sex == '男'|| sex == '女'){
acc.setSex(sex);
break;
}else{
System.out.println("请输入(男/女)");
}
}
while (true) {
System.out.println("请设置你的密码");
String password = sc.next();
System.out.println("请再次输入你的密码");
String okpassword = sc.next();
if (password.equals(okpassword)){
acc.setPassword(okpassword);
break;
}else{
System.out.println("两次密码不一致,请重新输入");
}
}
System.out.println("请设置每日取款限额");
double limit = sc.nextDouble();
acc.setLimit(limit);
String id =card();
acc.setCard(id);
accounts.add(acc);
System.out.println("恭喜"+acc.getUsername()+"开户成功,您的卡号为:"+acc.getCard());
}
由于卡号需要随机生成8位数字,并且不能与其他人的号码重复,所以需要建立两个方法来做调试。一个方法用于生成8位随机数字,一个方法用于检测号码是否重复
private String card(){
Random r = new Random();
while (true) {
String id = "";
for (int i = 0; i < 8; i++) {
int data= r.nextInt(10);
id +=data;
}
Account acc= vs(id);
if (acc == null){
return id;
}
}
}
private Account vs (String card){
for (int i = 0; i <accounts.size(); i++) {
Account acc= accounts.get(i);
if (acc.getCard().equals(card)){
return acc;
}
}
return null;
?这样一来,开户也就成功了。接下来是登陆的操作。
需要注意的是:当系统中没有账号时,要提示没有账号。登陆时需要注意号码的匹对。
private void login(){
if (accounts.size()==0){
System.out.println("请先创建账号");
return;
}
while (true) {
System.out.println("请输入卡号");
String card = sc.next();
Account acc = vs(card);
if(acc == null){
System.out.println("没有该账号,请重新输入");
}else if(acc.getCard().equals(card)){
while (true) {
System.out.println("请输入密码");
String password =sc.next();
if (acc.getPassword().equals(password)){
acco = acc;
check();
return;
}else{
System.out.println("密码不正确,请重新输入");
}
}
}
}
}
登陆成功后,便可进行业务选择。运用switch语句可以精准选择业务需求
private void check(){
while (true) {
System.out.println(acco.getUsername()+"你可以办理以下业务");
System.out.println("1.查询账户");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.转账");
System.out.println("5.修改密码");
System.out.println("6.退出");
System.out.println("7.注销账户");
System.out.println("请选择");
int check= sc.nextInt();
switch (check){
case 1:
idcheck();
break;
case 2:
moenycheck();
break;
case 3:
moneyleave();
break;
case 4:
transmoney();
break;
case 5:
changepassword();
return;
case 6:
System.out.println("你已经退出");
return;
case 7:
if (deleteid());
return;
default:
System.out.println("你输入的数字有误,请重新输入");
}
}
}
建议独立一个方法,其他没什么需要注意的
private void idcheck(){
System.out.println("号码:"+acco.getCard());
System.out.println("性别:"+acco.getSex());
System.out.println("存款:"+acco.getMoney());
System.out.println("每日限额:"+acco.getLimit());
}
?熟用switch语句及死循环能够很好的解决问题
private void moenycheck() {
while (true) {
System.out.println("欢迎来到存款界面");
System.out.println("存款请按1");
System.out.println("退出请按2");
int sd = sc.nextInt();
switch (sd){
case 1:
System.out.println("请输入你要存多少");
double money= sc.nextDouble();
System.out.println("请问你确定要存入"+money+"么");
System.out.println("确定请按1");
System.out.println("返回请按2");
int cc =sc.nextInt();
switch (cc){
case 1:
acco.setMoney(acco.getMoney()+money);
System.out.println("您的余额为"+acco.getMoney());
break;
case 2:
return;
default:
System.out.println("请重新输入");
}
break;
case 2:
return;
default:
System.out.println("请重新输入");
}
}
}
一连串的switch语句和if问句,解决每一项逻辑
private void moneyleave() {
while (true) {
System.out.println("欢迎来到取款界面");
System.out.println("取款请按1");
System.out.println("退出请按2");
int sd = sc.nextInt();
switch(sd){
case 1:
System.out.println("你目前的存款为" + acco.getMoney());
if (acco.getMoney() < 100) {
System.out.println("最低取款金额为100,您的余额不足");
break;
} else {
System.out.println("请输入你要取走的金额");
double money = sc.nextDouble();
if (acco.getMoney()<money){
System.out.println("您的余额不足,请重新输入");
break;
}else{
if (money>acco.getLimit()){
System.out.println("您已超过限额,请重新输入");
break;
}else{
System.out.println("您已取走"+money+"元");
acco.setMoney(acco.getMoney()- money);
System.out.println("您的余额为:"+acco.getMoney());
}
}
}
break;
case 2:
return;
default:
System.out.println("请重新输入");
}
}
}
选要注意的是:这里需要判断对方的姓氏,采用的方法为“*”加上第二位开始的名字。转账者需要填写姓氏后运用startwith语句进行匹配。
private void transmoney() {
while (true) {
System.out.println("欢迎来到转账界面");
System.out.println("转账请按1");
System.out.println("退出请按2");
int sd = sc.nextInt();
switch (sd){
case 1:
if (accounts.size()<2){
System.out.println("当前系统中只有一个账号,请创建新的账号");
break;
}
if (acco.getMoney()==0){
System.out.println("您的余额不足,不能转账");
break;
}
System.out.println("请输入对方的账号");
String id =sc.next();
Account acc =vs(id);
if (acc == null){
System.out.println("没有该账号,请重新输入");
}else{
String name ="*"+acc.getUsername().substring(1);
System.out.println("请输入【"+name+"】的姓氏");
String trname =sc.next();
if (acc.getUsername().startsWith(trname)){
System.out.println("请输入转账金额");
double money =sc.nextDouble();
if (acco.getMoney() >= money){
acco.setMoney(acco.getMoney()-money);
acc.setMoney(acc.getMoney()+ money);
System.out.println("您已转账"+money+"元,您的余额为"+acco.getMoney());
break;
}else{
System.out.println("您的余额不足,不能转账");
}
}else {
System.out.println("姓氏认证有问题");
}
}
break;
case 2:
return;
}}
}
使用if语句询问即可,需要注意的是,最后要用return返回,不能用break,(return是退出方法,break是退出循环)
private void changepassword() {
while (true) {
System.out.println("欢迎来到更换密码界面");
System.out.println("输入当前密码");
String pass =sc.next();
if (acco.getPassword().equals(pass)){
System.out.println("输入新密码");
String okpass =sc.next();
System.out.println("再一次输入新密码");
String okkpass =sc.next();
if (okkpass.equals(okpass)){
acco.setPassword(okkpass);
System.out.println("修改密码成功");
return;
}else{
System.out.println("密码有误");
}
}else {
System.out.println("密码有误");
}
}
}
用return即可
case 6:
System.out.println("你已经退出");
return;
删除当前账户即可。当前账户和创建账户的实体类是同一个地址,删除当前账户就是在ArrayList中删除了当前账户实体类的地址
private boolean deleteid() {
while (true) {
System.out.println("确定删除么(y/n)");
String sd =sc.next();
switch (sd){
case "y":
if (acco.getMoney()==0){
accounts.remove(acco);
return true;
}else{
System.out.println("还有存款,不能销户");
return false;
}
default:
System.out.println("删除失败");
return false;
}
}
}
这样操作类就完成了,有需要的可以复制这个完整版
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMOperator {
private ArrayList<Account> accounts = new ArrayList<>();
private Scanner sc = new Scanner(System.in);
private Account acco = new Account();
public void start(){
Scanner sc =new Scanner(System.in);
while (true) {
System.out.println("======欢迎来到ATM=======");
System.out.println("1,用户登陆");
System.out.println("2,用户开户");
System.out.println("请选择");
int id =sc.nextInt();
switch (id){
case 1:
//用户登陆
login();
break;
case 2:
//用户开户
Operator();
break;
default:
System.out.println("请重新输入");
}
}
}
private void login(){
if (accounts.size()==0){
System.out.println("请先创建账号");
return;
}
while (true) {
System.out.println("请输入卡号");
String card = sc.next();
Account acc = vs(card);
if(acc == null){
System.out.println("没有该账号,请重新输入");
}else if(acc.getCard().equals(card)){
while (true) {
System.out.println("请输入密码");
String password =sc.next();
if (acc.getPassword().equals(password)){
acco = acc;
check();
return;
}else{
System.out.println("密码不正确,请重新输入");
}
}
}
}
}
private void check(){
while (true) {
System.out.println(acco.getUsername()+"你可以办理以下业务");
System.out.println("1.查询账户");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.转账");
System.out.println("5.修改密码");
System.out.println("6.退出");
System.out.println("7.注销账户");
System.out.println("请选择");
int check= sc.nextInt();
switch (check){
case 1:
idcheck();
break;
case 2:
moenycheck();
break;
case 3:
moneyleave();
break;
case 4:
transmoney();
break;
case 5:
changepassword();
return;
case 6:
System.out.println("你已经退出");
return;
case 7:
if (deleteid());
return;
default:
System.out.println("你输入的数字有误,请重新输入");
}
}
}
private void changepassword() {
while (true) {
System.out.println("欢迎来到更换密码界面");
System.out.println("输入当前密码");
String pass =sc.next();
if (acco.getPassword().equals(pass)){
System.out.println("输入新密码");
String okpass =sc.next();
System.out.println("再一次输入新密码");
String okkpass =sc.next();
if (okkpass.equals(okpass)){
acco.setPassword(okkpass);
System.out.println("修改密码成功");
return;
}else{
System.out.println("密码有误");
}
}else {
System.out.println("密码有误");
}
}
}
private boolean deleteid() {
while (true) {
System.out.println("确定删除么(y/n)");
String sd =sc.next();
switch (sd){
case "y":
if (acco.getMoney()==0){
accounts.remove(acco);
return true;
}else{
System.out.println("还有存款,不能销户");
return false;
}
default:
System.out.println("删除失败");
return false;
}
}
}
private void transmoney() {
while (true) {
System.out.println("欢迎来到转账界面");
System.out.println("转账请按1");
System.out.println("退出请按2");
int sd = sc.nextInt();
switch (sd){
case 1:
if (accounts.size()<2){
System.out.println("当前系统中只有一个账号,请创建新的账号");
break;
}
if (acco.getMoney()==0){
System.out.println("您的余额不足,不能转账");
break;
}
System.out.println("请输入对方的账号");
String id =sc.next();
Account acc =vs(id);
if (acc == null){
System.out.println("没有该账号,请重新输入");
}else{
String name ="*"+acc.getUsername().substring(1);
System.out.println("请输入【"+name+"】的姓氏");
String trname =sc.next();
if (acc.getUsername().startsWith(trname)){
System.out.println("请输入转账金额");
double money =sc.nextDouble();
if (acco.getMoney() >= money){
acco.setMoney(acco.getMoney()-money);
acc.setMoney(acc.getMoney()+ money);
System.out.println("您已转账"+money+"元,您的余额为"+acco.getMoney());
break;
}else{
System.out.println("您的余额不足,不能转账");
}
}else {
System.out.println("姓氏认证有问题");
}
}
break;
case 2:
return;
}}
}
private void moneyleave() {
while (true) {
System.out.println("欢迎来到取款界面");
System.out.println("取款请按1");
System.out.println("退出请按2");
int sd = sc.nextInt();
switch(sd){
case 1:
System.out.println("你目前的存款为" + acco.getMoney());
if (acco.getMoney() < 100) {
System.out.println("最低取款金额为100,您的余额不足");
break;
} else {
System.out.println("请输入你要取走的金额");
double money = sc.nextDouble();
if (acco.getMoney()<money){
System.out.println("您的余额不足,请重新输入");
break;
}else{
if (money>acco.getLimit()){
System.out.println("您已超过限额,请重新输入");
break;
}else{
System.out.println("您已取走"+money+"元");
acco.setMoney(acco.getMoney()- money);
System.out.println("您的余额为:"+acco.getMoney());
}
}
}
break;
case 2:
return;
default:
System.out.println("请重新输入");
}
}
}
private void moenycheck() {
while (true) {
System.out.println("欢迎来到存款界面");
System.out.println("存款请按1");
System.out.println("退出请按2");
int sd = sc.nextInt();
switch (sd){
case 1:
System.out.println("请输入你要存多少");
double money= sc.nextDouble();
System.out.println("请问你确定要存入"+money+"么");
System.out.println("确定请按1");
System.out.println("返回请按2");
int cc =sc.nextInt();
switch (cc){
case 1:
acco.setMoney(acco.getMoney()+money);
System.out.println("您的余额为"+acco.getMoney());
break;
case 2:
return;
default:
System.out.println("请重新输入");
}
break;
case 2:
return;
default:
System.out.println("请重新输入");
}
}
}
private void idcheck(){
System.out.println("号码:"+acco.getCard());
System.out.println("性别:"+acco.getSex());
System.out.println("存款:"+acco.getMoney());
System.out.println("每日限额:"+acco.getLimit());
}
private void Operator(){
Account acc = new Account();
System.out.println("请输入姓名");
String name = sc.next();
acc.setUsername(name);
while (true) {
System.out.println("请输入性别(男/女)");
char sex = sc.next().charAt(0);
if (sex == '男'|| sex == '女'){
acc.setSex(sex);
break;
}else{
System.out.println("请输入(男/女)");
}
}
while (true) {
System.out.println("请设置你的密码");
String password = sc.next();
System.out.println("请再次输入你的密码");
String okpassword = sc.next();
if (password.equals(okpassword)){
acc.setPassword(okpassword);
break;
}else{
System.out.println("两次密码不一致,请重新输入");
}
}
System.out.println("请设置每日取款限额");
double limit = sc.nextDouble();
acc.setLimit(limit);
String id =card();
acc.setCard(id);
accounts.add(acc);
System.out.println("恭喜"+acc.getUsername()+"开户成功,您的卡号为:"+acc.getCard());
}
private String card(){
Random r = new Random();
while (true) {
String id = "";
for (int i = 0; i < 8; i++) {
int data= r.nextInt(10);
id +=data;
}
Account acc= vs(id);
if (acc == null){
return id;
}
}
}
private Account vs (String card){
for (int i = 0; i <accounts.size(); i++) {
Account acc= accounts.get(i);
if (acc.getCard().equals(card)){
return acc;
}
}
return null;
}
}
public class ATMDemo {
public static void main(String[] args) {
ATMOperator de = new ATMOperator();
de.start();
}
}
?测试效果:
总结:完美运行,有些语句需要在加强,熟用if语句和switch可以完成精准操作
整理结束撒花!!!!?
?