继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

迪迪打车系统。。。。。。。

qq_Matsukazeten_0
关注TA
已关注
手记 1
粉丝 0
获赞 5

package DiDiSystem;
/**

  • 所有车的父类
  • @author XJkon
  • */
    public abstract class Car {
    private int price; //车辆的价格
    private int day; //租车天数
    private String name; //车的名字
    protected int number; //车的数量
    public int getPrice() { //get set封装方法
    return price;
    }
    public void setPrice(int price) {
    this.price = price;
    }

    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getDay() {
    return day;
    }
    public void setDay(int day) { //get set封装方法
    this.day = day;
    }
    abstract public void display(); //抽象的展示方法
    abstract public int price(); //抽象的返回价格的方法

}


package DiDiSystem;
/**

  • 小型货车载货量4.5T
  • @author XJkon
  • */
    public class SmallLorry extends Car {
    private float goodsLoad; //载货量
    SmallLorry(){
    goodsLoad=5.5f;
    super.setPrice(600);
    super.setName("小型货车");
    }

    @Override
    public void display() {
    System.out.println(super.getName()+"\t"+super.getPrice()+"元/天\t\t载货:"+goodsLoad+"吨");

    }

    @Override
    public int price() { //返回该对象的总价
    // TODO Auto-generated method stub
    return super.getPrice()super.getDay();
    }
    public float goodsLoad(){
    return goodsLoad
    super.number;

    }

}


package DiDiSystem;
/**

  • 大型货车载货量20T
  • @author XJkon
  • */
    public class SuperLorry extends Car {
    private float goodsLoad; //载货量
    SuperLorry(){
    goodsLoad=20f;
    super.setPrice(1000);
    super.setName("大型货车");
    }
    @Override
    public void display() { //展示的方法
    System.out.println(super.getName()+"\t"+super.getPrice()+"元/天\t载货:"+goodsLoad+"吨");

    }

    public int price() { //返回该对象的总价
    // TODO Auto-generated method stub
    return super.getPrice()super.getDay();
    }
    public float goodsLoad(){
    return goodsLoad
    super.number;

    }

}
package DiDiSystem;
/**

  • 小型轿车载客量5人
  • @author XJkon
  • */
    public class Limousine extends Car {
    private int busLoad; //载客量
    Limousine(){
    busLoad=5;
    super.setPrice(300);
    super.setName("小轿车");
    }
    @Override
    public void display() {
    System.out.println(super.getName()+"\t"+super.getPrice()+"元/天\t\t载人:"+busLoad+"人");

    }

    public int price() { //返回该对象的总价
    // TODO Auto-generated method stub
    return super.getPrice()super.getDay();
    }
    public int busLoad(){
    return busLoad
    super.number;

    }

}


package DiDiSystem;
/**

  • 客运巴士载客量25人
  • @author XJkon
  • */
    public class Bus extends Car {
    private int busLoad;
    Bus(){
    busLoad=25;
    super.setPrice(500);
    super.setName("客运巴士");
    }
    @Override
    public void display() {
    System.out.println(super.getName()+"\t"+super.getPrice()+"元/天\t\t载人:"+busLoad+"人");

    }

    public int price() { //返回该对象的总价
    // TODO Auto-generated method stub
    return super.getPrice()super.getDay();
    }
    public int busLoad(){
    return busLoad
    super.number;

    }

}


package DiDiSystem;
/**

  • 皮卡车载人4个,载货4.5T
  • @author XJkon
  • */
    public class Pickup extends Car {
    private int busLoad; //载人量
    private float goodsLoad; //载货量
    Pickup(){
    busLoad=5;
    goodsLoad=4.5f;
    super.setPrice(550);
    super.setName("皮卡车");
    }
    @Override
    public void display() {
    System.out.println(super.getName()+"\t"+super.getPrice()+"元/天\t\t载人:"+busLoad+"人 载货:"+goodsLoad+"吨");

    }

    public int price() { //返回该对象的总价
    // TODO Auto-generated method stub
    return super.getPrice()super.getDay();
    }
    public int busLoad(){
    return busLoad
    super.number;

    }
    public float goodsLoad(){
    return goodsLoad*super.number;

    }

}


package DiDiSystem;

import java.util.Scanner;

/**

  • 该类是打车系统的执行类
  • @author XJkon
  • */
    public class DiDisytem {

    public static void main(String[] args) {
    display();

    }
    static void display(){ //这个方法用于前半段,展示并给用户提供输入
    String scanner; //用于接收用户输入
    Car [] sum; //保存用户输入的对象
    Car[] car={new SmallLorry(),new SuperLorry(),new Limousine(),new Bus(),new Pickup()}; //建立父类对象的数组
    Scanner sc = new Scanner(System.in);
    System.out.println("欢迎使用租车系统:\n您是否要租车:1是 0否");
    try{ //检测用户输入的正确性
    scanner=sc.next();
    if(scanner.equals("0")){ //输入0退出
    System.out.println("感谢您的使用,再见!");
    System.exit(0);
    }
    if(scanner.equals("1")==false){ //如果用户输入了"1"以外的字符报异常
    throw new RuntimeException("您的输入有误!");
    }
    System.out.println("您可租车的类型和价目表:"); //开始展示
    System.out.println("序号\t汽车类型\t租金\t\t容量");
    for(int i=0;i<car.length;i++){
    System.out.print(i+1+".\t");
    car[i].display();
    }
    System.out.print("请输入租车的数量(1-20台):\r");
    scanner=sc.next(); //用户输入租车台数
    if(Integer.parseInt(scanner)<0||Integer.parseInt(scanner)>10){
    throw new RuntimeException("非常抱歉,输入异常字符或超过本系统支持的最大租车量<1-10台>");
    }
    sum =new Car[Integer.parseInt(scanner)]; //创建sum这个对象数组的长度=用户输入的租车数,便于统计用户使用了多少个对象
    for(int i=0;i<Integer.parseInt(scanner);i++){
    System.out.print("请输入第"+(i+1)+"台车的序号:"+"\r");
    String s=sc.next(); //用户输入的数量用字符串变量s来接收
    if(Integer.parseInt(s)<0||Integer.parseInt(s)>5){
    throw new RuntimeException("非常抱歉,输入异常字符或序号输入不正确<1-5号>");
    }
    car[Integer.parseInt(s)-1].number++;
    sum[i]=car[Integer.parseInt(s)-1]; //
    将用户选择的车保存起来
    }
    System.out.print("请输入租用天数1-90天:"+"\r");
    scanner=sc.next();
    if(Integer.parseInt(scanner)<=0||Integer.parseInt(scanner)>90){
    throw new RuntimeException("非常抱歉,输入异常字符或超过最大租用天数<90天>");
    }
    for(int i=0;i<sum.length;i++){ //**一种不太好的方法,给用户选择的车的租车天数赋值
    sum[i].setDay(Integer.parseInt(scanner));
    }
    billprint(sum,car);
    } //try块结束标识
    catch(RuntimeException e){
    e.printStackTrace();
    }
    }
    static void billprint(Car[] sum,Car[] car){ //这个方法用于为用户提供账单
    System.out.println("您的账单");
    System.out.print("可载人的车有:");
    SmallLorry a= (SmallLorry) car[0]; //向下转型
    SuperLorry a1=(SuperLorry) car[1];
    Limousine a2=(Limousine) car[2];
    Bus a3=(Bus) car[3];
    Pickup a4= (Pickup) car[4];
    for(int i=2;i<=4;i++)
    {
    if(car[i].number>0)
    System.out.print(car[i].getName()+"
    "+car[i].number+"\t");
    if(i==4)
    System.out.print("总载客量"+(a2.busLoad()+a3.busLoad()+a4.busLoad())+"人\n");
    }
    System.out.print("可载货的车有:");
    for(int i=0;i<=4;i++)
    {
    if(car[i].number>0&&i<=1)
    System.out.print(car[i].getName()+""+car[i].number+"\t");
    if(car[i].number>0&&i==4)
    {
    System.out.print(car[i].getName()+"
    "+car[i].number+"\t");
    }
    }
    System.out.print("总载货量"+(a.goodsLoad()+a4.goodsLoad()+a1.goodsLoad())+"吨\n");
    for(int i=0,sun=0;i<car.length;i++){
    sun+=car[i].price();
    if(i==4){
    System.out.println("这是您的账单总价:"+sun+"元,租"+car[i].getDay()+"天 感谢您的使用,再见");
    }
    }
    }

}

打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP

热门评论

package DiDiSystem;
/**
 * 大型货车载货量20T
 * @author XJkon
 *
 */
public class SuperLorry extends Car  {
 private float goodsLoad;        //载货量
 SuperLorry(){
  goodsLoad=20f;
  super.setPrice(1000);
  super.setName("大型货车");
 }
 @Override
 public void display() {                                                          //展示的方法
  System.out.println(super.getName()+"\t"+super.getPrice()+"元/天\t载货:"+goodsLoad+"吨");
  
 }

 public int price() {                  //返回该对象的总价
  // TODO Auto-generated method stub
  return super.getPrice()*super.getDay();
 }
 public float goodsLoad(){
  return goodsLoad*super.number;
  
 }


}

package DiDiSystem;
/**
 * 小型货车载货量4.5T
 * @author XJkon
 *
 */
public class SmallLorry extends Car {
    private float goodsLoad;     //载货量
    SmallLorry(){
     goodsLoad=5.5f;
        super.setPrice(600);
        super.setName("小型货车");
    }
 
 @Override
 public void display() {
  System.out.println(super.getName()+"\t"+super.getPrice()+"元/天\t\t载货:"+goodsLoad+"吨");
  
 }

 @Override
 public int price() {                  //返回该对象的总价
  // TODO Auto-generated method stub
  return super.getPrice()*super.getDay();
 }
 public float goodsLoad(){
  return goodsLoad*super.number;
  
 }

}

查看全部评论