package hhh;
import com.sun.deploy.security.SelectableSecurityManager;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Strung {
public static void main(String[] args) {
ArrayList<Account> list = new ArrayList<>();
show(list);
System.out.println("欢迎下次使用");
}
public static void show(ArrayList<Account> list) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("操作");
System.out.println("1,登录");
System.out.println("2,开户");
System.out.println("3,退出");
int command = sc.nextInt();//输入所要操作选项
switch (command) {
case 1:
logn(list,sc);//进入登录页面
break;
case 2:
resign(list, sc);//进入开户页面
break;
case 3:
return;//退出ATM系统
default:
System.out.println("没有这个操作");
}
}
}
private static void logn(ArrayList<Account> list,Scanner sc) {
int i;
if(list.size()==0)//判断系统内是否有账号
{
System.out.println("没有账号");
return;
}
while (true) {
System.out.println("输入卡号");
String cord=sc.next();//输入所需卡号
Account s=check(cord,list);//检查是否有这个账号
if(s!=null)//有这个账号进入输入密码
{
for ( i = 1; i <=3; i++) {
System.out.println("输入密码");
String pass=sc.next();//输入密码
if (s.getPassword().equals(pass))//判断密码是否一样
{
System.out.println("登录成功"+s.getName()+"卡号是"+s.getCardid());
showcommand(sc,s,list);//进入操作页面
return;//结束操作后返回首页
}
else
{
System.out.println("密码错误,你还有"+(3-i)+"次机会");
if(i==3)
{
System.out.println("输入密码超过3次,已锁死");
return;
}
System.out.println("是否继续输入密码");
String s1=sc.next();
String s2="是";
if (s1.equals(s2))//是就继续输入密码,否则退到输入卡号界面
{
}
else
break;
}
}
}
else
{
System.out.println("卡号错误");
System.out.println("是否继续输入卡号");
String s1=sc.next();
String s2="是";
if (s1.equals(s2))//是就继续输入卡号,否则退到输入首页
{
}
else
return;
}
}
}
private static void showcommand(Scanner sc,Account s,ArrayList list) {
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,注销账户");
System.out.println("输入操作");
int command=sc.nextInt();
switch (command){
case 1:
showaccout(s);//展示
break;
case 2:
demoney(s,sc);//存款
break;
case 3:
drawmoney(s,sc);//取款
break;
case 4:
transmoney(s,sc,list);//转账
break;
case 5:
updateword(s,sc);//改密码
return;
case 6:
System.out.println("欢迎下次使用");//退出
return;
case 7:
list.remove(s);
System.out.println("销户成功");
return;
default:
System.out.println("输入命令有误");
}
}
}
private static void updateword(Account s, Scanner sc) {
System.out.println("正在改密码");
int i;
for ( i = 1; i <= 3; i++) {
System.out.println("输入正确密码");
String word=sc.next();
if (s.getPassword().equals(word))
{
while (true) {
System.out.println("输入新密码");
String newword=sc.next();
String oknewword=sc.next();
if (newword.equals(oknewword))
{s.setPassword(newword);
System.out.println("修改成功");
return;
}
else
System.out.println("密码不一致");
}
}
else
{
System.out.println("密码不正确,是否继续改密码,你还有"+(3-i)+"次机会");
if(i==3)
{
System.out.println("输入密码次数已经超过3次,以锁死");
return;
}
String s1=sc.next();
String s2="是";
if (s1.equals(s2))
{
}
else
return;
}
}
}
private static void transmoney(Account s, Scanner sc, ArrayList list) {
System.out.println("====转账====");
if (list.size()<2)
{
System.out.println("系统没有其他账号,无法转账");
return;
}
if (s.getMoney()==0)
{
System.out.println("没钱了");
return;
}
while (true) {
System.out.println("输入对方卡号");
String card=sc.next();
Account account=check(card,list);
if(account!=null)
{
if (account.getCardid().equals(s.getCardid()))
System.out.println("不可以给自己转账");
else
{
while (true) {
System.out.println("确认对方姓名");
String name="*"+account.getName().substring(1);
System.out.println("确认这个名字"+name);
String press=sc.next();
if (account.getName().startsWith(press))
{
System.out.println("输入转账金额");
double money=sc.nextDouble();
if (money>s.getMoney())
{
System.out.println("金额太多,最多可以转"+s.getMoney());
}
else
{
s.setMoney(s.getMoney()-money);
account.setMoney(account.getMoney()+money);
System.out.println("转账成功,给"+account.getName()+"转了"+money);
showaccout(s);
return;
}
}
else
System.out.println("姓氏有误");
}
}
}
else
{
System.out.println("没有这个卡号,是否继续转账");
String s1=sc.next();
String s2="是";
if (s1.equals(s2))
{
}
else
return;
}
}
}
private static void drawmoney(Account s, Scanner sc) {
System.out.println("----取款-----");
if(s.getMoney()>=100){
while (true) {
System.out.println("取款金额");
double money=sc.nextDouble();
if (money>s.getQoute())
System.out.println("取额太多,每次最多取"+s.getQoute());
else
{
if (s.getMoney()>money)
{s.setMoney(s.getMoney()-money);
System.out.println("取款完成");
return;
}else
{
System.out.println("金额不足");
}
}
}
}
else
System.out.println("金额太小");
}
private static void demoney(Account s,Scanner sc) {
System.out.println("-----存钱操作--------");
System.out.println("输入存款金额");
double money=sc.nextInt();
s.setMoney(s.getMoney()+money);
System.out.println("存款完成");
showaccout(s);
}
private static void showaccout(Account s) //展现当前账号信息
{
System.out.println("--------------当前账号信息----------------");
System.out.println("卡号"+s.getCardid());
System.out.println("姓名"+s.getName());
System.out.println("余额"+s.getMoney());
}
private static void resign(ArrayList<Account> list, Scanner sc) //开户
{
String pass = "";
String okpass = "";
System.out.println("名称");
String name = sc.next();
while (true) {
System.out.println("请输入六位数字密码");
System.out.println("密码");
pass = sc.next();
System.out.println("确认密码");
okpass = sc.next();
if (okpass.equals(pass)) {
System.out.println("密码一致");
break;
} else
System.out.println("密码不一致");
}
String cord=creat(list);
Account account=new Account(cord,0,5000,name,pass);
list.add(account);
System.out.println("开户成功,卡号是"+account.getCardid());
}
public static String creat(ArrayList<Account> list) //创建卡号
{
while (true) {
String cord = "";
Random r = new Random();
for (int i = 0; i < 8; i++) {
cord += r.nextInt(10);
}
Account s=check(cord,list);
if(s==null)
{
return cord;
}
}
}
public static Account check(String cord, ArrayList<Account> list)//检查是否有这个账号
{
for (int i = 0; i < list.size(); i++) {
Account s = list.get(i);
if (s.getCardid().equals(cord)) {
return s;
}
}
return null;
}
}
还要创建另一个类存放账号
package hhh;
public class Account {
private String cardid;//卡号
private double money;//余额
private double qoute;//取钱额度
private String name;//客户名字
private String password;//密码
public Account() {
}
public Account(String cardid, double money, double qoute, String name, String password) {
this.cardid = cardid;
this.money = money;
this.qoute = qoute;
this.name = name;
this.password = password;
}
public String getCardid() {
return cardid;
}
public void setCardid(String cardid) {
this.cardid = cardid;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getQoute() {
return qoute;
}
public void setQoute(double qoute) {
this.qoute = qoute;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
他们要在同一个包下