1 问题
如何设计实现一个统一透支限额的信用卡账户类(CreditCard)和一个用于测试的演示类 。
2 方法
带有一个参数的构造方法,用于初始化name。
各个属性的 setters和getters 方法:注意:。
往账户中存款方法void deposit(double amount) //向账户中存款amount 元
从账户中取款方法boolean withdraw(double amount) //从账户中取款amount元,取款成功(条件为:取款金额<账户余额+透支限额)返回true,否则返回false。
返回账户信息字符串的方法:Stringinfo()。返回的字符串内容包括:姓名、余额、最大透支限额。
代码清单 1
package practice.bank; public class CreditCard { public String name; public double balance; public double amount; static double maxOverdraft; public CreditCard(String name, double balance, double amount) { this.name = name; this.balance = balance; this.amount = amount; } public CreditCard() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } static { maxOverdraft=3000; } public void deposit(double amount) { balance = amount; } public boolean withdraw(double amount) { if (amount <= balance + maxOverdraft) { System.out.println("取出" + amount + "元" + "成功!"); System.out.println("账户信息" + "姓名:" + this.name+ ",余额:" + this.balance+ ",最大透支额:" + maxOverdraft); return true; } else{ System.out.println("余额不足,取款"+amount+"元"+"失败!"); return false; } } public String info() { return"账户信息"+"姓名" + this.name + ", 余额" + this.balance+ ", 最大透支额" + maxOverdraft; } } package practice.bank; public class CreditCardDemo { public static void main(String[] args){ CreditCard creditCard = new CreditCard(); creditCard.setName("张三"); System.out.println(creditCard.info()); System.out.println("**测试1:从信用卡取出5000元。"); creditCard.withdraw(5000); System.out.println("**测试2:往信用卡存2000元。"); creditCard.deposit(2000); System.out.println(creditCard.info()); System.out.println("**测试3:再从信用卡取出5000元。"); creditCard.withdraw(5000); } } |
3 结语
在本次任务要学会分析“信用卡账户的设计与实现”任务实现的逻辑思路。能够独立完成“信用卡账户的设计与实现”程序的源代码编写、编译以及运行。掌握静态属性、静态方法的编写与使用。掌握 Java 中的构造方法、setter 方法、getter 方法的知识点。