Car.java
package com.kj;
public class Car {
public int Carnum;
public String Carname;//汽车名字
public int Carvalue;//汽车价格
public int loadpeople;//载客量
public int loadgoods;//载货量
//获取车的编号
public int getCarnum(){
return Carnum;
}
//获取车的名字
public String getCarname(){
return Carname;
}
//获取车的价格
public int getCarvalue(){
return Carvalue;
}
//获取车的载客量
public int getloadpeople(){
return loadpeople;
}
//获取车的载货量
public int getloadgoods(){
return loadgoods;
}
}
peoplecar.java
package com.kj;
public class peoplecar extends Car {
public peoplecar(int Carnum,String Carname,int Carvalue,int Carloadpeople){
this.Carnum=Carnum;
this.Carname=Carname;
this.Carvalue=Carvalue;
this.loadpeople=Carloadpeople;
}
}
goodscar.java
package com.kj;
public class goodscar extends Car {
public goodscar(int Carnum,String Carname,int Carvalue,int Carloadgoods){
this.Carnum=Carnum;
this.Carname=Carname;
this.Carvalue=Carvalue;
this.loadgoods=Carloadgoods;
}
}
pickup.java
package com.kj;
public class pickup extends Car {
public pickup(int Carnum,String Carname,int Carvalue,int Carloadpeople,int Carloadgoods){
this.Carnum=Carnum;
this.Carname=Carname;
this.Carvalue=Carvalue;
this.loadpeople=Carloadpeople;
this.loadgoods=Carloadgoods;
}
}
initial.java
package com.kj;
import java.util.Scanner;
public class initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
//定义数组,实例化车的对象
Car[] car={
new peoplecar(1,"奥迪A4",500,4),
new peoplecar(2,"马自达6",400,4),
new pickup(3,"皮卡雪6",450,4,2),
new peoplecar(4,"金龙",800,20),
new goodscar(5,"松花江",400,4),
new goodscar(6,"依维柯",1000,20)
};
Scanner input =new Scanner(System.in);
int num =input.nextInt();
if(num == 0){
System.out.println("不租车,正在退出系统!");
System.exit(0);
}
if(num != 1){
System.out.println("输入有误,正在退出系统!");
System.exit(0);
}
if(num == 1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
System.out.println("1 奥迪A4 500元/天 载人:4人");
System.out.println("2 马自达6 400元/天 载人:4人");
System.out.println("3 皮卡雪6 450元/天 载人:2人 载货:4吨");
System.out.println("4 金龙 400元/天 载人:20人");
System.out.println("5 松花江 400元/天 载货:4吨");
System.out.println("6 依维柯 1000元/天 载货:20吨");
System.out.println("请输入您要租汽车的数量:");
int num1=input.nextInt();
int price=0;
Car[] carlist=new Car[num1];
for(int i=0;i<num1;i++){
System.out.println("请输入第"+ (i+1) +"辆车的序号:");
int j=input.nextInt();
carlist[i]=car[j-1];
price+=carlist[i].getCarvalue();
}
System.out.println("请输入租车天数:");
int days=input.nextInt();
price*=days;//租车价格
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
int loadpeoplenum=0;
for(int i=0;i<num1;i++){
if(carlist[i] instanceof peoplecar carlist[i] instanceof pickup){//如果租的车辆里有peoplecar类或pickup类,就可以载人
System.out.print(carlist[i].getCarname() + "\t");
loadpeoplenum+=carlist[i].getloadpeople();
}
}
System.out.println("共载人:" + loadpeoplenum + "人");
System.out.println("载货的车有:");
int loadgoodsnum=0;
for(int i=0;i<num1;i++){
if(carlist[i] instanceof goodscar carlist[i] instanceof pickup){////如果租的车辆里有goodscar类或pickup类,就可以载货
System.out.print(carlist[i].getCarname() + "\t");
loadgoodsnum+=carlist[i].getloadgoods();
}
}
System.out.println("共载货:" + loadgoodsnum + "吨");
System.out.println("租车总价格:" + price + "元");
}
}
}
热门评论
peoplecar, goodscar 和 pickup 这三个类中定义的函数没有写返回类型啊?这里编译通不过,不知道到底该怎么写
System.exit(0);//第一次见这种用法唉,不理解
还有就是scanner 不怎么会用
Scanner input =new Scanner(System.in); int num =input.nextInt();
作者: KingJ3421703
链接:http://www.imooc.com/article/12530
来源:慕课网
求教
写的真好 代码清晰没有什么拖泥带水的地方,尤其是算价格的地方给我很大的启发,对于一个小白来说实在是入门的经典代码,100个赞 加油
目前看到的所有代码貌似都没有考虑健壮性,就是默认用户都会输入数字,加入用户在输入进入系统的选择或者天数时输入字母或其他字符,程序直接崩溃!不过还没有学习异常处理,也算是可以理解吧!
题主这个解决方法还是很不错的,思路清晰,而且也用到了这阶段学习的内容,非常不错!
我自己写着写着就把继承多态什么的全忘记了,然后纯粹用数组实现了一个,惭愧啊!