交作业~折腾了好久o(╥﹏╥)o
//父类
package com.imooc; public class Car { String name;//名字 double rent;//租金 int passengers;//载客量 double goods;//载货量 public Car(String name, double rent, int passengers, double goods){ this.name = name; this.rent = rent; this.passengers = passengers; this.goods = goods; } }
//子类
package com.imooc; public class Bus extends Car { public Bus(String name, double rent, int passengers, double goods) { super(name, rent, passengers, 0); } }
package com.imooc; public class Truck extends Car { public Truck(String name, double rent, int passengers, double goods) { super(name, rent, 0, goods); } }
package com.imooc; public class PickUp extends Car { public PickUp(String name, double rent, int passengers, double goods) { super(name, rent, passengers, goods); } }
//测试
package com.imooc; import java.util.Scanner; public class RentalSystem { static Scanner input = new Scanner(System.in); static Car[] cars = { new Bus("奥迪A4", 500, 4, 0), new Bus("马自达6", 400, 4, 0), new PickUp("皮卡雪6", 450, 4, 2), new Bus("金龙", 800, 20, 0), new Truck("松花江", 400, 0, 4), new Truck("依维柯", 1000, 0, 20) }; static int cho; static double rentPer = 0; static double renTotal = 0; static int passTotal = 0; static int goodTotal = 0; static int num; public static void main(String[] args) { System.out.println("*****欢迎使用嗒嗒租车系统*****"); isNeed(); if(cho == 1){ displayList(); rentMessage(); } } public static void isNeed(){ System.out.println("您是否要租车:1是 按其他数字键退出"); cho = input.nextInt(); if(cho != 1){ System.out.println("感谢使用,再见。"); } } public static void displayList(){ System.out.println("您可租车的类型及其价目表:"); System.out.println("序号\t汽车名称\t租金\t\t容量"); for(int i=0; i<cars.length; i++){ if(cars[i].goods == 0){ System.out.println((i+1) + "." + "\t" + cars[i].name + "\t" + cars[i].rent + "元/天" + "\t" + "载人:" + cars[i].passengers + "人"); } else if(cars[i].passengers == 0){ System.out.println((i+1) + "." + "\t" + cars[i].name + "\t" + cars[i].rent + "元/天" + "\t" + "载货:" + cars[i].goods + "吨"); } else{ System.out.println((i+1) + "." + "\t" + cars[i].name + "\t" + cars[i].rent + "元/天" + "\t" + "载人:" + cars[i].passengers + "人 " + "载货:" + cars[i].goods + "吨"); } } } public static void rentMessage(){ System.out.println("请输入您要租车的数量:"); num = input.nextInt(); String[] goodNames = new String[num]; String[] passNames = new String[num]; for(int i=0; i<num; i++){ System.out.println("请输入第" + (i+1) + "辆车的序号:"); int cur = input.nextInt(); rentPer += cars[cur-1].rent; passTotal += cars[cur-1].passengers; goodTotal += cars[cur-1].goods; if(cars[cur-1].passengers > 0){ passNames[i] = cars[cur-1].name; } if(cars[cur-1].goods > 0){ goodNames[i] = cars[cur-1].name; } } System.out.println("请输入租车天数:"); int time = input.nextInt(); renTotal = time * rentPer; System.out.println("***您的账单***"); System.out.println("--可载人的车有:"); for(String passName : passNames){ if(passName != null) System.out.println(passName); } //System.out.println(Arrays.toString(passNames)); System.out.println("共载人数:" + passTotal); System.out.println("--可载货的车有:"); //System.out.println(Arrays.toString(goodNames)); for(String goodName : goodNames){ if(goodName != null) System.out.println(goodName); } System.out.println("共载货数:" + goodTotal); System.out.println("您的账单总价为:" + renTotal + "元"); } }