第一天写,输出已选择的车功能未实现。然后参考了几位同学的代码,写出来了,在此贴上链接。
MikaMika:http://www.imooc.com/article/6381
小尾巴都糊了:http://www.imooc.com/article/9507
charsandrew:http://www.imooc.com/article/10181
ajjrx丶3647181:http://www.imooc.com/article/10379
Car父类
package com.kkk;
public class Car {
private String name;
private int money;
private int man;
private int huo;
public Car(String name, int money, int man, int huo) {// ??????????????????
// TODO Auto-generated constructor stub
this.huo = huo;
this.name = name;
this.money = money;
this.man = man;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getMan() {
return man;
}
public void setMan(int man) {
this.man = man;
}
public int getHuo() {
return huo;
}
public void setHuo(int huo) {
this.huo = huo;
}
@Override
public String toString() {
return " " + name + " 单价" + money + "元/天, 载人数" + man + "人, 载货量"
+ huo + "吨";
}
}
载客车-子类
package com.kkk;
public class ManCar extends Car {
public ManCar(String name, int money, int man) {
super(name, money, man, 0);
}
}
载货车-子类
package com.kkk;
public class HuoCar extends Car {
public HuoCar(String name, int money, int huo) {
super(name, money, 0, huo);
}
}
皮卡-子类
package com.kkk;
public class TwoCar extends Car {
public TwoCar(String name, int money, int man, int huo) {
super(name, money, man, huo);
}
}
主函数
package com.kkk;
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
Car[] cars = { new ManCar("奥迪A4", 500, 4), new ManCar("马自达6", 400, 4),
new TwoCar("皮卡雪6", 450, 4, 2), new ManCar("金龙", 800, 20),
new HuoCar("松花江", 400, 4), new HuoCar("依维柯", 1000, 20) };
System.out.println("欢迎来大大租车系统\n请确定您是否要租车:1.是 2.否");
Scanner input = new Scanner(System.in);
int yes = input.nextInt();
if (yes == 1) {
System.out.println("可租车价目表");
for (int i = 0; i < 6; i++) {
System.out.println(i + 1 + "." + cars[i].toString());
}
System.out.println("您要租几辆车?");
int number = input.nextInt();
System.out.println("您想租几天?");
int days = input.nextInt();
int[] num = new int[number];
for (int j = 0; j < number; j++) {
System.out.println("*****请输入您想租的第" + (j + 1) + "辆车的序号*****");
num[j] = input.nextInt();
if (num[j] > 6 num[j] < 0) {
System.out.println("请输入1~6的数字");//这里如果输入错误程序还是会出错,暂时不知道如何解决
}
}
System.out.println("您租的可载人的车有:");//如果选了两辆可载人的车会输出两次,这是一个问题,暂时不知道如何修改
int manNumber = 0;
int price1 = 0;
for (int i = 0; i < number; i++) {
if ((cars[num[i] - 1] instanceof ManCar)
(cars[num[i] - 1] instanceof TwoCar)) {
manNumber += cars[num[i] - 1].getMan();
System.out.print(cars[num[i] - 1].getName() + " ");
price1 += cars[num[i] - 1].getMoney();
}
}
System.out.println("\n您租的可载货的车有:");
int huoNumber = 0;
int price2 = 0;
for (int i = 0; i < number; i++) {
if ((cars[num[i] - 1] instanceof HuoCar)
(cars[num[i] - 1] instanceof TwoCar)) {
huoNumber += cars[num[i] - 1].getHuo();
System.out.print(cars[num[i] - 1].getName() + " ");
price2 += cars[num[i] - 1].getMoney();
}
}
int twoNum=0;
for(int i=0;i<number;i++){//皮卡的价格加了两次,这里算租了几辆皮卡,最后算价格时减去
if(cars[num[i] - 1] instanceof TwoCar){
twoNum++;
}
}
System.out.println("\n您选择的车共可载人:" + manNumber + "人");
System.out.println("您选择的车共可载货:" + huoNumber + "吨");
System.out.println("您共需付款:" + ((price1 + price2-twoNum*450) * days));
System.out.println("欢迎下次再来租车!");
} else {
System.out.println("下次您想租车还可以找我们!");
}
}
}
点击查看更多内容
4人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦