Java接口公交卡(金陵一卡通)问题
这次老师没有发题目,内容大致如下:(加上了个人的修改)

二话不说上代码。这次算上两个接口和一个主类,总共有11个文件。
1.Card.java源文件:
package BusCard;
public class Card {
Card(){
System.out.println("A Card has been produced!");
}
public void Using(){
System.out.println("Card is using!");
}
}
**************************************************************************
2.Buscard,java源文件:
package BusCard;
public class Buscard extends Card{
Buscard(){
System.out.println("A Buscard has been produced!");
}
public void Using(){
System.out.println("Buscard is using!");
}
}
*****************************************************************************
3.park.java源文件:
package BusCard;
public class park {
String name;
}
********************************************************************************
4.hospital.java源文件:
package BusCard;
public class hospital {
String name;
}
******************************************************************************
5.SeeDoctorable.java源文件:(注意这里创建文件时选interface而不是class,虽然没啥影响……)
package BusCard;
interface SeeDoctorable {
public void SeeDoctor(hospital p); //使用医疗卡
public void showh(); //显示能使用医疗卡的医院
}
**********************************************************************************
6.Parkable.java源文件:(注意点同上……)
package BusCard;
public interface Parkable {
public void Park(park pa); //公园卡功能
public void showp(); //显示能使用公园卡的公园名字
}
***********************************************************************************
7.NoneBusCard.java源文件:
package BusCard;
public class NoneBusCard {
NoneBusCard(){
System.out.println("A NoneBusCard has been produced!");
}
public void Using(){
System.out.println("NoneBusCard is using!");
}
}
******************************************************************************
8.RealBusCard.java源文件:
package BusCard;
import java.util.*;
public class RealBusCard implements //连接接口
SeeDoctorable,Parkable{
int Cardnumber;
String username;
Account user;
RealBusCard(){ //创建实名卡
Account u = new Account();
this.Cardnumber = u.Accountnumber;
this.user = u;
System.out.println("Please input your name:");
Scanner cin = new Scanner(System.in);
this.username = cin.next();
user.username = this.username;
System.out.println("A RealBusCard has been produced!");
System.out.println("Your cardnumber is "+this.Cardnumber);
}
public void Using(){
System.out.println("RealBusCard is using!");
}
public void RealBusCard_set(){}
public void SeeDoctor(hospital p){ //接口1,医疗卡功能
if (RegisterPool.if_inhospital(p)==true) {
System.out.println("RealBusCard is hospitalable!");
}
else {
System.out.println("RealBusCard is not hospitalable!\n"
+ "Because the hospital is not in the list!");
}
}
public void Park(park pa){ //接口2,公园卡功能
if (RegisterPool.if_inpark(pa)==true) {
System.out.println("RealBusCard is parkable!");
}
else {
System.out.println("RealBusCard is not parkable!\n"
+ "Because the park is not in the list!");
}
}
public void showp() { //显示能使用公园卡的公园名字
System.out.println("You can visit the following park:");
for (int i=0;i<RegisterPool.pi;i++) {
System.out.println(RegisterPool.p[i].name);
}
}
public void showh() { //显示能使用医疗卡的医院
System.out.println("You can visit the following hospital :");
for (int i=0;i<RegisterPool.hi;i++) {
System.out.println(RegisterPool.h[i].name);
}
}
}
****************************************************************************
9.RegisterPool源文件:
package BusCard;
public class RegisterPool {
static hospital[] h = new hospital[10];
static park[] p = new park[10];
static int hi = 0; //医院序号
static int pi = 0; //公园序号
static void newhospital(hospital ho){ //添加医院信息
h[hi] = ho;
hi++;
System.out.println(ho.name+"has been created!");
}
static void newpark(park pa){ //添加公园信息
p[pi] = pa;
pi++;
System.out.println(pa.name+"has been created!");
}
static void deletehospital(hospital ho){ //删除医院信息
for (int i=0;i<10;i++) {
if (ho==RegisterPool.h[i]) {
RegisterPool.h[i] = null;
RegisterPool.hi--;
break;
}
}
System.out.println(ho.name+"has been deleted!");
}
static void deletepark(park pa){//删除公园信息
for (int i=0;i<10;i++) {
if (pa==RegisterPool.p[i]) {
RegisterPool.p[i] = null;
RegisterPool.pi--;
break;
}
}
System.out.println(pa.name+"has been deleted!");
}
static boolean if_inhospital(hospital ho){ //检测医院是否在列表中
boolean flag = false;
for (int i =0;i<10;i++) {
if (ho==RegisterPool.h[i]) {
flag = true;
break;
}
}
return flag;
}
static boolean if_inpark(park pa){ //检测公园是否在列表中
boolean flag = false;
for (int i =0;i<10;i++) {
if (pa==RegisterPool.p[i]) {
flag = true;
break;
}
}
return flag;
}
}
*********************************************************************
10.Account.java源文件:
package BusCard;
public class Account {
static int Accountnumber = 1001;
String username;
Account(){
Accountnumber++;
//每次创造一张实名卡就使卡号+1
}
}
*************************************************************************************
11.Cardmain.java源文件:
package BusCard;
public class Cardmain {
public static void main(String[] args) {
// TODO Auto-generated method stub
hospital A = new hospital(); //创建基础医院和公园信息。
A.name = "中山医院";
hospital A2 = new hospital(); //创建基础医院和公园信息。
A2.name = "武警医院";
hospital A3 = new hospital(); //创建基础医院和公园信息。
A3.name = "卫岗社区医院";
park B = new park();
B.name = "下马坊公园";
park B2 = new park();
B2.name = "仙林湖公园";
RegisterPool.newhospital(A);
RegisterPool.newhospital(A2);
RegisterPool.newhospital(A3);
RegisterPool.newpark(B);
RealBusCard C1 = new RealBusCard(); //创建实名卡
C1.showh();
C1.showp();
C1.Using(); //使用
C1.SeeDoctor(A); //医疗卡功能,正常使用
C1.Park(B); //公园卡功能,正常使用
C1.Park(B2); //公园卡功能,非法使用,公园列表中没有B2
}
}
*****************************************************************************************
如果有错误欢迎提出讨论!

浙公网安备 33010602011771号