码农_鑫森淼焱垚
2015-08-15 16:25
package com.imooc.item; /** * 汽车类 * @author MeRos * */ public class Car { public int rent; public String name; 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; } }
package com.imooc.item; /** * 载客车类 * * @author MeRos * */ public class PassagerCar extends Car { private int peopleCapacity; public PassagerCar(String name, int rent, int peopleCapacity) { this.name = name; this.rent = rent; this.peopleCapacity = peopleCapacity; } public int getPeopleCapacity() { return peopleCapacity; } public void setPeopleCapacity(int peopleCapacity) { this.peopleCapacity = peopleCapacity; } }
package com.imooc.item; /** * 载货车类 * * @author MeRos * */ public class Trunk extends Car { private int cargoCapacity; public Trunk(String name, int rent, int cargoCapacity) { this.name = name; this.rent = rent; this.cargoCapacity = cargoCapacity; } public int getCargoCapacity() { return cargoCapacity; } public void setCargoCapacity(int cargoCapacity) { this.cargoCapacity = cargoCapacity; } }
package com.imooc.item; /** * 皮卡车类 * * @author MeRos * */ public class Pickup extends Car { private int cargoCapacity; private int peopleCapacity; public Pickup(String name, int rent, int peopleCapacity, int cargoCapacity) { this.name = name; this.rent = rent; this.peopleCapacity = peopleCapacity; this.cargoCapacity = cargoCapacity; } public int getCargoCapacity() { return cargoCapacity; } public void setCargoCapacity(int cargoCapacity) { this.cargoCapacity = cargoCapacity; } public int getPeopleCapacity() { return peopleCapacity; } public void setPeopleCapacity(int peopleCapacity) { this.peopleCapacity = peopleCapacity; } }
package com.imooc.item; import java.util.Scanner; public class Test { public static void main(String[] args) { double totalMoney = 0; // 汽车总单价 int totalPerson = 0; // 总载人数 double totalCargo = 0; // 总载货量 StringBuffer perMessage = new StringBuffer(); // 动态字符串数组存储载人的车辆名 StringBuffer cargoMessage = new StringBuffer(); // 动态字符串数组存储载物的车辆名 Car[] cars = { new PassagerCar("奥迪A4", 500, 4), new PassagerCar("马自达6", 400, 4), new Pickup("皮卡雪", 450, 4, 2), new PassagerCar("金龙", 800, 20), new Trunk("松花江", 400, 4), new Trunk("依维柯", 1000, 20) }; System.out.println("欢迎使用嘀嘀租车系统:"); System.out.println("您是否要租车:1.是 0.否"); Scanner input = new Scanner(System.in); int isNot = input.nextInt(); if (isNot == 1) { System.out.println("您可租车的类型及其价目表:"); System.out.println("序号\t汽车名称\t租金\t\t容量"); int i = 1; for (Car currentCar : cars) { // 遍历输出所有车辆信息 if (currentCar instanceof PassagerCar) { System.out.println(i + "." + "\t" + currentCar.getName() + "\t" + currentCar.getRent() + "元/天 " + "\t" + "载人:" + ((PassagerCar) currentCar).getPeopleCapacity() + "人"); } if (currentCar instanceof Trunk) { System.out.println(i + "." + "\t" + currentCar.getName() + "\t" + currentCar.getRent() + "元/天 " + "\t" + "载货:" + ((Trunk) currentCar).getCargoCapacity() + "吨"); } if (currentCar instanceof Pickup) { System.out.println(i + "." + "\t" + currentCar.getName() + "\t" + currentCar.getRent() + "元/天 " + "\t" + "载人:" + ((Pickup) currentCar).getPeopleCapacity() + "人 载货:" + ((Pickup) currentCar).getCargoCapacity() + "吨"); } i++; } System.out.println("请输入您要租汽车的数量:"); int num = input.nextInt(); for (int j = 0; j < num; j++) { System.out.println("请输入第" + (j + 1) + "辆车的序号:"); int serialNum = input.nextInt(); boolean checkNum = false; while (!checkNum) { for (int k = 0; k < cars.length; k++) { if (serialNum == k + 1) { checkNum = true; break; } } if (!checkNum) { System.out.println("您输入的序号不存在,请重新输入:"); serialNum = input.nextInt(); } } serialNum = serialNum - 1; // 获取不同车型的相关信息 if (cars[serialNum] instanceof PassagerCar) { totalPerson += ((PassagerCar) cars[serialNum]) .getPeopleCapacity(); totalMoney += ((PassagerCar) cars[serialNum]).getRent(); perMessage.append(cars[serialNum].getName() + " "); } if (cars[serialNum] instanceof Trunk) { totalCargo += ((Trunk) cars[serialNum]).getCargoCapacity(); totalMoney += ((Trunk) cars[serialNum]).getRent(); cargoMessage.append(cars[serialNum].getName() + " "); } if (cars[serialNum] instanceof Pickup) { totalPerson += ((Pickup) cars[serialNum]) .getPeopleCapacity(); totalCargo += ((Pickup) cars[serialNum]).getCargoCapacity(); totalMoney += ((Pickup) cars[serialNum]).getRent(); perMessage.append(cars[serialNum].getName() + " "); cargoMessage.append(cars[serialNum].getName() + " "); } } System.out.println("请输入租车天数:"); int days = input.nextInt(); System.out.println("您的账单:"); System.out.println("**可载人的车有:"); System.out.println(perMessage + "\t共载人:" + totalPerson + "人"); System.out.println("**可载货的车有:"); System.out.println(cargoMessage + "\t共载货:" + totalCargo + "吨"); System.out.println("**租车总价格:" + (totalMoney * days) + "元"); } } }
做的很不错。我只做了一部分 ╮(╯▽╰)╭。
在哪可以找到Java界面编程的视频?
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题
回答 11
回答 3