ATM项目
package com.atm;
public class Account {
private String cardId;
private String userName;
private String password;
private double money;
private double quotaMoney;
public String getCardId () {
return cardId;
}
public void setCardId (String cardId) {
this.cardId = cardId;
}
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 getMoney () {
return money;
}
public void setMoney (double money) {
this.money = money;
}
public double getQuotaMoney () {
return quotaMoney;
}
public void setQuotaMoney (double quotaMoney) {
this.quotaMoney = quotaMoney;
}
}
package com.atm;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main (String[] args) {
ArrayList<Account> accounts = new ArrayList<> ();
Scanner sc = new Scanner (System.in);
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:
login(accounts,sc);
break;
case 2:
register(accounts,sc);
break;
case 3:
System.out.println ("退出旺财ATM系统成功,欢迎下次光临!");
return;
default:
System.out.println ("请输出的操作命令不存在!");
}
}
}
private static void login (ArrayList<Account> accounts, Scanner sc) {
System.out.println ("=============旺财ATM系统登录操作==============");
if (accounts.size () == 0){
System.out.println ("对不起,当前系统中无任何账户,请先注册一个吧");
return;
}
while (true) {
System.out.println ("请您输入登录卡号:");
String cardId = sc.next ();
Account acc = getAccountByCardId (cardId, accounts);
if (acc != null){
while (true) {
System.out.println ("请您输入密码:");
String passWord = sc.next ();
if (acc.getPassword ().equals (passWord)){
System.out.println ("恭喜您,"+acc.getUserName ()+"先生/女士进入系统,您的卡号是:"+cardId);
showUserCommand(sc,acc,accounts);
return;
}else {
System.out.println ("对不起!您输入的密码有误");
}
}
}else {
System.out.println ("对不起!您输入的账户不存在");
}
}
}
private static void showUserCommand (Scanner sc,Account acc,ArrayList<Account> accounts) {
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:
showAccount(acc);
break;
case 2:
depositMoney(acc,sc);
break;
case 3:
withdrawal(acc,sc);
break;
case 4:
transferMoney(sc,acc,accounts);
break;
case 5:
updatePassword (sc,acc);
return;
case 6:
System.out.println ("退出操作页成功,欢迎下次光临");
return;
case 7:
if (cancellationOfAccount(acc,accounts,sc)){
return;
}else {
break;
}
default:
System.out.println ("您输入的操作命令有误!");
}
}
}
private static boolean cancellationOfAccount (Account acc, ArrayList<Account> accounts,Scanner sc) {
System.out.println ("您真的要销户吗?Y/N");
String rs = sc.next ();
switch (rs){
case "Y":
accounts.remove (acc);
System.out.println ("注销成功!");
return true;
case "N":
System.out.println ("你选择保留账户,还是很有前途的!");
return false;
default:
System.out.println ("您输入了不正确的操作符,销户失败");
}
return false;
}
private static void updatePassword (Scanner sc, Account acc) {
System.out.println ("=========修改密码操作页面========");
while (true) {
System.out.println ("请输入原密码:");
String password = sc.next ();
if (acc.getPassword ().equals (password)){
System.out.println ("请您输入新密码:");
String newPassword = sc.next ();
System.out.println ("请确认新密码:");
String newPassword2 = sc.next ();
if (newPassword2.equals (newPassword)){
System.out.println ("密码修改成功,请您重新登录!");
acc.setPassword (newPassword2);
return;
}else {
System.out.println ("您输入的密码不一致!");
}
}else {
System.out.println ("您输入的当前账户原密码不正确!");
}
}
}
private static void transferMoney (Scanner sc, Account acc, ArrayList<Account> accounts) {
System.out.println ("===============用户转账操作==============");
if (accounts.size ()<2){
System.out.println ("当前系统中,不足两个账户吧,请先去注册一个账户吧!");
return;
}
if (acc.getMoney () == 0){
System.out.println ("对不起!您自己都没钱,就别转了吧");
return;
}
while (true) {
System.out.println ("请您输入对方账户卡号:");
String cardId = sc.next ();
if (cardId.equals (acc.getCardId ())){
System.out.println ("对不起!您不可以给自己转账!");
continue;
}
Account account = getAccountByCardId (cardId, accounts);
if (account == null){
System.out.println ("对不起!您当前输入的账户不存在!");
}else {
String userName = account.getUserName ();
String tip = "*"+userName.substring (1);
System.out.println ("请您输入["+tip+"]的姓氏");
String preName = sc.next ();
if (userName.startsWith (preName)){
while (true) {
System.out.println ("请您输入转账金额:");
double money = sc.nextDouble ();
if (money>acc.getMoney ()){
System.out.println ("对不起!您的余额不足,您最多可以转账:"+acc.getMoney ()+"元");
}else {
if (money>acc.getQuotaMoney ()){
System.out.println ("对不起!您的转账金额,超过了单次限额!");
continue;
}else {
acc.setMoney (acc.getMoney ()-money);
account.setMoney (account.getMoney ()+money);
System.out.println ("转账成功!您的账户还剩余:"+acc.getMoney ());
return;
}
}
}
}else {
System.out.println ("对不起!您输入的信息有误!");
}
}
}
}
private static void withdrawal (Account acc, Scanner sc) {
System.out.println ("========欢迎您进入旺财银行用户取款页面========");
if (acc.getMoney ()<100){
System.out.println ("您的账户余额不足100元,请先完成存款操作");
return;
}
while (true) {
System.out.println ("请输入您的取款金额:");
double money = sc.nextDouble ();
if (money<acc.getMoney ()){
if (money> acc.getQuotaMoney ()){
System.out.println ("您当前的取款金额超过了单次限额");
}else {
acc.setMoney (acc.getMoney ()-money);
System.out.println ("恭喜您,取款成功");
showAccount (acc);
return;
}
}else {
System.out.println ("您的余额不足!");
}
}
}
private static void depositMoney (Account acc, Scanner sc) {
System.out.println ("===============用户存钱操作==============");
System.out.println ("请您输入存款金额:");
double money = sc.nextDouble ();
acc.setMoney (money+ acc.getMoney ());
System.out.println ("存款成功!");
showAccount (acc);
}
private static void showAccount (Account acc) {
System.out.println ("=============您当前的账户信息如下===============");
System.out.println ("卡号:"+ acc.getCardId ());
System.out.println ("户主:"+acc.getUserName ());
System.out.println ("余额:"+acc.getMoney ());
System.out.println ("单次取款限额:"+acc.getQuotaMoney ());
}
private static void register (ArrayList<Account> accounts,Scanner sc) {
System.out.println ("============系统开户操作================");
Account account = new Account ();
System.out.println ("请您输入用户名:");
String userName = sc.next ();
account.setUserName (userName);
while (true) {
System.out.println ("请您输入密码:");
String password = sc.next ();
System.out.println ("请确认您的密码:");
String okPassword = sc.next ();
if (okPassword.equals (password)){
account.setPassword (okPassword);
break;
}else {
System.out.println ("对不起!您输入的2次密码不一致,请重新输入!");
}
}
System.out.println ("请你输入账户单次取款限额:");
double quotaMoney = sc.nextDouble ();
account.setQuotaMoney (quotaMoney);
String cardId = getRandomCardId(accounts);
account.setCardId (cardId);
accounts.add (account);
System.out.println ("恭喜您,"+userName + "先生/女士开户成功,您的卡号为:"+cardId+",请您妥善保管!");
}
private static String getRandomCardId (ArrayList<Account> accounts) {
String cardId = "";
Random r = new Random ();
while (true) {
for (int i = 0; i < 8; i++) {
cardId += r.nextInt (10);
}
Account acc = getAccountByCardId (cardId, accounts);
if (acc == null){
return cardId;
}
}
}
private static Account getAccountByCardId(String cardId,ArrayList<Account> accounts){
for (int i = 0; i < accounts.size (); i++) {
Account acc = accounts.get (i);
if (acc.getCardId ().equals (cardId)){
return acc;
}
}
return null;
}
}