//汽车父类
public abstract class Car {
public int passenageNumber;//载客量
public double carryNumber;//载货量
//public int rentNumber;//租车数量
public String name;//车型
public int money;//租金
public int id;//序号
public abstract void Print();
}
//可载人载货的子类
public class PickUp extends Car {
public PickUp(int id,String name,int money,int passN,double carrN){
this.id = id;
this.name = name;
this.money = money;
this.passenageNumber = passN;
this.carryNumber = carrN;
}
public void Print()
{ // TODO Auto-generated method stub
System.out.println(id+"\t"+name+"\t"+money+"元/ 天"+"\t"+"\t"+passenageNumber+"人/辆"+ "\t"+carryNumber+"吨/车");
}
}
//载人子类
public class PassCar extends Car {
public PassCar(int id,String name,int money,int passN){
this.id = id;
this.name = name;
this.money = money;
this.passenageNumber = passN;
}
public void Print() { // TODO Auto-generated method stub
System.out.println(id+"\t"+name+"\t"+money+"元/天"+"\t"+"\t"+passenageNumber+"人/辆"+"\t");
}
}
//载货子类
public class LorryCar extends Car {
public LorryCar(int id,String name,int money,double carrN){
this.id = id;
this.name = name;
this.money = money;
this.carryNumber = carrN;
}
public void Print() {
// TODO Auto-generated method stub
System.out.println(id+"\t"+name+"\t"+money+"元/天"+"\t"+"\t"+carryNumber+"吨/车");
}
}
//测试类
import java.util.*;
public class Test {
public static void main(String[] args) {
Car c[] = { new PassCar(1, "奥迪A4", 500, 4),
new PassCar(2, "宝马", 600, 4),
new PickUp(3, "皮卡雪6", 450, 4, 2),
new PassCar(4, "金龙", 800, 20),
new LorryCar(5, "松花江", 500, 4),
new LorryCar(6, "依维河", 1000, 10)
};
System.out.println("---欢迎使用哒哒租车---");
System.out.println("您是不是要租车:1:是"+"\t"+"2:否");
try{ Scanner input = new Scanner(System.in);
int h = input.nextInt();
//判断输入信息是否有效
while(h!=1&&h!=2){
System.out.println("请输入有效的选项! 您是否要租车: 1 是 2 否");
int d = input.nextInt();
if(d==1||d==2)
break;
}
if(h==1){
System.out.println("您可租车的价目表:");
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"\t"+"容量");
//循坏遍历租车情况
for(int i=0 ; i < c.length;i++){
c[i].Print(); }
System.out.println("请输入您需要租车的数量:");
int num = input.nextInt();//z租车数量
int sum = 0;//合计租金
int sumr = 0;//合计人数
int sumt = 0;//合计载货量
int ID[] = new int[num];//定义一个新的数组。用与存放键盘输入的租车信息
for(int i = 0;i<num;i++){//循坏保存用户需要的租车序号存入数组
System.out.println("请输入第"+(i+1)+"辆车的序号");
int id = input.nextInt();
sum += c[id-1].money;
sumr += c[id-1].passenageNumber;
sumt += c[id-1].carryNumber;
ID[i] = id; }
System.out.println("请输入租车天数:");
int day = input.nextInt();
System.out.println("您的账单:");
System.out.println("******可载人的车有:");
for(int i = 0;i<num;i++){
if(c[ID[i]-1].passenageNumber!=0){
System.out.println("\t"+c[ID[i]-1].name+"\t");//载人
}
}
System.out.println("共载入:"+sumr+"人");
System.out.println();
System.out.println("*****可载货的车:");
for(int i =0;i<num;i++){
if(c[ID[i]-1].carryNumber!=0){
System.out.println("\t"+c[ID[i]-1].name+"\t");
}
}
System.out.println("共载货:"+sumt+"吨");
System.out.println("*****总租金:"+sum*day+"元");
System.out.println("是否继续租车:1 继续 2:退出");
int choose = input.nextInt();
if(choose == 2){
break;
}
}else if(h==2){
System.out.println("欢迎您的使用!");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
打开App,阅读手记