package com.imooc;
public abstract class Vehicle {
public abstract String function();
public abstract int getPrice();
public abstract String getBrand();
}
package com.imooc;
public class Car extends Vehicle {
private int price;
private int amount;
private String brand;
public Car(String brand,int amount,int price){
this.brand = brand;
this.amount = amount;
this.price = price;
}
public String function()
{
return (brand+"\t"+amount+"人/辆\t"+0+"吨/辆\t"+price+"元/天");
}
public int getPrice(){
return price;
}
public String getBrand(){
return brand;
}
}
package com.imooc;
public class Truck extends Vehicle {
private int price;
private int amount;
private String brand;
public Truck(String brand,int amount,int price){
this.brand = brand;
this.amount = amount;
this.price = price;
}
public String function() {
// TODO Auto-generated method stub
return (brand+"\t"+0+"人/辆\t"+amount+"吨/辆\t"+price+"元/天");
}
public int getPrice(){
return price;
}
public String getBrand(){
return brand;
}
}
package com.imooc;
public class Mpv extends Vehicle {
private int price;
private int amount;
private int weight;
private String brand;
public Mpv(String brand,int amount,int weight,int price){
this.brand = brand;
this.amount = amount;
this.weight = weight;
this.price = price;
}
@Override
public String function() {
// TODO Auto-generated method stub
return (brand+"\t"+amount+"人/辆\t"+weight+"吨/辆\t"+price+"元/天");
}
public int getPrice(){
return price;
}
public String getBrand(){
return brand;
}
}
package com.imooc;
import java.util.Scanner;
public class InitialDada {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用哒哒租车系统:");
System.out.println("您是否需要使用哒哒租车:1是 0否");
Scanner keyboard = new Scanner(System.in);
int answer = keyboard.nextInt();
while((answer!=0)&&(answer!=1)){
System.out.println("输入有误,请重新输入");
answer = keyboard.nextInt();
}
if(answer == 1)
{
Vehicle []v = {new Car("奥迪",4,500),new Car("福特",4,300),new Car("哈弗",7,400),new Truck("东风",3,600),new Truck("重卡",6,650),new Mpv("依维柯",4,1,500)};
/****输出租车系统提供用户的租车信息***/
System.out.println("以下是可以提供的租车信息:");
System.out.println("序号\t"+"品牌 \t"+"载人\t"+"载货\t"+"租金\t");
for(int i=0;i<6;i++){
System.out.println((i+1)+".\t"+v[i].function());
}
/****用户输入租车信息,并保存在系统中***/
System.out.println("请输入需要租车的种类:");
int number = keyboard.nextInt();
int sequence[] = new int[number];
int day[] = new int[number];
int total=0;
for(int i = 0; i < number; i++)
{
System.out.println("请输入第"+(i+1)+"种车的序号:");
sequence[i] = keyboard.nextInt();
while((sequence[i]>6)||(sequence[i]<0)){
System.out.println("输入有误,请重新输入!提醒:输入的数字在0~6之间");
sequence[i] = keyboard.nextInt();
}
System.out.println("请输入租用该车的天数:");
day[i] = keyboard.nextInt();
total = total+(v[sequence[i]-1].getPrice())*day[i];
}
/*****计算并输出用户的租车具体情况**/
System.out.println("你的账单信息如下:");
System.out.println("****车辆信息:");
System.out.println("载人的汽车有:");
int flag1 = 0;
int flag2 = 0;
for(int i=0;i<number;i++)
{
if(sequence[i]<=3){
System.out.print(v[i].getBrand()+" ");
flag1++;
}
}
if(flag1 == 0){
System.out.println("未租用载货汽车");
}
System.out.println();
System.out.println("载货的汽车有:");
for(int i=0;i<number;i++)
{
if((sequence[i]>3)&&(sequence[i]<=6)){
flag2++;
System.out.print(v[i].getBrand()+" ");
}
}
if(flag2 == 0){
System.out.println("未租用载货汽车");
}
System.out.println();
System.out.println("****租车总金额:"+total);
}
else{
System.out.println("即将退出哒哒租车系统!");
}
}
}
测试效果:
欢迎使用哒哒租车系统:
您是否需要使用哒哒租车:1是 0否
1
以下是可以提供的租车信息:
序号 品牌 载人 载货 租金
- 奥迪 4人/辆 0吨/辆 500元/天
- 福特 4人/辆 0吨/辆 300元/天
- 哈弗 7人/辆 0吨/辆 400元/天
- 东风 0人/辆 3吨/辆 600元/天
- 重卡 0人/辆 6吨/辆 650元/天
- 依维柯 4人/辆 1吨/辆 500元/天
请输入需要租车的种类:
2
请输入第1种车的序号:
1
请输入租用该车的天数:
13
请输入第2种车的序号:
2
请输入租用该车的天数:
3
你的账单信息如下:
****车辆信息:
载人的汽车有:
奥迪 福特
载货的汽车有:
未租用载货汽车
****租车总金额:7400