Papers类
package day6_4;
public class Papers {
private String name;
private String add;
public String getAdd() {
return add;
}
public String getName() {
return name;
}
public Papers(String name,String add) {
this.name = name;
this.add=add;
}
}
IdCard类
package day6_4;
public class IdCard extends Papers {
private int age;
public int getAge() {
return age;
}
public IdCard(String name, String add,int age) {
super(name, add);
this.age=age;
}
}
StuCard类
package day6_4;
public class StuCard extends Papers{
private String SchoolName;
public StuCard(String name, String add,String sn) {
super(name, add);
this.SchoolName=sn;
}
public String getSchoolName() {
return SchoolName;
}
}
FireCar类
package day6_4;
import java.util.Scanner;
public class FireCar {
public Ticket buyTicket(Papers p){
Scanner in=new Scanner(System.in);
System.out.println("请输入你要前往的地点:");
String adde=in.next();
System.out.println("请输入日期:(例:2014/2/14/ 04:01)");
String date =in.next();
Ticket t=new Ticket(adde,date);
if(p instanceof StuCard){
System.out.println("因为您使用了学生卡,所有本次购票半价。");
t.setPrice(t.getPrice()/2);
}
return t;
}
}
Ticket类
package day6_4;
public class Ticket {
private String adde;
private String date;
private double price;
public double getPrice() {
return price;
}
public void setPrice(double price) {
price = price;
}
public String getAdde() {
return adde;
}
public void setAdde(String adde) {
this.adde = adde;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Ticket(String adde,String date){
this.adde=adde;
this.date=date;
if(adde.equals("无锡")){
this.price=120;
}else if(adde.equals("淮北")){
this.price=50;
}else if(adde.equals("上海")){
this.price=200;
}else{
this.price=100;
}
}
public Ticket(){}
public void showTicket(){
System.out.println("地点:"+this.adde);
System.out.println("时间:"+this.date);
System.out.println("票价:"+this.price);
}
}
Test类
package day6_4;
public class Test {
public static void main(String[] args) {
StuCard sc=new StuCard("张三","淮北","北师大");
IdCard ic=new IdCard("李四","上海",20);
FireCar fc=new FireCar();
Ticket t=fc.buyTicket(sc);
System.out.println("购票成功!");
System.out.println();
t.showTicket();
Ticket k=fc.buyTicket(ic);
System.out.println("购票成功!");
System.out.println();
k.showTicket();
}
}