求java题目答案!!大神快快来~~
java题目是:银行卡:有属性:卡号,密码(在创建时就应该有默认值),存款金额1.卡号允许查看,但不许修改2.密码不许查看,但可以修改3.取款要输入取款金额,密码,卡号a...
java题目是:银行卡:有属性:卡号,密码(在创建时就应该有默认值),存款金额1.卡号允许查看,但不许修改2.密码不许查看,但可以修改3.取款要输入取款金额,密码,卡号a:如果余额小于取款金额,不能取款b:如果密码或者卡号错误也要给提示c:取款完了要返回取款的金额,还要修改余额d:可以查看当前余额(要提供卡号及密码)修改银行卡的例子,保证其中使用了this的2种用法(this.成员,this());4.统计总共产生了多少张卡5.加入一个限制,限制卡只允许产生3张对象,比如3个对象那么第4个对象开始创建的结果为null;注意,无论创建多少对象,这些对象创建的时机是不可预测的
展开
3个回答
展开全部
package com.sxit.adc.test;
import java.util.ArrayList;
import java.util.List;
public class Bank {
private static final int MAX_CARD_NUMBER = 3;
private List<Card> cards = new ArrayList<Card>();
/**
* 取款要输入取款金额,密码,卡号
a:如果余额小于取款金额,不能取款
b:如果密码或者卡号错误也要给提示
c:取款完了要返回取款的金额,还要修改余额
d:可以查看当前余额(要提供卡号及密码)修改银行卡的例子,保证其中使用了this的2种用法(this.成员,this());
* @param no 卡号
* @param pwd 密码
* @param val 取款金额
* @return
*/
public Card withdrawMoney(int no, String pwd, float val) {
Card card = getCard(no);
if (card != null) {
// 验证密码
if (card.testPwd(pwd)) {
if(val >= card.getValue()){
card.setValue(card.getValue() - val);
System.out.println("取款成功");
System.out.println("卡号: " + card.getNo());
System.out.println("余额: " + card.getValue());
} else {
System.out.println("余额小于取款金额,不能取款");
}
} else {
System.out.println("密码错误");
}
} else {
System.out.println("账户不存在");
}
return card;
}
/**
* 查询账户信息
* @param no 卡号
* @param pwd 密码
* @return 银行卡对象
*/
public Card getCard(int no, String pwd){
Card card = getCard(no);
if (card != null) {
// 验证密码
if (card.testPwd(pwd)) {
System.out.println("取款成功");
System.out.println("卡号: " + card.getNo());
System.out.println("余额: " + card.getValue());
} else {
System.out.println("密码错误");
}
} else {
System.out.println("账户不存在");
}
return card;
}
/**
* 获取银行账户数
* @return
*/
public int getBankCardNumber(){
return cards.size();
}
/**
* 开户
* @param pwd 密码 传空密码则为默认值
* @return 返回新开的账户对象
*/
public Card newCard(String pwd) {
if (MAX_CARD_NUMBER < cards.size()) {
Card card = new Card();
if (pwd != null && pwd.length() > 0) {
card.setPwd(pwd);
}
return card;
} else {
System.out.println("银行账户已满, 咱不能开户");
return null;
}
}
private Card getCard(int no){
for (Card card : cards) {
if(card.getNo() == no){
return card;
}
}
return null;
}
}
package com.sxit.adc.test;
public class Card {
private static int curCardNumber = 1;
private String uid;
private int no;
private String pwd = "123456";
private float value;
public Card() {
no = curCardNumber++;
};
public boolean testPwd(String pwd) {
if (this.pwd.equals(pwd)) {
return true;
} else {
return false;
}
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public int getNo() {
return no;
}
public String getPwd() {
return "******";
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
}
刚写好的, 还没测
import java.util.ArrayList;
import java.util.List;
public class Bank {
private static final int MAX_CARD_NUMBER = 3;
private List<Card> cards = new ArrayList<Card>();
/**
* 取款要输入取款金额,密码,卡号
a:如果余额小于取款金额,不能取款
b:如果密码或者卡号错误也要给提示
c:取款完了要返回取款的金额,还要修改余额
d:可以查看当前余额(要提供卡号及密码)修改银行卡的例子,保证其中使用了this的2种用法(this.成员,this());
* @param no 卡号
* @param pwd 密码
* @param val 取款金额
* @return
*/
public Card withdrawMoney(int no, String pwd, float val) {
Card card = getCard(no);
if (card != null) {
// 验证密码
if (card.testPwd(pwd)) {
if(val >= card.getValue()){
card.setValue(card.getValue() - val);
System.out.println("取款成功");
System.out.println("卡号: " + card.getNo());
System.out.println("余额: " + card.getValue());
} else {
System.out.println("余额小于取款金额,不能取款");
}
} else {
System.out.println("密码错误");
}
} else {
System.out.println("账户不存在");
}
return card;
}
/**
* 查询账户信息
* @param no 卡号
* @param pwd 密码
* @return 银行卡对象
*/
public Card getCard(int no, String pwd){
Card card = getCard(no);
if (card != null) {
// 验证密码
if (card.testPwd(pwd)) {
System.out.println("取款成功");
System.out.println("卡号: " + card.getNo());
System.out.println("余额: " + card.getValue());
} else {
System.out.println("密码错误");
}
} else {
System.out.println("账户不存在");
}
return card;
}
/**
* 获取银行账户数
* @return
*/
public int getBankCardNumber(){
return cards.size();
}
/**
* 开户
* @param pwd 密码 传空密码则为默认值
* @return 返回新开的账户对象
*/
public Card newCard(String pwd) {
if (MAX_CARD_NUMBER < cards.size()) {
Card card = new Card();
if (pwd != null && pwd.length() > 0) {
card.setPwd(pwd);
}
return card;
} else {
System.out.println("银行账户已满, 咱不能开户");
return null;
}
}
private Card getCard(int no){
for (Card card : cards) {
if(card.getNo() == no){
return card;
}
}
return null;
}
}
package com.sxit.adc.test;
public class Card {
private static int curCardNumber = 1;
private String uid;
private int no;
private String pwd = "123456";
private float value;
public Card() {
no = curCardNumber++;
};
public boolean testPwd(String pwd) {
if (this.pwd.equals(pwd)) {
return true;
} else {
return false;
}
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public int getNo() {
return no;
}
public String getPwd() {
return "******";
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
}
刚写好的, 还没测
追问
谢啦!!
追答
可以吗?
展开全部
拿去试试吧,我还没测呢
public class Card{
private String username;
private String password;
private static volatile int count = 0;
private volatile Double amount = 0.0D;
private Card(){
}
public static Card newCard(){
if (count <=3) {
synchronized(Card.class){
if (count <=3) {
count ++;
return new Card();
}else {
return null;
}
}
}else {
return null;
}
}
public double withDrawal(double amount){
if (this.amount > 0 && this.amount > amount) {
synchronized(this.amount){
if (this.amount > 0 && this.amount > amount) {
this.amount -= amount;
return amount;
}else{
return 0d;
}
}
}else{
return 0d;
}
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
}
public class Card{
private String username;
private String password;
private static volatile int count = 0;
private volatile Double amount = 0.0D;
private Card(){
}
public static Card newCard(){
if (count <=3) {
synchronized(Card.class){
if (count <=3) {
count ++;
return new Card();
}else {
return null;
}
}
}else {
return null;
}
}
public double withDrawal(double amount){
if (this.amount > 0 && this.amount > amount) {
synchronized(this.amount){
if (this.amount > 0 && this.amount > amount) {
this.amount -= amount;
return amount;
}else{
return 0d;
}
}
}else{
return 0d;
}
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
}
追问
谢啦!!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-06-16
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询