为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第二季 答答租车系统

标签:
Java

借鉴了几位大神的
Car类

   package cn.liu.kf;

    public class Car {
private int price;
private String name;
private int id;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

   }

AutoCar类

  package cn.liu.kf;

    public class AutoCar extends Car implements ILoadPerson{
private int numbers;
  public AutoCar(int id,String name, int price, int newloadPerson){

        super.setId(id);
        super.setName(name);
        super.setPrice(price);
        this.setloadPerson(newloadPerson);
    }
@Override
public void setloadPerson(int number) {
    // TODO Auto-generated method stub
    this.numbers = number;
}

@Override
public int getloadPerson() {
    // TODO Auto-generated method stub
    return numbers;
}
    public String toString(){
   return  super.getId() + "  " + super.getName() + "\t" + super.getPrice() + "元/天\t" + " " + "载人:" + this.getloadPerson() + "人";
  }
}

Trunk类

package cn.liu.kf;

public class Trunk extends Car implements ILoadGoods {
private int numbers;
  public Trunk(int id,String name, int price, int newloadGoods){

        super.setId(id);
        super.setName(name);
        super.setPrice(price);
        this.setloadGoods(newloadGoods);
    }
@Override
public void setloadGoods(int number) {
    // TODO Auto-generated method stub
  this.numbers = number;
}

@Override
public int getloadGoods() {
    // TODO Auto-generated method stub
    return this.numbers;
}

public String toString(){
       return  super.getId() + "  " + super.getName() + "\t" + super.getPrice()  + "元/天\t" + " " + "载货:" + this.getloadGoods() + "吨";
   }
}

PickUpCar类

package cn.liu.kf;

public class PickUpCar extends Car implements ILoadGoods, ILoadPerson {
private int personNumber;
private int goodsNumber;
public PickUpCar(int id,String name, int price, int newloadPerson , int newloadGoods){

    super.setId(id);
    super.setName(name);
    super.setPrice(price);
    this.setloadPerson(newloadPerson);
    this.setloadGoods(newloadGoods);
}

@Override
public void setloadPerson(int number) {
    // TODO Auto-generated method stub
    this.personNumber = number;
}

@Override
public int getloadPerson() {
    // TODO Auto-generated method stub
    return this.personNumber;
}

@Override
public void setloadGoods(int number) {
    // TODO Auto-generated method stub
 this.goodsNumber = number;
}

@Override
public int getloadGoods() {
    // TODO Auto-generated method stub
    return this.goodsNumber;
}

public String toString(){
       return  super.getId() + "  " + super.getName() + "\t" + super.getPrice()  + "元/天\t" + " " +"载人"+ this.getloadPerson() + "载货:" + this.getloadGoods() + "吨";
   }
}

ILoadPerson类

package cn.liu.kf;

public interface ILoadPerson {
  public void setloadPerson(int number);
  public int getloadPerson();
}

ILoadGoods类

package cn.liu.kf;

public abstract interface ILoadGoods {
public abstract void setloadGoods(int number);
public  abstract int getloadGoods();
}

Test类

package cn.liu.kf;

import java.util.Scanner;

public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
 Scanner sc = new Scanner(System.in);
 System.out.println("欢迎使用答答租车系统");
 System.out.println("您是否要租车:1是 0否");
 int num = sc.nextInt();

 Car[] cars = {new AutoCar(1,"奥迪A4",500,4), 
               new AutoCar(2,"马自达6",400,4),
               new PickUpCar(3,"皮卡雪6",450,4,2),
               new AutoCar(4,"金龙",800,20),
               new Trunk(5,"松花江",400,4),
               new Trunk(6,"依维河",1000,20)};

if(num!=1){
    System.out.println("欢迎下次光临");
    System.exit(0);
}else{
    System.out.println("您可租车的类型及其价目表:");
     System.out.println("序号 汽车名称 租金    容量");
    for(int i=0;i<cars.length;i++){
        System.out.println(cars[i]);
    }
}
int priceSum = 0;
int loadPersonSum = 0;
int loadGoodsSum = 0;
String personNames = "";
String goodsNames = "";
System.out.println("请输入您要租车汽车的数量");
int count = sc.nextInt();
for(int i=1;i<=count;i++){
    System.out.println("请输入第"+i+"辆车的序号");
    int k = sc.nextInt()-1;
    if(cars[k].getClass()==AutoCar.class){
        personNames = personNames + cars[k].getName() + "\t";
    loadPersonSum = loadPersonSum + ((AutoCar)cars[k]).getloadPerson();
    priceSum = priceSum + cars[k].getPrice();
    }else if(cars[k].getClass() == Trunk.class){    
        goodsNames = goodsNames + cars[k].getName() + "\t";
            loadGoodsSum = loadGoodsSum + ((Trunk)cars[k]).getloadGoods();
            priceSum = priceSum + cars[k].getPrice();
    }else if(cars[k].getClass() == PickUpCar.class){
        personNames = personNames + cars[k].getName() + "\t";
        goodsNames = goodsNames + cars[k].getName() + "\t";
        loadPersonSum = loadPersonSum + ((PickUpCar)cars[k]).getloadPerson();
        loadGoodsSum = loadGoodsSum + ((PickUpCar)cars[k]).getloadGoods();
        priceSum = priceSum + cars[k].getPrice();
    }

        }

   System.out.println("请输入租车天数");
   int days = sc.nextInt();
   System.out.println("***可载人的车有:");
   System.out.println(personNames + "共载人:" + loadPersonSum + "人");
   System.out.println("***载货的车有:");
   System.out.println(goodsNames + "共载货:" + loadGoodsSum + "吨");
   System.out.println("***租车总价格:" + priceSum*days + "元");
}

}
点击查看更多内容
26人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消