A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 江州刺史 中级黑马   /  2015-8-27 10:14  /  2314 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

6黑马币
制作信用卡类:
        类的属性:账户金额
        类的方法:充值,取款,查看余额,查看是否透支
   
        在main 函数中实现使用信用卡类完成如下功能
        实例化两张信用卡对象 card1,card2;  
        为card1 充值 1000 元,card2 充值 500 元;
        从card2 取款 600 元
        向card1 充值500 元
        分别输出 card1和card2 的余额,以及是否透支

最佳答案

查看完整内容

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; ...

7 个回复

正序浏览
liuch111 发表于 2015-8-27 10:32
2楼好厉害 2分钟解决问题

表示两分钟还没看完问题
回复 使用道具 举报
这个题目就不严谨。
不过能写是真的。
在正式项目中不能这样写。考虑的还需要很多
回复 使用道具 举报

  1. /*
  2. *
  3. 制作信用卡类:
  4.         类的属性:账户金额
  5.         类的方法:充值,取款,查看余额,查看是否透支
  6.    
  7.         在main 函数中实现使用信用卡类完成如下功能
  8.         实例化两张信用卡对象 card1,card2;  
  9.         为card1 充值 1000 元,card2 充值 500 元;
  10.         从card2 取款 600 元
  11.         向card1 充值500 元
  12.         分别输出 card1和card2 的余额,以及是否透支
  13. */

  14. class CreditCard
  15. {
  16.         private float yuE=0;
  17.         //充值方法
  18.         public boolean  chongZhi(float jinE)
  19.         {
  20.                 yuE+=jinE;
  21.                 return true;
  22.         }
  23.         //取款方法
  24.         public boolean quKuan(float jinE)
  25.         {
  26.                 yuE-=jinE;
  27.                 return true;
  28.         }
  29.         //查余额方法
  30.         public float chaYuE()
  31.         {
  32.                 return yuE;
  33.         }
  34.         //查看是否透支方法
  35.         public boolean chaTouZhi()
  36.         {
  37.                 return yuE<0;
  38.         }
  39.        
  40.        
  41. }
  42. public class BankDemo {

  43.         public static void main(String[] args) {
  44.                 CreditCard card1 = new CreditCard();
  45.                 CreditCard card2 = new CreditCard();
  46.                 System.out.println("card1充值1000元:"+card1.chongZhi(1000));
  47.                 System.out.println("card2充值500元:"+card2.chongZhi(500));
  48.                 System.out.println("card2取款600:"+card2.quKuan(600));
  49.                 System.out.println("card1充值500:"+card1.chongZhi(500));
  50.                 System.out.println("card1余额:"+card1.chaYuE());
  51.                 System.out.println("card1是否透支:"+card1.chaTouZhi());
  52.                 System.out.println("card2余额:"+card2.chaYuE());
  53.                 System.out.println("card2是否透支:"+card2.chaTouZhi());
  54.                
  55.                
  56.         }
复制代码
回复 使用道具 举报
这个不涉及多线程吧。每个人有自己的卡。不存在操作同一账号的情况。没必要的,就是一个简单地面向对象。我根据实际情况设计了下,可以参考下。

  1.         public class Card {
  2.         private int MAXLIMIT=0;
  3.         private String name="";//为账户加个名字
  4.         private int money = 0;//账户余额
  5.     //构造函数初始账户最高信用额度
  6.         public Card(String name,int num)
  7.         {
  8.                 MAXLIMIT=num;
  9.                 this.money=MAXLIMIT;
  10.                 this.name=name;
  11.                 System.out.println(name+"账户额度:¥"+money+"元" );
  12.         }
  13.         //还款
  14.         public void addMoney(int num) {
  15.                 money+=num;
  16.                 System.out.println(name+"账户还款:¥"+num+"元  当前额度:¥"+money+"元");
  17.         }
  18.         //获取账户当前额度
  19.         public int getMoney()
  20.         {
  21.                 return money;
  22.         }
  23.         //透支
  24.         public int isOver (int money)
  25.         {
  26.                 return money-1000;
  27.         }
  28.         //消费函数
  29.         public void getoutMoney(int num)
  30.         {
  31.                 money-=num;
  32.                 System.out.println(name+"消费:¥"+num+"元  当前额度:¥"+ getMoney()+"元");
  33.         }
  34.         //获取账户信息
  35.         public void cardInfo()
  36.         {
  37.                 //账户的额度大于等于初始额度,没有透支,反之透支。。
  38.                 System.out.println(name+"的账户当前额度:¥"+ getMoney()+"元\t"+(isOver(getMoney())>0?"没有透支":"透支了¥"+isOver(getMoney())+"元"));
  39.         }
  40.         public static void main(String[] args)
  41.         {
  42.                 Card c1=new Card("小明",1000);
  43.                 Card c2=new Card("花花",1000);
  44.                 c1.getoutMoney(1500);
  45.                 c2.getoutMoney(2500);
  46.                 c1.addMoney(1000);
  47.                 c2.addMoney(1500);
  48.                 c1.cardInfo();
  49.                 c2.cardInfo();
  50.         }
  51. }
复制代码
回复 使用道具 举报
2楼好厉害 2分钟解决问题
回复 使用道具 举报
这应该是使用多线程解决问题的一个案例吧
回复 使用道具 举报
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已透支");
                }
        };

};


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马