'use strict'
const readline = require("readline-sync")
let money = 1000
let ATM = function () {
console.log("输入账号")
let accout = readline.question("")
console.log("输入密码")
let passwordatm = parseInt(readline.question(""))
if (login(accout, passwordatm) == true) {
console.log("1查看余额2取钱3存钱")
let cho = readline.question("") - 0
switch (cho) {
case 1:
console.log("还有" + money)
break
case 2:
console.log("输入金额")
let getmoney = readline.question("") - 0
getmo(getmoney)
break
case 3:
console.log("输入金额")
let savemoney = readline.question("") - 0
save(savemoney)
break
}
} else {
console.log("输入错误")
}
}
function login(username, pasw) {
if ((username == "zhangsan") &&(pasw == 123)) {
return true
} else {
return false
}
}
let getmo = function (getmoney) {
if (money >= getmoney) {
money -= getmoney
console.log("取款" + getmoney, "还剩" + money)
return true
}
return false
}
let save = function (savemoney) {
money += savemoney
console.log("存款成功,余额:" + money)
}
ATM()
#include <iostream>#include <string>
using namespace std
class consumer
class ATM
// ATM取款机
{
public:
ATM(consumer&cn):cnsm(cn)
{
}
void welcome()// 登录界面
bool check_passwd(char n[],char pwd[])// 核对密码
void change_passwd() // 修改密码
void fetchmoney() // 取款
void information()// 查询信息
void exitATM()// 退出系统
void functionshow() // 功能界面
void lock() // 锁机
private:
int times // 记录密码输入次数
consumer & cnsm
}
class consumer // 用户
{
public:
friend class ATM
consumer(char Name[],char Num[],float Money,char Password[])
protected:
char* get_name()// 取得姓名
char* get_num()// 取得卡号
char* get_passwd() // 取得密码
float get_money() // 取得余额
void set_passwd(char pwd[])// 设置密码
void set_money(float m)// 取钱
private:
char passwd[8] // 用户密码
char name[20] // 用户姓名
char num[20]
float money
}
//
// ************************************
// * *
// * consumer类的成员函数 *
// * *
// ************************************
consumer::consumer(char Name[],
char Num[],float Money,char Password[])
{
strcpy(name,Name)
strcpy(num,Num)
money=Money
strcpy(passwd,Password)
}
float consumer::get_money()
{
return money
}
char* consumer::get_name()
{
return name
}
char* consumer::get_num()
{
return num
}
char* consumer::get_passwd()
{
return passwd
}
void consumer::set_money(float m)
{
money-=m
}
void consumer::set_passwd(char pwd[])
{
strcpy(passwd,pwd)
}
//
// ************************************
// * *
// * ATM类的成员函数 *
// * *
// ************************************
void ATM::welcome()
{
times=0
cout<<"$ 欢迎使用若雪银行ATM自动取款机!~! "<<endl
char pwd[8],num[20],ch
int i=0
do
{
i=0
cout<<endl<<"请输入卡号:"
do
{
cin.get(ch)
num[i++]=ch
}while(ch!='\n')
num[i-1]='\0'
i=0
cout<<"请输入密码:"
do
{
cin.get(ch)
pwd[i++]=ch
}while(ch!='\n')
pwd[i-1]='\0'
if(!check_passwd(num,pwd))
{
cout<<"你输入的卡号或密码有误,请重新输入"<<endl
times++
}
else
{
functionshow()
}
}while(times<3)
lock()
}
bool ATM::check_passwd(char num[],
char pwd[])
{
if(strcmp(num,cnsm.get_num())==0&&strcmp
(pwd,cnsm.get_passwd())==0)
return true
else
return false
}
void ATM::functionshow()
{
int n
do
{
cout<<endl<<"请你输入相应的操作序号进行操作:"<<endl
cout<<"1) 修改密码 "<<endl
<<"2) 取款 "<<endl
<<"3) 查询余额 "<<endl
<<"4) 退出系统 "<<endl
cout<<"$ >\\"
cin>>n
while(n<1||n>4)
{
cout<<"请输入正确的操作序号!"<<endl
cout<<"$ >\\"
cin>>n
}
switch(n)
{
case 1: change_passwd()
break
case 2: fetchmoney()
break
case 3: information()
break
case 4: exitATM()
break
}
}while(true)
}
void ATM::change_passwd()
{
char pwd[8],repwd[8]
times=0
do
{
cout<<endl<<"请输入旧密码:"
cin>>pwd
if(!check_passwd(cnsm.get_num(),pwd))
times++
else
break
}while(times<3)
if(times==3)
lock()
int t=0
do
{
cout<<"请输入新密码:"
cin>>pwd
cout<<"请再输入一次新密码:"
cin>>repwd
if((t=strcmp(pwd,repwd))!=0)
cout<<"你输入的两次密码不一样,请重新输入!"<<endl
}while(t!=0)
cnsm.set_passwd(pwd)
cout<<"密码修改成功,请牢记!"<<endl
}
void ATM::fetchmoney()
{
float m
char ch
do
{
cout<<endl<<"你要取多少钱:"
<<"\n$>\\"<<endl
cin>>m
while(m<=0)
{
cout<<"请输入正确的数字!"<<endl
cout<<"$ >\\ "
cin>>m
}
if(cnsm.get_money()-m<0)
{
cout<<"对不起,你的余额不足!" <<endl
}
else
{
cout<<endl<<"操作成功,请收好钱!" <<endl
cnsm.set_money(m)
}
cout<<"是否要继续该项操作:(Y/N) "<<endl
cout<<"$ >\\ "
cin>>ch
while(ch!='n'&&ch!='N'&&ch!='Y'&&ch!='y')
{
cout<<"$ >\\"
cin>>ch
}
}while(ch=='y'||ch=='Y')
}
void ATM::information()
{
cout<<"**********************************"<<endl
cout<<"*"<<endl
cout<<"* 用户姓名:"<<cnsm.get_name()<<endl
cout<<"* 卡号:"<<cnsm.get_num()<<endl
cout<<"* 余额: "<<cnsm.get_money()<<endl
cout<<"**********************************"<<endl
}
void ATM::lock()
{
cout<<endl<<"对不起,由于你的操作有误, 你的卡已经被没收! "<<endl
exit(1)
}
void ATM::exitATM()
{
cout<<endl<<"感谢你对本银行的支持, 欢迎下次光临!"<<endl
cout<<"请取卡……"<<endl
exit(0)
}
// ************************************
// * *
// * ATM.cpp *
// * *
// ************************************
int main()
{
consumer c1("jim","12345",5200.3f,"123")
// 先构造一个用户
ATM atm(c1)
atm.welcome()
return 0
}
import java.io.IOException
/**
* ATM机类
*
* 查看余额
*
* 取款
*
* 存款
*
* 退出系统
*
*
*
*/
public class ATM {
static double yue = 1200.00
public static void main(String[] arg) {
ATM localTest1 = new ATM()
localTest1.ATM_Operate()
}
/**
* ATM机的操作
*/
private void ATM_Operate() {
System.out.println("欢迎使用中国工商银行ATM取款机")
System.out.println("1、查看余额 2、取款")
System.out.println("3、存款0、退出")
System.out.print("请输入您需要的服务:")
byte[] buffer = new byte[512]
try {
int count = System.in.read(buffer)// 返回实际读取的字节数
System.out.print("您输入的是:")
for (int i = 0i <counti++) {
System.out.print("" + (char) buffer[i])
}
if ((char) buffer[0] == '1') {
// 查看余额
System.out.println("您的余额是:¥" + yue + "元")
System.out.println()
ATM_Operate()
} else if ((char) buffer[0] == '2') {
// 取款
withdrawal()
System.out.println()
ATM_Operate()
} else if ((char) buffer[0] == '3') {
// 存款
deposit()
System.out.println()
ATM_Operate()
} else if ((char) buffer[0] == '0') {
// 退出
System.out.println("您已经成功退出系统,谢谢你的使用")
System.exit(0)
} else {
System.out.println("输入不合法,请重新输入")
System.out.println()
ATM_Operate()
}
} catch (IOException e) {
e.printStackTrace()
}
}
/**
* 取款
*
* @throws IOException
*/
private void withdrawal() throws IOException {
byte[] buffer = new byte[512]
System.out.print("请输入您要取出的金额:¥")
int count2 = System.in.read(buffer)// 返回实际读取的字节数
System.out.print("您输入的金额是:")
for (int i = 0i <count2 - 1i++) {
System.out.print("" + (char) buffer[i])
}
System.out.println()
// 字符0 ~ 9对应ASCII值48 ~ 57
boolean flag = false
for (int i = 0i <count2 - 1i++) {
if ((char) buffer[i] >47 &&(char) buffer[i] <58) {
if (i == count2 - 2) {
flag = true
}
} else {
// 输入的字符不是数值
System.out.println("输入不合法,请重新输入")
withdrawal()
break
}
}
System.out.println()
if (flag) {
System.out.print("您已成功取出¥:")
String num = ""
for (int i = 0i <count2 - 1i++) {
System.out.print("" + (char) buffer[i])
num += (char) buffer[i]
}
yue -= Double.valueOf(num)
System.out.print(",现在余额¥:" + yue)
}
}
/**
* 存款
*
* @throws IOException
*/
private void deposit() throws IOException {
byte[] buffer = new byte[512]
System.out.print("请输入您要存入的金额:¥")
int count2 = System.in.read(buffer)// 返回实际读取的字节数
System.out.print("您输入的金额是:")
for (int i = 0i <count2 - 1i++) {
System.out.print("" + (char) buffer[i])
}
System.out.println()
// 字符0 ~ 9对应ASCII值48 ~ 57
boolean flag = false
for (int i = 0i <count2 - 1i++) {
if ((char) buffer[i] >47 &&(char) buffer[i] <58) {
if (i == count2 - 2) {
flag = true
}
} else {
// 输入的字符不是数值
System.out.println("输入不合法,请重新输入")
withdrawal()
break
}
}
System.out.println()
if (flag) {
System.out.print("您已成功存入¥:")
String num = ""
for (int i = 0i <count2 - 1i++) {
System.out.print("" + (char) buffer[i])
num += (char) buffer[i]
}
yue += Double.valueOf(num)
System.out.print(",现在余额¥:" + yue)
}
}
}