手记

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人推荐
随时随地看视频
慕课网APP

热门评论

请问这个是什么意思:cars[k].getClass()==AutoCar.class

厉害,帮我学到好多东西

之后输入的天数会覆盖之前的天数:如一次租两辆车 第二辆输入的天数 会使得第一次输入的天数算成第二辆车的天数 有可能第一辆车的天数不与第二辆相同

查看全部评论