-------------------------------------------------
public class Car {
public String Name = "";
public int Rent = 0;
public int FreightVolume = 0;//载物
public int CarryinCapacity = 0;//载人
}
--------------------------------------------
public class PassengerCar extends Car{
public PassengerCar(String name,int rent,int ca) {
Name = name;
Rent = rent;
CarryinCapacity = ca;
}
}
-------------------------------------
public class Truck extends Car{
public Truck(String name,int rent,int fr) {
Name = name;
Rent = rent;
FreightVolume = fr;
}
}
-----------------------------------------------------
public class PickUp extends Car {
public PickUp(String name,int rent,int ca,int fr) {
Name = name;
Rent = rent;
CarryinCapacity = ca;
FreightVolume = fr;
}
}
--------------------------------------------------------
import java.util.Scanner;
public class Initail {
public static void main(String[] args) {
Car[] cars = {new PassengerCar("奥迪A4",500,4),
new PassengerCar("马自达6",400,4),
new PickUp("皮卡雪6",450,4,2),
new PassengerCar("金龙",800,20),
new Truck("松花江",400,4),
new Truck("伊维科",1000,20)};
System.out.println("欢迎使用答答租车系统!");
System.out.println("您是否要租车:1是 0否");
Scanner scan = new Scanner(System.in);
String input = scan.next();
if(input.equals("1")) {
System.out.println("您可以租车的类型及价目表:");
System.out.println("序号\t汽车\t租金\t容量");
int a = 1;//汽车的序号
for(Car car : cars) {
if(car instanceof PassengerCar) {
System.out.println("" + a + "\t" + car.Name + "\t" + car.Rent + "元/天" + "\t" + "载人:" + car.CarryinCapacity + "人");
}
if(car instanceof Truck) {
System.out.println("" + a + "\t" + car.Name + "\t" + car.Rent + "元/天" + "\t" + "载货:" + car.FreightVolume + "吨");
}
if(car instanceof PickUp) {
System.out.println("" + a + "\t" + car.Name + "\t" + car.Rent + "元/天" + "\t" + "载人:" + car.CarryinCapacity + "人" + " " + "载货:" + car.FreightVolume + "吨");
}
a++;
}
System.out.println("请输入您要租汽车的数量:");
int num = scan.nextInt();
int[] count = new int[10];
for(int i = 0;i < num;i++) {
int b = i + 1;
System.out.println("请输入第" + b + "辆车的序号");
count[i] = scan.nextInt() - 1;//序号是从1开始,而数组是从0开始
}
System.out.println("请输入租车天数:");
int day = scan.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
int allcc = 0;//共载人
int allfv = 0;//共载货
int price = 0;//总价格
for(int i = 0;i < num;i++) {
if(cars[count[i]] instanceof PassengerCar || cars[count[i]] instanceof PickUp) {
//count[i]保存的是车的序号,cars[count[i]]就是第i+1辆车
System.out.print("\t" + cars[count[i]].Name);
allcc = cars[count[i]].CarryinCapacity + allcc;
price = cars[count[i]].Rent + price;
}
}
System.out.println("\t" + "共载人:" + allcc + "人");
System.out.println("***载货的车有:");
for(int i = 0;i < num;i++) {
if(cars[count[i]] instanceof Truck || cars[count[i]] instanceof PickUp) {
System.out.print("\t" + cars[count[i]].Name);
allfv = cars[count[i]].FreightVolume + allfv;
if(cars[count[i]] instanceof Truck) {//如果是皮卡,则价格已经在前面统计了
price = cars[count[i]].Rent + price;
}
}
}
System.out.println("\t" + "共载货:" + allfv + "吨");
price = price * day;
System.out.println("***租车总价格为:" + price + "元");
}
else {
System.exit(0);
}
scan.close();
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章