运行效果
package com.xt.car;
public abstract class Car {
private String name;
private int price;
public Car(String name, int price) {
super();
this.name = name;
this.price = price;
}
public abstract String getCapacity();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
PassengerCar.java
package com.xt.car;
import com.xt.car.Interface.IMen;
public class PassengerCar extends Car implements IMen {
private int seat;
public PassengerCar(String name, int price, int seatCount) {
super(name, price);
seat = seatCount;
}
public void setSeatCount(int count) {
seat = count;
}
public int getSeatCount() {
return seat;
}
public String getCapacity() {
return "载人:" + seat + "人";
}
}
TruckCar.java
package com.xt.car;
import com.xt.car.Interface.IGoods;
public class TruckCar extends Car implements IGoods{
private int weight;
public TruckCar(String name, int price, int weight) {
super(name, price);
this.weight = weight;
}
public int getGoodsWeight() {
return weight;
}
public void setGoodsWeight(int weight) {
this.weight = weight;
}
public String getCapacity() {
return "载重:" + weight + "吨";
}
}
pikCar.java
package com.xt.car;
import com.xt.car.Interface.IGoods;
import com.xt.car.Interface.IMen;
public class pikCar extends Car implements IGoods,IMen{
private int count;
private int weight;
public pikCar(String name, int price, int count, int weight) {
super(name, price);
this.count = count;
this.weight = weight;
}
public void setSeatCount(int count) {
this.count = count;
}
public int getSeatCount() {
return count;
}
public int getGoodsWeight() {
return weight;
}
public void setGoodsWeight(int weight) {
this.weight = weight;
}
public String getCapacity() {
return "载人:" + count + "人,载重:" + weight + "吨";
}
}
IGoods.java
package com.xt.car.Interface;
public interface IGoods {
public int getGoodsWeight();
public void setGoodsWeight(int weight);
}
`
IMen.java
package com.xt.car.Interface;
public interface IMen {
public void setSeatCount(int count);
public int getSeatCount();
}
Init.java
package com.xt.car;
import java.util.Scanner;
import com.xt.car.Interface.IGoods;
import com.xt.car.Interface.IMen;
public class Init {
public static final Car[] CARS = { new PassengerCar("奥迪A4", 500, 4),
new PassengerCar("马自达6", 400, 4), new pikCar("皮卡雪6", 450, 4, 2),
new PassengerCar("金龙", 800, 20), new TruckCar("松花江", 400, 4),
new TruckCar("依维柯", 1000, 20)
};
static int choose[] = new int[100];
static int[] no = new int[100];
static int[] days = new int[100];
static int count = 0;
public static void main(String[] args) {
showlist();
choosecar();
}
private static void showresult() {
System.out.println("您选租的车辆信息如下:");
int totleprice = 0;
int man = 0;
int weight = 0;
for (int i = 0; i < count; i++) {
int price = (CARS[choose[i]-1]).getPrice() * no[i] * days[i];
if (CARS[choose[i]-1] instanceof IMen) {
man = ((IMen) CARS[choose[i]-1]).getSeatCount();
}
if (CARS[choose[i]-1] instanceof IGoods) {
weight = ((IGoods) CARS[choose[i]-1]).getGoodsWeight();
}
System.out.printf("%s\t%d辆\t%d天\t载客量%d人 \t载货量%d吨\t单价%d元/辆/天\t总价%d%n",
CARS[choose[i]-1].getName(), no[i], days[i], man, weight,
CARS[choose[i]-1].getPrice(),price);
totleprice += price;
}
System.out.println("您选租的车辆总价格:" + totleprice + "元");
}
public static void choosecar() {
System.out.println("欢迎使用答答租车系统!");
for (int i = 0;; i++) {
Scanner scanner = new Scanner(System.in);
System.out.println("您是否继续租车:1是;0否");
int yesOrNo = scanner.nextInt();
if (yesOrNo == 1) {
System.out.println("请输入您要租车型的序号:");
while (true) {
choose[i] = scanner.nextInt();
if (1 > choose[i]||choose[i] > 6) {
showlist();
System.out.println("选择错误,请重新选择!");
} else {
System.out.printf("请输入您要租%d号车型的数量:%n", choose[i]);
no[i] = scanner.nextInt();
System.out.printf("请输入您要租%d号车型的天数:%n", choose[i]);
days[i] = scanner.nextInt();
count++;
System.out.println("选择完成!");
break;
}
}
} else if (yesOrNo == 0){
if (count == 0) {
System.out.println("未选择车辆,退出!");
break;
} else{
System.out.println("选择车辆完成!");
showresult();
break;
}
}else {
System.out.println("输入错误!请重新输入");
}
}
}
public static void showlist() {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t 汽车名称\t 租金\t\t 容量");
for (int i = 0; i < CARS.length; i++) {
System.out.printf("%d\t %s\t %d元/天\t %s%n", (i + 1),
CARS[i].getName(), CARS[i].getPrice(),
CARS[i].getCapacity());
}
}
}