面向对象思想小案例——银行存取款代码示例static类属性运用,面向对象static
分享于 点击 20762 次 点评:227
面向对象思想小案例——银行存取款代码示例static类属性运用,面向对象static
package com.bank;/**
* 银行存钱取钱工具类
*/
public class Account {
public static int money=0;
//存款方法
public boolean inCash(int cash){
boolean flag=false;
if(cash>0){
money+=cash;
flag=true;
}else{
System.out.println("存款成功!");
}
return flag;
}
//取钱的方法
public boolean outCash(int cash){
boolean flag=false;
if(money<cash){
System.out.println("余额不足");
flag=true;
return flag;
}else{
System.out.println("取款成功!");
}
money-=cash;
flag=true;
return flag;
}
public void showInfo(){
System.out.println("余额为:"+this.money);
}
}
package com.bank;
import java.util.Scanner;
/**
*代码测试类主方法类
*/
public class Test {
public static void main(String[] args) {
//生成对象
Scanner input =new Scanner(System.in);
boolean flag=false;
do{
System.out.println("1.存款。2.取款。3退出");
int choose=input.nextInt();
switch (choose) {
case 1:
Account ac=new Account();
System.out.println("请输入存款金额:");
int inCash=input.nextInt();
flag=ac.inCash(inCash);
if(flag==true){
System.out.println("存款成功");
ac.showInfo();
}
break;
case 2:
Account ac1=new Account();
System.out.println("请输入取款金额:");
int outCash=input.nextInt();
if(flag==true){
flag=ac1.outCash(outCash);
ac1.showInfo();
}
break;
case 3:
System.out.println("谢谢~");
break;
default:
break;
}
}while(flag==true);
}
}
相关文章
- 暂无相关文章
用户点评