练习题:试使用C#编程实现银行、ATM等功能
练习题:试使用编程实现银行、ATM等功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
//创建账户wangcehnjun
Account wangchenjun = new Account("wangchenjun", "");
//创建账户niejiong
Account niejiong = new Account("niejiong", ""); //创建ATM机Transfer1
ATM Transfer1 =new ATM();
//用户wangchenjun存入余额100万元
wangchenjun.toDeposit(); Console.WriteLine("wangchenjun用户的余额为: {0}元",wangchenjun.getBalance()); //使用ATM机Transfer1,从用户wangchenjun向用户niejiong转账500元
if (Transfer1.toTransfer(wangchenjun, niejiong, ))
Console.WriteLine("转账已完成!");
else Console.WriteLine("转账未完成!"); //用户wangchenjun取出50元现金
wangchenjun.toTakeOut(); Console.WriteLine("wangchenjun用户的余额为:{0}元",wangchenjun.getBalance());
Console.WriteLine("niejiong用户的余额为:{0}元", niejiong.getBalance()); Console.ReadKey(); }
}
class Account
{
//用户名
private string userName = ""; //用户密码
private string userPassword = ""; //用户余额(balance)
private int userBalance = ; //创建账号,构造函数,输入姓名和密码
public Account(string name, string password)
{
userName = name;
userPassword = password;
} //获取该账户的用户名
public string getUserName()
{
return userName;
} //获取账户余额
public int getBalance()
{ return userBalance; } //存钱
public bool toDeposit(int numOfDeposit)
{
if (numOfDeposit >= ) //存储金额为正数
{
userBalance = userBalance + numOfDeposit;
return true;
}
else
{
return false;
}
} //取钱
public bool toTakeOut(int numOfTakeOut)
{
if ((numOfTakeOut >= ) && (numOfTakeOut <=userBalance)) //要保证取钱数为正数,且要小于余额
{
userBalance = userBalance - numOfTakeOut;
return true;
}
else return false;
} } class ATM
{
//ATM主要用于转账,该类只包含转账这一种方法
public bool toTransfer(Account account1,Account account2,int amount)
{
bool isTakeOutOK = account1.toTakeOut(amount);
if (isTakeOutOK)
{
Console.Write("账户1({0})转出{1}元成功!",account1.getUserName(), amount);
bool isDepositOK = account2.toDeposit(amount);
if (isDepositOK)
{
Console.Write("账户2({0})转入{1}元成功!", account2.getUserName(),amount);
return true;
}
else
{
account1.toDeposit(amount); //将扣的钱退回到account1
Console.Write("转账失败!");
return false;
}
}
else
{
Console.Write("转账失败!");
return false;
}
}
}
}
运行结果:

练习题:试使用C#编程实现银行、ATM等功能的更多相关文章
- 银行ATM机工作流程模拟编程
[编程内容] 编程,模拟一个ATM(Automatic Teller Machine,自动取款机)的工作流程.依据帐户信息:姓名.帐号.密码.余额,完成ATM机功能:登录.显示余额.取款.修改密码. ...
- MySQL银行ATM存取款机系统(需求分析)
银行ATM需求文档 一.E-R图形文 二.开发步骤 1.明确需求--数据库设计--编码实现功能--测试 2.绘制e-r图--绘制数据库模型图--使用三大方式规范数据库结构 三.开发思路 1. 模型图综 ...
- 实验01——java模拟银行ATM系统
用java写的一个模拟银行系统,比较初级. ATM.java package cn.tedu.yinhang; import java.util.Scanner; /** * @author 赵瑞鑫 ...
- 银行ATM存取款系统(C语言实现)
这里使用的运行工具是DEV C++.老铁们一定要看仔细了.是DEV C++ 仅供借鉴:这个是大一时期写的.大四的时候整理了一下(本人C语言学的也不太好).肯定很多不足和存在漏洞的地方.仅供借鉴.仅供借 ...
- jsp+servlet+mysql 实现简单的银行登录转账功能
jsp+servlet+mysql 实现简单的银行登录转账功能 [前期的准备] html(登录界面),servlet(处理业务逻辑),jsp(主要实现界面),mysql(实现与数据库的简单的交互)先从 ...
- NODE编程(一)--Node功能的组织和重用
Node开发面对的两个问题: 1.如何组织代码 2.如何进行异步编程. 一.Node功能的组织和重用 Node模块允许你从被引入文件中选择要暴露给程序的函数和变量.如果模块返回的函数或变量不止一个,那 ...
- 银行ATM机工作流程模拟编程(代码)
#include<stdio.h>#include<stdlib.h>#include <conio.h>#include <string.h> voi ...
- 模拟银行ATM系统(基础版)
Account类 package ATM; public class Account {//定义Account类 private String accountID;//用于存储学生的用户账号(由八位数 ...
- 数据库期末作业之银行ATM存取款机系统
--一.建库.建表.建约束 --1.使用SQL创建表 --客户信息表userinfo --字段名称 说明 备注 --customerID 顾客编号 自动编号(标识列),从1开始,主键 --用序列seq ...
随机推荐
- java按照指定格式输出系统时间使用SimpleDateFormat方法
public class TimeThree { public static void main(String[] args) { SimpleDateFormat d = new SimpleDat ...
- springboot秒杀课程学习整理1-6
1)活动模型设计 配饰秒杀的模型(promoModel)id promoName startDate(建议使用joda-time) endDate itemId promoItemPrice 数据库( ...
- Kali Linux Vmware虚拟机(新手)安装
准备工作: 1.安装VMware workstation 软件 2.下载好kali linux 的ios系统文件 3.打开电脑的虚拟化支持(Intel VT-x/EPT或AMD-V/RVI(V)) 虚 ...
- 8款非常不错的.Net反编译利器
本人搜集了下8款非常不错的.Net反编译利器: 1.Reflector Reflector是最为流行的.Net反编译工具.Reflector是由微软员工Lutz Roeder编写的免费程序.Refle ...
- Ehlib 学习
似乎是为了垂直滚动条 SumList.Active := True; SumList.VirtualRecords := True; TDBGridEh 设计时 It is useful to use ...
- Django知识总结(三)
拾伍 ● Ajax技术 一 ● Ajax定义 Ajax: 异步的 JavaScript 和 XML (Asynchronous+Javascript+XML) 通过Ajax, 我们可以在不重新加载整个 ...
- SQL-59 按照salary的累计和running_total,其中running_total为前两个员工的salary累计和,其他以此类推。
题目描述 按照salary的累计和running_total,其中running_total为前两个员工的salary累计和,其他以此类推. 具体结果如下Demo展示..CREATE TABLE `s ...
- vs问题--------------标记为系统必备组建...
问题:标记为系统必备组建 要将程序集“D:\project\DMS\DMSGaeaService\TmsApplication\bin\Debug\Jns.Gaea.dll”标记为系统必备组件,必须对 ...
- linux部署小结
一.连接外网1.配置网卡 vi /etc/sysconfig/network-scripts/ifcfg-eth0 BOOTPROTO=static IPADDR= PREFIX= GATEWAY= ...
- Python基础(切片,list循环,元组)
list和字符串循环: 切片:list取值的一种方式,同样适用于字符串(因为字符串也有下标) 不可变类型:元组,字符串