public class creditCard { //余额属性 private int restMoney; public int getRestMoney() { return restMoney; } public void setRestMoney(int restMoney) { this.restMoney = restMoney; } //充值 public int topUp(int Money){ this.restMoney += Money; return restMoney; } //取款 public int withdrawals(int Money){ this.restMoney = restMoney - Money; return restMoney; } //查看余额 public int viewMoney(){ return this.restMoney; } //判断是否透支 public boolean ifOverdraft(){ if(this.restMoney < 0){ return true; }else{ return false; } } public static void main(String[] args){ creditCard card1 = new creditCard(); card1.setRestMoney(0); creditCard card2 = new creditCard(); card2.setRestMoney(0); card1.topUp(1000); card2.topUp(500); card2.withdrawals(600); card1.topUp(500); System.out.println("card1的余额:"+card1.getRestMoney()); System.out.println("card2的余额:"+card2.getRestMoney()); if(!card1.ifOverdraft()){ System.out.println("card1未透支"); }else{ System.out.println("card1已透支"); } if(!card2.ifOverdraft()){ System.out.println("card2未透支"); }else{ System.out.println("card2已透支"); } }; }; |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |