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

Java第二季-嗒嗒租车系统

马克Y图布
关注TA
已关注
手记 2
粉丝 0
获赞 3

import java.util.Scanner;
public class Test {
public static void main(String[] args){
Car[] cars = {new PassengerCar("奥迪A4",4,500),
new PassengerCar("马自达6",4,400),
new PickUp("皮卡雪 6",4,2,450),
new PassengerCar("金龙",20,800),
new Truck("松花江",4,400),
new Truck("依维合",20,100)};
Scanner input = new Scanner(System.in);
System.out.println("***您是否要租车 (任意数.是 0.否)");
int inp = input.nextInt();
if(inp != 0){
System.out.println("您可租车的类型和价格");
System.out.println("序号 汽车 名称 租金 功能");
for(int i=1;i<cars.length;i++){
System.out.println("序号"+i+" "+cars[i-1].toString());
}
}else{
System.out.println("欢迎下次再来");
System.exit(0);
}
System.out.println("预定数量");
int num = input.nextInt();//编号
int passengerSum = 0; //载客人数
int priceSum = 0; //价钱
int tonnageSum = 0; //拉货吨数
for(int i=1;i<num+1;i++){
System.out.println("请输入第"+i+"辆要预定车辆的序号");
int on=input.nextInt();
passengerSum += cars[i-1].getPassenger() ;
priceSum += cars[i-1].getPrice();
tonnageSum += cars[i-1].getTonnage();
}
System.out.println("一共需要"+priceSum+"元/天"+" "+"共载货量:"+tonnageSum+"吨 "+"共载客量:"+ passengerSum+"人");
}
}


public abstract class Car {
private String name; //车名
private int number; //编号
private int passenger; //载客人数
private int price; //价钱
private int tonnage; //拉货吨数
/**

  • @return the name
    */
    public String getName() {
    return name;
    }
    /**
  • @param name the name to set
    */
    public void setName(String name) {
    this.name = name;
    }
    /**
  • @return the number
    */
    public int getNumber() {
    return number;
    }
    /**
  • @param number the number to set
    */
    public void setNumber(int number) {
    this.number = number;
    }
    /**
  • @return the passenger
    */
    public int getPassenger() {
    return passenger;
    }
    /**
  • @param passenger the passenger to set
    */
    public void setPassenger(int passenger) {
    this.passenger = passenger;
    }
    /**
  • @return the price
    */
    public int getPrice() {
    return price;
    }
    /**
  • @param price the price to set
    */
    public void setPrice(int price) {
    this.price = price;
    }
    /**
  • @return the tonnage
    */
    public int getTonnage() {
    return tonnage;
    }
    /**
  • @param tonnage the tonnage to set
    */
    public void setTonnage(int tonnage) {
    this.tonnage = tonnage;
    }
    }

public class PassengerCar extends Car {

public PassengerCar(String name, int passenger, int price) {
    // TODO Auto-generated constructor stub
    this.setName(name);
    this.setPassenger(passenger);
    this.setPrice(price);
}
public String toString() { 
    return  this.getName()+"----"+this.getPrice()+"元/天---- 载客"+this.getPassenger()+"人"; }

}


public class PickUp extends Car {

public PickUp(String name, int passenger, int price) {
    // TODO Auto-generated constructor stub
    this.setName(name);
    this.setPassenger(passenger);
    this.setPrice(price);
}

public PickUp(String name, int passenger, int tonnage,int price) {
    // TODO Auto-generated constructor stub
    this.setName(name);
    this.setPassenger(passenger);
    this.setPrice(price);
    this.setTonnage(tonnage);;
}
public String toString() { 
    return  this.getName()+"----"+this.getPrice()+"元/天---- 载客"+this.getPassenger()+"人---"+this.getTonnage()+"吨";
    }

}


public class Truck extends Car {

public Truck(String name, int passenger, int price) {
    // TODO Auto-generated constructor stub
    this.setName(name);
    this.setPassenger(passenger);
    this.setPrice(price);
}

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
public String toString() {
    return  this.getName()+"----"+this.getPrice()+"元/天---- 载客"+this.getPassenger()+"人"; 
}

}

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