该项目可以分为三部分:账户类,ATM管理类,测试类
以上也是我思考的过程:下面就是按照顺序的程序:
public class Account {
//账户类 :负责存储(set),获取(get)账户的信息
//信息:账户的 卡号,户主名,性别,密码,余额,限额;
private String idcard;
private String username;
private char sex;
private String password;
private double money;
private double limit;
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
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;
}
}
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATM {
private ArrayList accounts = new ArrayList<>();//将账户信息统一管理并私有化;
private Scanner sc = new Scanner(System.in);
private Account loginaccount;
//欢迎页;
public void start()//ok了
{
while (true) {
System.out.println("=====欢迎进入ATM系统=====");
System.out.println("1.用户开户");
System.out.println("2.用户登录");
System.out.println("3.退出系统");
System.out.println("请您输入相应的操作数,选择要进行的业务类型:");
int command = sc.nextInt();
switch (command)
{
//用户开户;
case 1:
createAccount();
break;
//用户登录
case 2:
login();
break;
case 3:
System.out.println("您已经退出系统!");
return;
default:
System.out.println("没有该操作!");
break;
}
}
}
//用户登录后操作页面方法:
private void showUsercommand() {
//*为了防止用户出现乱输入命令,放在死循环中
while (true) {
System.out.println("以下是可以办理的全部业务,请您输入对应的数字进行相应的业务办理:");
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.退出登录");
int command = sc.nextInt();
switch (command)
{
case 1://账户查询;
showloginAccount();
break;
case 2://存款;
inmoney();
break;
case 3://取款;
outmoney();
break;
case 4://转账;
transfermoney();
break;
case 5://修改密码;
changepassword();
break;//这里要返回到欢迎页;
case 6://销户;
if(deleteaccount())
{
System.out.println("欢迎您下次再来!");
return;
}
break;
case 7://退出登录;
System.out.println(loginaccount.getUsername() + "您已经成功退出系统!");
return;
default:
System.out.println("请您输入正确命令!");
break;
}
}
}
//销户;
private boolean deleteaccount() {
//就是把当前登录账户从账户集合中移除掉;
//先判断当前账户中是否还存在钱;
System.out.println("请问您确认要销户吗?请输入“是”或“否”!");
String choice = sc.next();
//如果确认销户,则开始销户判断
if (choice.equalsIgnoreCase("是")) {
if(loginaccount.getMoney() == 0)
{
//销户;
accounts.remove(loginaccount);
System.out.println("您已经完成销户!");
//完成销户后要返回欢迎页面;
return true;
}
else{
System.out.println("您的账户中还有钱,请全部取出后再来销户吧!");
return false;
}
}
else
return false;
}
//修改密码;
private void changepassword() {
while (true) {
System.out.println("请输入原密码:");
String password = sc.next();
if(password.equals(loginaccount.getPassword())) {
System.out.println("请输入新密码:");
String newpassword = sc.next();
System.out.println("请再次确认新密码:");
String okpassword = sc.next();
if (okpassword.equals(newpassword)) {
loginaccount.setPassword(newpassword);
System.out.println("密码修改成功!");
return;
} else {
System.out.println("两次密码输入不一样,请确认!");
}
}
else
{
System.out.println("您输入的原密码错误请确认!");
}
// 询问用户是否还要进行修改密码的操作
System.out.println("是否还要进行修改密码的操作?请输入“是”或者“否”:");
String choice = sc.next();
if (choice.equalsIgnoreCase("否")) {
break;
}
}
}
//转账;
private void transfermoney() {
//先判断系统中是否存在两个及以上的客户;
if(accounts.size()<2)
{
System.out.println("------目前系统只有你一个客户,无法转账!!------");
return;
}
//再判断当前的账户中是否有钱;
if(loginaccount.getMoney()==0)
{
System.out.println("-----您的账户中都没钱,请先进行存款操作!------");
return;
}
//再判断转账的金额是否大于账户余额;
System.out.println("请输入待转账的卡号:");
String idcard = sc.next();
Account account = judgebyID(idcard);
while (true) {
System.out.println("请输入转账的金额:");
double money = sc.nextDouble();
if(money>loginaccount.getMoney())
System.out.println("对不起,您的余额不足!您的余额为:" + loginaccount.getMoney());
//最后 更新转账账户和收款账户的余额;
else
{
loginaccount.setMoney(loginaccount.getMoney() - money);
account.setMoney(account.getMoney() + money);
System.out.println("转账成功!");
break;
}
// 询问用户是否还要进行转账操作
System.out.println("是否还要进行转账操作?请输入“是”或者“否”:");
String choice = sc.next();
if (choice.equalsIgnoreCase("否")) {
break;
}
}
}
//取款;
private void outmoney() {
//先判断余额是否高于最低取款标准,如果没有,直接退出;
if(loginaccount.getMoney() < 100)
return;
else
{
while (true) {
System.out.println("请您输入取款金额:");
double money = sc.nextDouble();
//先判断取款金额是否大于余额;
if(money>loginaccount.getMoney())
System.out.println("您的取款金额大于余额,不能进行取款操作!");
else
{
//再判断取款金额是否大于取款最低金额和是否小于存款最大限额;
if(money<100||money>loginaccount.getLimit())
{
if(money<100)
System.out.println("您的取款金额低于100元!");
else
System.out.println("您的取款金额高于最大限额!");
System.out.println("您不能进行取款操作!");
}
else
{
loginaccount.setMoney(loginaccount.getMoney() - money);
System.out.println("您取款:" + money + "元,现在您的账户余额为:" + loginaccount.getMoney());
}
}
// 询问用户是否还要进行取款操作
System.out.println("是否还要进行取款操作?请输入“是”或者“否”:");
String choice = sc.next();
if (choice.equalsIgnoreCase("否")) {
break;
}
}
}
}
//存款:存款其实就是更新用户的余额信息;
private void inmoney() {
System.out.println("请输入您的存款金额:");
double money = sc.nextDouble();
loginaccount.setMoney(loginaccount.getMoney() + money);
System.out.println("恭喜您存款成功!您现在的余额为:" + loginaccount.getMoney());
}
//账户查询;
private void showloginAccount() {
System.out.println("以下是用户的全部信息:");
System.out.println("账户信息:" + loginaccount.getUsername());
System.out.println("卡号:" + loginaccount.getIdcard());
System.out.println("余额:" + loginaccount.getMoney());
System.out.println("限额:" + loginaccount.getLimit());
}
//用户登录;+ 登录后的操作页;
private void login() {
if(accounts.size() == 0) {
System.out.println("当前系统无任何账户,请开户后再来!");
return;
}
while (true) {
System.out.println("请您输入账户卡号:");
String idcard = sc.next();
Account account = judgebyID(idcard);
if(account == null)
{
System.out.println("您输入的卡号不存在!");
//如果账户不存在,继续登录操作;
}
else//卡号存在,可以登录;
{
while (true) {
System.out.println("请您输入登录密码:");
String password = sc.next();
if(password.equals(account.getPassword()))
{
System.out.println(account.getUsername() + "恭喜您,登录成功!" + "您的卡号是:" + account.getIdcard());
loginaccount = account; //把登录后的账户放在最上面的成员变量里,类内的方法都可以直接访问;
//接下来就是登录后的操作页;
showUsercommand();
return;
}
else
{
System.out.println("您输入的密码有误,请确认!");
}
}
}
}
}
private void createAccount() {
//开户:输入 户主信息后,将户主信息封装入一个账户对象中,系统自动为用户生成8位的卡号;
//要求:开户成功后,提示开户成功;
//所以首先需要创建一个账户信息:
Account account = new Account();
//提示输入各项信息;
System.out.println("请输入户主姓名:");
String name = sc.next();
account.setUsername(name);
System.out.println("请输入户主性别:");
while (true) {
char sex = sc.next().charAt(0);
if(sex == '男' || sex == '女')
{
account.setSex(sex);
break;
}
else
System.out.println("请您输入正确的性别:");
}
System.out.println("请输入每次取款限额:");
double limit = sc.nextDouble();
account.setLimit(limit);
while (true) {
System.out.println("请您设置密码:");
String password = sc.next();
System.out.println("请您再次确认密码:");
String okpassword = sc.next();
if(okpassword.equals(password))
{
account.setPassword(password);
break;
}
else
System.out.println("您两次输入的密码不正确,请确认呢!");
}
//记录卡号,卡号为8位的数字,且不能重复;这里我们用字符串记住;
String idcard = createIdcard();
account.setIdcard(idcard);//***第一次这里给错了值;用setpassword了;
//把账户存入账户集合;
accounts.add(account);
System.out.println(account.getUsername() + "恭喜您开户成功!您的卡号是:" + idcard);
}
private String createIdcard() {
while (true) {
String idcard = "";
Random r = new Random();
for (int i = 0; i < 8; i++) {
int data = r.nextInt(10);
idcard += data;
}
Account account = judgebyID(idcard);
if(account == null)
{
return idcard;
}
}
}
//用于判断卡号重复与否 <==> 用户是否存在;
private Account judgebyID(String idcard) {
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (idcard.equals(acc.getIdcard())) {
return acc; // 存在重复的卡号,返回存在的账户;
}
}
return null; // 卡号不重复,返回null;
}
}
public class Main {
//这是一个ATM系统,负责账户的开户,登录,登录后的:账户查询,存款,取款,转账,修改密码,销户,退出;
public static void main(String[] args) {
//定义一个ATM对象
ATM atm = new ATM();
//对ATM对象直接进行方法调用;启动ATM系统;
atm.start();
}
}
以上就是全部代码!注释已经写在旁边!