Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
给你学习路线:html-css-js-jq-javase-数据库-jsp-servlet-Struts2-hibernate-mybatis-spring4-springmvc-ssh-ssm
小编推荐一个学Java的学习裙【 六五零,五五四,六零七 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!
这是自己写的一个特别简单的银行ATM机系统,逻辑功能都一般,只是随便写了实现几个小功能,还有很多需要完善优化的地方。这个小程序涉及的主要就是面向对象分析。在主函数部分,写了3种实现,读者都可以尝试看看,说实话,这种代码就是垃圾,因为阅读性实在太差,读者有兴趣可以看看研究研究。
//源代码
package oo.day06;
import java.util.Scanner;
//import oo.day05.CCB;
public class UnionPayTest {
public static void main(String[] args) {
ICBCImpl icbc = new ICBCImpl(2000.0,"123456");
Scanner scan = new Scanner(System.in);
System.out.println("输入数字选择银行:"+" "+"1-ICBC 2-ABC 3-CCB");
int num1 = scan.nextInt();
switch(num1){
case 1:
//验证密码
System.out.println("输入密码:");
String password = scan.next();
if(icbc.checkPwd(password)){
//System.out.println("密码正确!");
System.out.println("密码正确!"+" "+"请输入数字选择功能:1-查询 2-取款 3-网上支付");
//开始输入数字选择功能
int num = scan.nextInt();
switch(num){
case 1:
//查询余额
System.out.println("余额:"+icbc.getBalance());
break;
case 2:
//取款
System.out.println("请输入取款金额:");
double number1 = scan.nextDouble();
if(icbc.drawMoney(number1)){
System.out.println("取款成功,余额为:"+icbc.getBalance());
}else{
System.out.println("取款失败!");
}
break;
case 3:
//网上支付
System.out.println("请输入支付金额:");
double number2 = scan.nextDouble();
if(icbc.payOnline(number2)){
System.out.println("支付成功,余额为:"+icbc.getBalance());
}else{
System.out.println("支付失败!");
}
break;
default :
System.out.println("输入错误!");
}
}else{
System.out.println("密码错误!");
}
break;
case 2:
break;
case 3:
break;
default :
System.out.println("输入错误!");
}
/*
//验证密码
System.out.println("输入密码:");
String password = scan.next();
if(icbc.checkPwd(password)){
//System.out.println("密码正确!");
System.out.println("密码正确!"+" "+"请输入数字选择功能:1-查询 2-取款 3-网上支付");
//开始输入数字选择功能
int num = scan.nextInt();
switch(num){
case 1:
//查询余额
System.out.println("余额:"+icbc.getBalance());
break;
case 2:
//取款
System.out.println("请输入取款金额:");
double number1 = scan.nextDouble();
if(icbc.drawMoney(number1)){
System.out.println("取款成功,余额为:"+icbc.getBalance());
}else{
System.out.println("取款失败!");
}
break;
case 3:
//网上支付
System.out.println("请输入支付金额:");
double number2 = scan.nextDouble();
if(icbc.payOnline(number2)){
System.out.println("支付成功,余额为:"+icbc.getBalance());
}else{
System.out.println("支付失败!");
}
break;
default :
System.out.println("输入错误!");
}
*/
/*
//查询余额
System.out.println("余额:"+icbc.getBalance());
//取款
System.out.println("请输入取款金额:");
double number1 = scan.nextDouble();
if(icbc.drawMoney(number1)){
System.out.println("取款成功,余额为:"+icbc.getBalance());
}else{
System.out.println("取款失败!");
}
//网上支付
System.out.println("请输入支付金额:");
double number2 = scan.nextDouble();
if(icbc.payOnline(number2)){
System.out.println("支付成功,余额为:"+icbc.getBalance());
}else{
System.out.println("支付失败!");
}
*/
/*
}else{
System.out.println("密码错误!");
}
*/
/*农行ATM机系统
* 1,什么卡都能插
* 2,只有农行卡才能支付话费
*
*
* */
ABCATM atm = new ABCATM();
//UnionPay card = new ABCImpl(2000.0,"123456");
UnionPay card = new ABCImpl();
atm.insertCard(card);
atm.payTelBill();
//
}
}
class ABCATM{
private UnionPay card;//银联卡
public void insertCard(UnionPay card){
this.card=card;
}
public void payTelBill(){//支付入口按钮
if(card instanceof ABC){
ABC abcCard = (ABC)card;
abcCard.payTelBill("12345678912", 500);
}else{
System.out.println("不是农行卡不能支付");
}
}
}
interface UnionPay{
public abstract double getBalance();
public abstract boolean drawMoney(double money);//取款
public abstract boolean checkPwd(String input);//验证密码
}
interface ICBC extends UnionPay{
public abstract boolean payOnline(double money);
}
interface ABC extends UnionPay{
public abstract boolean payTelBill(String phoneNumber,double money);
}
interface CCB extends UnionPay{
public abstract boolean payGas(String homeNumber,double money);
}
class ABCImpl implements ABC{
private double balance;
private String password;
/*
ABCImpl(double balance,String password){
this.balance=balance;
this.password=password;
}
*/
public double getBalance(){
return balance;
}
public boolean drawMoney(double money){
if(money<=balance){
balance-=money;
return true;
}else{
return false;
}
}
小编推荐一个学Java的学习裙【 六五零,五五四,六零七 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!
public boolean checkPwd(String input){
if(password.equals(input)){
return true;
}
return false;
}
public boolean payTelBill(String phoneNumber,double summary){
if(phoneNumber.length()==11 && (balance-summary)>=2000){
balance-=summary;
return true;
}
return false;
}
}
class ICBCImpl implements ICBC{
private double balance;
private String password;
ICBCImpl(double balance,String password){
this.balance=balance;
this.password=password;
}
public double getBalance(){
return balance;
}
public boolean drawMoney(double money){
if(money<=balance){
balance-=money;
return true;
}else{
return false;
}
}
public boolean checkPwd(String input){
if(password.equals(input)){
return true;
}
return false;
}
public boolean payOnline(double money){
if(balance>=money){
balance-=money;
return true;
}
return false;
}
}
class CCBImpl implements CCB{
private double balance;
private String password;
CCBImpl(double balance,String password){
this.balance=balance;
this.password=password;
}
public double getBalance(){//查询余额
return balance;
}
public boolean drawMoney(double number){//取钱,返回取钱成功与否的结果
if((balance-number)>=5000){
balance-=number;
return true;
}
return false;
}
public boolean checkPwd(String input){//检查密码
if(password.equals(input)){
return true;
}else{
return false;
}
}
public boolean payGas(String homeNumber,double number){
if(homeNumber.length()==11 && (balance-number)>=2000){
balance-=number;
return true;
}
return false;
}
}
程序运行结果演示
程序运行结果演示
小编推荐一个学Java的学习裙【 六五零,五五四,六零七 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!
程序运行结果演示
程序运行结果演示
程序运行结果演示