首先创建一个父类 car
public abstract class Car {
public String name;// 车的名字
public double cargo;// 车的载货量
public int people;// 车的载客量
public int rent;// 车的租金
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCargo() {
return cargo;
}
public void setCargo(double cargo) {
this.cargo = cargo;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
}
然后是子类 AoDiA4
public class AoDiA4 extends Car {
int people;
int rent;
String name;
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AoDiA4(String name, int rent, int people) {
this.name = name;
this.rent = rent;
this.people = people;
}
}
继续子类 MaZiDa6
public class MaZiDa6 extends Car {
int people;
int rent;
String name;
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MaZiDa6(String name, int rent, int people) {
this.name = name;
this.rent = rent;
this.people = people;
}
}
子类PiKaXue6
public class PiKaXue6 extends Car {
int people;
int rent;
double cargo;
String name;
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public double getCargo() {
return cargo;
}
public void setCargo(int cargo) {
this.cargo = cargo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PiKaXue6(String name, int rent, int people, int cargo) {
this.name = name;
this.rent = rent;
this.people = people;
this.cargo = cargo;
}
}
子类JinLong
public class JinLong extends Car {
int people;
int rent;
String name;
public JinLong(String name, int rent, int people) {
this.name = name;
this.rent = rent;
this.people = people;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
子类SongHuaJiang
public class SongHuaJiang extends Car {
int rent;
double cargo;
String name;
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public double getCargo() {
return cargo;
}
public void setCargo(int cargo) {
this.cargo = cargo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SongHuaJiang(String name, int rent, int cargo) {
this.name = name;
this.rent = rent;
this.cargo = cargo;
}
}
子类 YiWeiKe
public class YiWeiKe extends Car {
int rent;
double cargo;
String name;
public YiWeiKe(String name, int rent, int cargo) {
this.name = name;
this.rent = rent;
this.cargo = cargo;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public double getCargo() {
return cargo;
}
public void setCargo(int cargo) {
this.cargo = cargo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
终于到main方法了。。
import java.util.Scanner;
public class Test {
public static Car[] cars = { new AoDiA4("奥迪A4", 500, 4), new MaZiDa6("马自达6", 400, 4),new PiKaXue6("皮卡雪6", 450, 4, 2), new JinLong("金龙", 800, 20), new SongHuaJiang("松花江", 400, 4),new YiWeiKe("依维柯", 1000, 20) };// 创建所有车类的数组并赋于属性
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1:是 0:否");
entrySystem();// 判断是否进入系统
printCars();// 打印可租车列表
int carNum = getCarNum();// 获取用户租车数量
int[] carNums = getCarNums(carNum);// 获取用户租车的序号列表
int days = getDays();// 获取用户租车时长
detailedList(days, carNum, carNums);// 计算清单
}
public static void entrySystem() {
for (;;) {
int z = sc.nextInt();
if (z != 0 & z != 1) {
System.out.println("输入错误,请重新输入!");
continue;
} else if (z == 0) {
System.out.println("感谢使用,再见!");
System.exit(0);
} else if (z == 1) {
break;
}
}
}
public static void printCars() {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
int num = 1;
for (Car newCar : cars) {// 对所有车进行一次循环遍历,既foreach(),来创建一个汽车一览表
if (newCar instanceof AoDiA4) {// 判断正在遍历的car是否是奥迪A4类
System.out.println(num + ".\t" + newCar.getName() + "\t" + newCar.getRent() + "元/天" + "\t" + "载人:"+ newCar.getPeople() + "人");
num++;
}
if (newCar instanceof MaZiDa6) {
System.out.println(num + ".\t" + newCar.getName() + "\t" + newCar.getRent() + "元/天" + "\t" + "载人:"+ newCar.getPeople() + "人");
num++;
}
if (newCar instanceof PiKaXue6) {
System.out.println(num + ".\t" + newCar.getName() + "\t" + newCar.getRent() + "元/天" + "\t" + "载人:"+ newCar.getPeople() + "人,载货" + newCar.getCargo() + "吨");
num++;
}
if (newCar instanceof JinLong) {
System.out.println(num + ".\t" + newCar.getName() + "\t" + newCar.getRent() + "元/天" + "\t" + "载人:"+ newCar.getPeople() + "人");
num++;
}
if (newCar instanceof SongHuaJiang) {
System.out.println(num + ".\t" + newCar.getName() + "\t" + newCar.getRent() + "元/天" + "\t" + "载货:"+ newCar.getCargo() + "吨");
num++;
}
if (newCar instanceof YiWeiKe) {
System.out.println(num + ".\t" + newCar.getName() + "\t" + newCar.getRent() + "元/天" + "\t" + "载货:"+ newCar.getCargo() + "吨");
num++;
}
}
}
public static int getCarNum() {
System.out.println("请输入要租汽车的数量:");
return sc.nextInt();
}
public static int[] getCarNums(int carNum) {
int[] arr = new int[carNum];
int y = 0;
for (int x = 1; x <= carNum; x++) {
System.out.println("请输入第" + x + "辆车的序号:");
arr[y] = sc.nextInt();
y++;
}
return arr;
}
public static int getDays() {
System.out.println("请输入租车的天数:");
return sc.nextInt();
}
public static void detailedList(int Days, int num, int[] nums) {
int people = 0;
double cargo = 0;
int rent = 0;
System.out.println("您的账单:\n***可载人的车有:");
for (int i = 0; i < nums.length; i++) {
if ((cars[nums[i] - 1] instanceof AoDiA4) || (cars[nums[i] - 1] instanceof MaZiDa6) || (cars[nums[i] - 1] instanceof PiKaXue6) || (cars[nums[i] - 1] instanceof JinLong)) {
System.out.print(cars[nums[i] - 1].getName() + " ");
people = people + cars[nums[i] - 1].getPeople();
}
}
System.out.println("共可载人:" + people + "人!");
System.out.println("***可载货的车有:");
for (int i = 0; i < nums.length; i++) {
if ((cars[nums[i] - 1] instanceof PiKaXue6) || (cars[nums[i] - 1] instanceof SongHuaJiang) || (cars[nums[i] - 1] instanceof YiWeiKe)) {
System.out.print(cars[nums[i] - 1].getName() + " ");
cargo = cargo + cars[nums[i] - 1].getCargo();
}
}
System.out.println("共可载货:" + cargo + "吨!");
for (int i = 0; i < nums.length; i++) {
rent = rent + cars[nums[i] - 1].getRent();
}
rent = rent * Days;
System.out.println("***租车总价格:" + rent + "元!");
}
}
点击查看更多内容
18人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦