参考其他同学以后写的...(api我还不太理解, 所以就没使用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | //汽车父类 public abstract class Car { // 基本变量 int number; // 汽车序号 String name; // 汽车名称 double cargoCap; // 载货量 int peopleCap; // 载人量 int rent; // 租金 //基本方法 public String getName() { //获得名称 return name; } public int getRent() { //获得租金 return rent; } public double getCargo(){ //获得载货量 return cargoCap; } public int getPeople() { //获得载人量 return peopleCap; } public int getNumber() { //获得车辆序号 return number; } public abstract String getType(); //获得车辆类型的抽象方法 public String carInfo(){ // 获得可阅读的车辆信息 String result = this .getNumber() + ". 汽车名称: " + this .getName() + ", 汽车类型: " + this .getType() + ", 租金: " + this .getRent() + "元/天, " ; if ( this .getType() == "轿车" ) { result += "载人数: " + this .getPeople() + "人" ; } else if ( this .getType() == "货车" ) { result += "载货量: " + this .getCargo() + "顿" ; } else { result += "载人数: " + this .getPeople() + "人, 载货量: " + this .getCargo() + "顿" ; } return result; } public String toString() { //以车辆名称覆盖默认print方式 return this .name; } } //货车子类 public class Truck extends Car { public Truck( int number, String name, int rent, double cargoCap) { this .number = number; this .name = name; this .rent = rent; this .cargoCap = cargoCap; } @Override public String getType() { // TODO Auto-generated method stub return "货车" ; } //轿车子类 public class Normal extends Car { public Normal( int number, String name, int rent, int peopleCap) { this .number = number; this .name = name; this .rent = rent; this .peopleCap = peopleCap; } @Override public String getType() { // TODO Auto-generated method stub return "轿车" ; } } //皮卡子类 public class PickUp extends Car { public PickUp( int number, String name, int rent, int peopleCap, double cargoCap){ this .number = number; this .name = name; this .rent = rent; this .peopleCap = peopleCap; this .cargoCap = cargoCap; } @Override public String getType() { // TODO Auto-generated method stub return "皮卡" ; } } import java.util.Scanner; // main方法 public class init { public static void main(String[] args) { // 所有可租车辆 Car[] allRent = { new Normal( 1 , "奥迪A4" , 500 , 4 ), new Normal( 2 , "马自达6" , 400 , 4 ), new PickUp( 3 , "皮卡雪6" , 450 , 4 , 2.0 ), new Normal( 4 , "金龙" , 800 , 20 ), new Truck( 5 , "松花江" , 400 , 4.0 ), new Truck( 6 , "依维河" , 1000 , 20.0 )}; System.out.println( "欢迎光临didi打车" ); Scanner input = new Scanner(System.in); int choice; do { System.out.println( "是否继续? 0:退出, 1:继续" ); choice = input.nextInt(); } while (choice != 0 && choice != 1 ); if (choice == 0 ) { System.out.println( "很遗憾, 期待您的下次光临" ); return ; } System.out.println( "我们提供如下车辆:" ); for ( int i = 0 ; i < allRent.length; i++) { System.out.println(allRent[i].carInfo()); } System.out.println( "请问您一共需要几辆汽车? (请输入数字)" ); int numberCar = input.nextInt(); Car[] picking = new Car[numberCar]; for ( int i = 0 ; i < numberCar; i++) { do { System.out.println( "请输入您的第 " + (i + 1 ) + " 辆车的序号? " + "(请输入1-" + allRent.length + ")" ); choice = input.nextInt(); } while (choice < 1 || choice > 6 ); picking[i] = allRent[choice - 1 ]; } System.out.println( "您需要使用的天数: " ); int days = input.nextInt(); int totalRent = 0 , totalCargo = 0 , totalPeople = 0 ; System.out.println( "========您的租赁信息如下: ========" ); System.out.println( "您的租赁天数为: " + days + "天" ); System.out.print( "租赁的车辆: " ); for ( int i = 0 ; i < numberCar; i++) { System.out.print(picking[i] + " " ); totalRent += days * picking[i].getRent(); totalCargo += picking[i].getCargo(); totalPeople += picking[i].getPeople(); } System.out.println( "" ); System.out.println( "总计租金为: " + totalRent + "元" ); System.out.println( "总计载人数为: " + totalPeople + "人" ); System.out.println( "总计载货量为: " + totalCargo + "顿" ); System.out.println( "谢谢使用租车系统, 欢迎下次光临!" ); return ; } } |