使用Java语言中(类和对象)编写: ATM自动存取款机的程序?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Java语言中(类和对象)编写: ATM自动存取款机的程序?相关的知识,希望对你有一定的参考价值。
【可以实现:密码(判断3次退出)存取款(100或100的倍数),余额查询,系统退出,】要有菜单 System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆(ABC)中国农业银行ATM自动存取款机☆☆☆☆☆☆☆☆☆☆☆☆☆"); System.out.println("\n\t\t\t\t1.存款业务"); System.out.println("\n\t\t\t\t2.取款业务"); System.out.println("\n\t\t\t\t3.查询余额"); System.out.println("\n\t\t\t\t4.退出ATM系统"); System.out.println("\n☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆中国农业银行欢迎您的使用☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
很简单的例子,我把代码贴出来吧import java.util.Scanner;
public class ATM
/**
* @param args
*/
public static void main(String[] args)
Scanner in = null;
int result;
double drawMoney;
double depositMoney;
Account account = new Account();
while (true)
System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆(ABC)中国农业银行ATM自动存取款机☆☆☆☆☆☆☆☆☆☆☆☆☆");
System.out.println("\n\t\t\t\t1.存款业务");
System.out.println("\n\t\t\t\t2.取款业务");
System.out.println("\n\t\t\t\t3.查询余额");
System.out.println("\n\t\t\t\t4.退出ATM系统");
System.out.println("\n☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆中国农业银行欢迎您的使用☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
in = new Scanner(System.in);
switch (in.nextInt())
case 1:
System.out.println("请输入您的存款数额!");
depositMoney = in.nextDouble();
if(depositMoney < 0)
System.out.println("您的输入无效,请重新输入!");
result = account.deposit(depositMoney);
if(result == 0)
System.out.println("存款业务完成,是否显示余额?Y/N");
if("Y".equalsIgnoreCase(in.next()))
System.out.println("您当前的余额为:"+account.checkCurrent());
else
break;
else
System.out.println("无法完成交易!");
break;
break;
case 2:
System.out.println("请输入您的取款数额!");
drawMoney = in.nextDouble();
if (drawMoney < 0)
System.out.println("您的输入无效,请重新输入!");
result = account.withDraw(drawMoney);
if (result == 0)
System.out.println("存款业务完成,是否显示余额?Y/N");
if("Y".equalsIgnoreCase(in.next()))
System.out.println("您当前的余额为:"+account.checkCurrent());
else
break;
else
System.out.println("无法完成交易!");
break;
case 3:
System.out.println("您当前的余额为:"+account.checkCurrent());
break;
case 4:
break;
default:
break;
public class Account
private double currentMoney;//当前余额
public double getCurrentMoney()
return currentMoney;
public void setCurrentMoney(double currentMoney)
if (currentMoney > 0)
this.currentMoney = currentMoney;
/**
* 取款业务
* @param drawMoney
* @return 0代表成功,1代表失败
*/
public int withDraw(double drawMoney)
if (currentMoney > 0 && drawMoney <= currentMoney)
currentMoney -= drawMoney;
else
System.out.println("您的余额已不足!");
return 1;
return 0;
/**
* 存款业务
* @param depositMoney
* @return 0代表成功,1代表失败
*/
public int deposit(double depositMoney)
if (depositMoney > 0)
currentMoney += depositMoney;
return 0;
else
System.out.println("您提交的存款为负数,无法完成存款交易");
return -1;
/**
* 查询余额业务
* @return
*/
public double checkCurrent()
return currentMoney;
参考技术A 可以去专业论坛去 参考技术B 对JAVA感兴趣可以看下疯狂JAVA的视频 这系列视频很多好项目的 你可以去了解下。。 参考技术C a
类和对象,类定义了对象的特征和行为。属性,方法。
1、编写一个ATM机类,有属性:所属银行、显示余额、,有方法:取款、存款、查询余额
/*
* 取款机实体类
* ATM
*/
public class ATM {
String affiliatedBank;// 声明所属银行
String keyboard; // 键盘
Double displayBalance; // 显示余额
String horn; // 喇叭
// 无参构造函数
public ATM() {
}
/*
* 封装属性,getXXX()取值,setXXXX()赋值
*/
public String getAffiliatedBank() {
return affiliatedBank;
}
public void setAffiliatedBank(String affiliatedBank) {
this.affiliatedBank = affiliatedBank;
}
public String getKeyboard() {
return keyboard;
}
public void setKeyboard(String keyboard) {
this.keyboard = keyboard;
}
public Double getDisplayBalance() {
return displayBalance;
}
public void setDisplayBalance(Double displayBalance) {
this.displayBalance = displayBalance;
}
public String getHorn() {
return horn;
}
public void setHorn(String horn) {
this.horn = horn;
}
// ATM取款方法
public void withdrawals(Double sum) {
Double withdrawalBalance; // 账户余额
System.out.println("欢迎使用" + affiliatedBank + "ATM01");
withdrawalBalance = displayBalance - sum;
System.out.println("ATM账户余额" + withdrawalBalance + "ATM011");
}
// ATM存款方法
public void deposits(Double sum1) {
Double withdrawalBalance; // 账户余额
System.out.println("欢迎使用" + affiliatedBank + "ATM02");
withdrawalBalance = displayBalance + sum1;
System.out.println("ATM账户余额" + withdrawalBalance + "ATM012");
}
// 查询存款后ATN余额方法
public void inquiries3(Double sum1) {
Double c = displayBalance + sum1; // ATM机余额
displayBalance = c;
System.out.println("欢迎使用" + affiliatedBank + "ATM03");
System.out.println("ATM账户余额" + displayBalance + sum1 + "ATM013");
}
// 取款后查询ATM余额方法
public void inquiries(Double sum1) {
Double c = displayBalance - sum1;
displayBalance = c;
System.out.println("欢迎使用" + affiliatedBank + "ATM03");
System.out.println("ATM账户余额" + displayBalance + "ATM013");
}
// 查询ATM余额方法
public void inquiries() {
System.out.println("欢迎使用" + affiliatedBank + "ATM03");
System.out.println("ATM账户余额" + displayBalance + "ATM013");
}
}
/*
* 储户实体类
*
*/
public class Depositor {
String accountNumber;// 账号
String affiliatedBank;// 所属银行
Double depositAmount;// 账户余额
ATM atm; // ATM 对象
Bank bank;// Bank 对象
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAffiliatedBank() {
return affiliatedBank;
}
public void setAffiliatedBank(String affiliatedBank) {
this.affiliatedBank = affiliatedBank;
}
public Double getDepositAmount() {
return depositAmount;
}
public void setDepositAmount(Double depositAmount) {
this.depositAmount = depositAmount;
}
// 储户取款方法
public void withdrawals2(Double sum) {
Double withdrawalBalance; //账户余额
System.out.println("欢迎使用"+affiliatedBank+"ATM0");
withdrawalBalance=depositAmount-sum;
System.out.println("储户账户余额"+withdrawalBalance+"ATM01");
}
// 储户取存款方法
public void deposits2(Double sum1) {
Double withdrawalBalance; //账户余额
System.out.println("欢迎使用"+affiliatedBank+"ATM02");
withdrawalBalance=depositAmount+sum1;
System.out.println("储户账户余额"+withdrawalBalance+"ATM12");
}
// 取款查询余额方法
public void inquiries22(Double sum) {
double b=depositAmount-sum;
depositAmount=b;
System.out.println("欢迎使用"+affiliatedBank+"ATM031");
System.out.println("储户账户余额"+depositAmount+"ATM0131");
}
// 储户存款增加查询余额方法
public void inquiries2(Double sum) {
//atm 余额增加
//atm.inquiries3(sum);
double b=depositAmount+sum;
depositAmount=b;
System.out.println("欢迎使用"+affiliatedBank+"ATM031");
System.out.println("储户账户余额"+depositAmount+"ATM0131");
}
// 查询余额方法
public void inquiries2() {
System.out.println("欢迎使用"+affiliatedBank+"ATM03");
System.out.println("储户账户余额"+depositAmount+"ATM013");
}
}
/*
* 银行实体类
*
*/
public class Bank {
String bankName;// 银行名称
String address;// 地址
Double depositAmount;// 存款金额
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Double getDepositAmount() {
return depositAmount;
}
public void setDepositAmount(Double depositAmount) {
this.depositAmount = depositAmount;
}
// 银行取款方法
public void withdrawals1(Double sum) {
Double withdrawalBalance; // 账户余额
System.out.println("欢迎使用" + bankName + "柜台0");
withdrawalBalance = depositAmount - sum;
System.out.println("银行账户余额" + withdrawalBalance + "柜台00");
}
// 银行存款方法
public void deposits1(Double sum1) {
Double withdrawalBalance; // 账户余额
System.out.println("欢迎使用" + bankName + "柜台1");
withdrawalBalance = depositAmount + sum1;
System.out.println("银行账户余额" + withdrawalBalance + "柜台11");
}
// 查询银行余额方法
public void inquiries11(Double sum) {
Double a=depositAmount+sum;
depositAmount=a;
System.out.println("欢迎使用" + bankName + "柜台2");
System.out.println("银行账户余额" + depositAmount + "柜台22");
}
// 查询银行余额方法
public void inquiries1(Double sum) {
Double a=depositAmount-sum;
depositAmount=a;
System.out.println("欢迎使用" + bankName + "柜台2");
System.out.println("银行账户余额" + depositAmount + "柜台22");
}
// 查询银行余额方法不带参
public void inquiries1() {
System.out.println("欢迎使用" + bankName + "柜台21");
System.out.println("银行账户余额" + depositAmount + "柜台221");
}
}
以上是关于使用Java语言中(类和对象)编写: ATM自动存取款机的程序?的主要内容,如果未能解决你的问题,请参考以下文章