Initial commit
This commit is contained in:
23
实验报告模板/实验报告6/p14/BusCard.java
Normal file
23
实验报告模板/实验报告6/p14/BusCard.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.p14;
|
||||
|
||||
public class BusCard {
|
||||
private float balance;
|
||||
public BusCard() {
|
||||
balance=0;
|
||||
}
|
||||
public float queryBalance() {
|
||||
return balance;
|
||||
}
|
||||
public void charge(float money) {
|
||||
balance=balance+money;
|
||||
}
|
||||
public void swipe(float money) throws CardException{
|
||||
if(balance<money) {
|
||||
CardException ex=new CardException("余额不足,无法刷卡,请尽快充值!");
|
||||
throw ex;
|
||||
}else {
|
||||
balance=balance-money;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
17
实验报告模板/实验报告6/p14/CardException.java
Normal file
17
实验报告模板/实验报告6/p14/CardException.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.p14;
|
||||
|
||||
public class CardException extends Exception{
|
||||
private String message;
|
||||
public CardException(String message) {
|
||||
this.message=message;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
22
实验报告模板/实验报告6/p14/Test.java
Normal file
22
实验报告模板/实验报告6/p14/Test.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.p14;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//开卡
|
||||
BusCard card=new BusCard();
|
||||
//充值
|
||||
card.charge(2);
|
||||
//查询余额
|
||||
System.out.println("余额:"+card.queryBalance());
|
||||
|
||||
//刷卡
|
||||
try {
|
||||
card.swipe(5);
|
||||
System.out.println("刷卡成功!您的余额:"+card.queryBalance());
|
||||
} catch (CardException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user