Java购票系统实训总结_Java 购票系统实现

本文介绍了使用Java编程实现的一个简单购票系统。系统包括Papers、IdCard、StuCard和FireCar等类,用于模拟购票流程。用户可以选择学生卡享受半价优惠,程序会根据输入的目的地和日期计算票价。
摘要由CSDN通过智能技术生成

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值