ATM界面
import java.util.Scanner;
public class AccountView {
private AccountManger am=new AccountManger();
public void registerView(){
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("请输入用户名");
String accountName=sc.next();
if(am.isExitAccountName(accountName)){
System.out.println("您输入的用户名已经存在,请重新输入");
continue;
}
System.out.println("请输入密码");
int accountPwd=sc.nextInt();
System.out.println("请确认密码");
int accountPwd1=sc.nextInt();
if(accountPwd!=accountPwd1){
System.out.println("请重新输入");
continue;
}
//将用户名和密码封装为账户对象
Account account=new Account(accountName,accountPwd);
am.addAccount(account);
System.out.println("注册成功");
break;
}
}
public void loginView(){
Scanner sc=new Scanner(System.in);
int count=0;
while(true){
System.out.println("请输入用户名");
String accountName=sc.next();
System.out.println("请输入密码");
int accountPwd=sc.nextInt();
Account account=new Account(accountName, accountPwd);
if(am.isExitAccout(account)){
System.out.println("登录成功");
break;
}
else{
//限制密码错误的次数
count++;
if(count>3){
System.out.println("您的账户已经冻结。。。。,系统即将退出");
System.exit(0);//退出系统
}
else{
System.out.println("n您输入的用户名或者密码不正确,请重新输入");
continue;
}
}
}
}
public void transferView(){
Scanner sc=new Scanner(System.in);
String ans="y";
while(ans.equals("y")){
System.out.println("请输入对方账户");
String accountName=sc.next();
if(!am.isExitAccountName(accountName)){
System.out.println("您输入的账户不存在,请重新输入");
continue;
}
System.out.println("请输入转账金额");
double money=sc.nextDouble();
am.transfer(accountName, money);
System.out.println("是否要继续转账");
ans=sc.next();
}
}
//展示自己账户的余额
public void showMoney(){
double money=am.getLoginUserBalance();
System.out.println("您的账户余额为:"+money);
}
//测试,展示所有账户信息
public void showAll(){
Account accounts[]=am.getAccounts();
for (int i = 0; i < am.getCount(); i++) {
System.out.println(accounts[i]);
}
}
//存钱界面,自己完成
public void saveMoney(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入存钱金额");
double money=sc.nextDouble();
am.transfer(money);
}
//取钱界面,自己完成
public void giveMoney(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入取款金额");
double money=sc.nextDouble();
am.transfer1(money);
}
}
ATM管理者操作界面
public class AccountManger {
private Account accounts[];//存放很多个账户信息
private int size=5;//最大容量
private int count;//实际容量
private String loginUser;
public AccountManger() {
// TODO Auto-generated constructor stub
accounts=new Account[size];
accounts[0]=new Account("admin",123456);
accounts[0].changeMoney(100);
accounts[1]=new Account("gly",123456);
accounts[1].changeMoney(400);
count=2;
}
public AccountManger(int size) {
// TODO Auto-generated constructor stub
this.size=size;
accounts=new Account[size];
accounts[0]=new Account("admin",123456);
accounts[0].changeMoney(100);
accounts[1]=new Account("gly",123456);
accounts[1].changeMoney(400);
count=2;
}
//增加账户
public void addAccount(Account account){
accounts[count]=account;
count++;
}
//验证账户对象是否存在
public boolean isExitAccout(Account account){
for (int i = 0; i < count; i++) {
if(accounts[i].getAccountName().equals(account.getAccountName())&&accounts[i].getAccountPsd()==(account.getAccountPsd())){
//将登陆成功的用户的用户名存放起来
loginUser=account.getAccountName();
return true;
}
}
return false;
}
//验证用户名是否存在
public boolean isExitAccountName(String accountName){
for (int i = 0; i < count; i++) {
if(accounts[i].getAccountName().equals(accountName)){
//将登陆成功的用户的用户名存放起来
return true;
}
}
return false;
}
//转账
public void transfer(String otherAccountName,double vb){
//遍历找到两个账户
for (int i = 0; i < count; i++) {
//找登录的账户
if(accounts[i].getAccountName().equals(loginUser)){
accounts[i].changeMoney(-vb);
}
//找另外一个账户
if(accounts[i].getAccountName().equals(otherAccountName)){
accounts[i].changeMoney(vb);
}
}
}
public double getLoginUserBalance(){
for (int i = 0; i < count; i++) {
//找登录的账户
if(accounts[i].getAccountName().equals(loginUser)){
return accounts[i].getBalance();
}
}
return 0;
}
//存钱
public void transfer(double vb){
//遍历找到两个账户
for (int i = 0; i < count; i++) {
//找登录的账户
if(accounts[i].getAccountName().equals(loginUser)){
accounts[i].changeMoney(vb);
}
}
}
//取钱
public void transfer1(double vb){
//遍历找到两个账户
for (int i = 0; i < count; i++) {
//找登录的账户
if(accounts[i].getAccountName().equals(loginUser)){
accounts[i].changeMoney(-vb);
}
}
}
//测试过程中,获得所有的账户
public Account[] getAccounts() {
return accounts;
}
public int getCount() {
return count;
}
public int getSize() {
return size;
}
}
账户对象
public class Account {
private String accountName;
private int accountPsd;
private double accountBalance;
public Account(String accountName, int accountPsd) {
super();
this.accountName = accountName;
this.accountPsd = accountPsd;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountName() {
return accountName;
}
public void setAccountPsd(int accountPsd) {
this.accountPsd = accountPsd;
}
public int getAccountPsd() {
return accountPsd;
}
public double getBalance() {
return accountBalance;
}
//存取钱操作
public void changeMoney(double _vb){
accountBalance=accountBalance+_vb;
}
@Override
public String toString() {
return "Account [accountName=" + accountName + ", accountPsd=" + accountPsd + ", accountBalance="
+ accountBalance + "]";
}
}
测试ATM
import java.util.Scanner;
public class Main {
private AccountView accountView=new AccountView();
public void mainMenu(){
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("1.注册");
System.out.println("2.登录");
System.out.println("请选择");
int se=sc.nextInt();
switch (se) {
case 1:
accountView.registerView();
break;
case 2:
accountView.loginView();
subMenu();
break;
default:
break;
}
}
}
public void subMenu(){
Scanner sc=new Scanner(System.in);
while(true){
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("请选择");
int se=sc.nextInt();
switch (se) {
case 1:
accountView.transferView();
break;
case 2:
accountView.showMoney();
break;
case 3:
accountView.saveMoney();
break;
case 4:
accountView.giveMoney();
break;
case 5:
accountView.showAll();
break;
default:
mainMenu();
break;
}
}
}
public static void main(String[] args) {
Main m=new Main();
m.mainMenu();//启动主菜单
}
}
首个测试对象