弄了好久,也参考了很多前辈的代码.最后终于做出属于自己的代码了
这里用到了第三季的try...catch语句,以防客人乱输入
萌新初次交作业,希望前辈们指点指点
1.首先是创建abstract父类
public abstract class Car {
int id;
String name;
int price;
int people;
int weight;
public abstract String showcarmsg();
}
2.接着创建载人汽车子类
public class PeopleCar extends Car {
public PeopleCar(int id,String name,int price,int people){
this.id=id;
this.name=name;
this.price=price;
this.people=people;
};
public String showcarmsg() {
return (id+"\t"+name+"\t"+price+"元/天\t"+"\t载人:"+people+"人");
}
}
3.创建载货汽车子类
public class WeightCar extends Car {
public WeightCar(int id,String name,int price,int weight){
this.id=id;
this.name=name;
this.price=price;
this.weight=weight;
};
public String showcarmsg() {
return (id+"\t"+name+"\t"+price+"元/天\t"+"\t载物:"+weight+"吨");
}
}
4.创建既可载人又可载货的汽车的子类
public class TwoCar extends Car {
public TwoCar(int id,String name,int price,int people,int weight){
this.id=id;
this.name=name;
this.price=price;
this.people=people;
this.weight=weight;
};
public String showcarmsg() {
return (id+"\t"+name+"\t"+price+"元/天\t"+"\t载人:"+people+"人\t"+"载物:"+weight+"吨");
}
}
5.额外创建一个Inputter类,以防顾客输入错误信息,可以返回继续再输入
import java.util.Scanner;
public class Inputter {
public int Inputter(){
Boolean inputterBoolean=false;
int getInputter=0;
//int类输入
while(inputterBoolean==false){
try{
Scanner input = new Scanner(System.in);
int intInputter=input.nextInt();
getInputter=intInputter;
inputterBoolean=true;
}catch(Exception e){
System.out.println("您输入有误,请重新输入");
}
};
return getInputter;
}
}
6.最后创建text类来实现人机互动
public class Text {
public static void main(String[] args) {
//0.创建cars数组,保存录入信息
Car[] cars={
new PeopleCar(1,"奥迪A4", 500, 4),
new PeopleCar(2, "马自达6", 400, 4),
new TwoCar(3, "皮卡雪6", 450, 4, 2),
new PeopleCar(4, "金龙", 800, 20),
new WeightCar(5, "松花江", 400,4),
new WeightCar(6,"依维柯" , 1000,20),
};
//1.人机互动
//创建一个对象以便使用input输入信息
Inputter inputmsg=new Inputter();
System.out.println("欢迎使用答答租车系统:\n您是否要租车:1是 0否");
int num=inputmsg.Inputter();
//判断是否要进入系统
if (num==0){
System.out.println("感谢您的使用");
}else if ((num!=1)&&(num!=0)){
System.out.println("您输入的有误,请您输入0或者1");
}else if (num==1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金 \t\t容量");
//循环打印Cars数组中的车型
for (int i=0;i<cars.length;i++){
System.out.println(cars[i].showcarmsg());
}
System.out.println("请输入您要租汽车的数量:");
//创建rentnum记录租车数量
int rentnum=inputmsg.Inputter();
//pricenum记录总价
double pricesum=0;
//pcarname记录载人车名
String pcarname="";
//pcarsum记录共载人数
int pcarsum =0;
//wcarname记录载货车名
String wcarname="";
//wcarsum记录共载货吨数
double wcarsum=0;
//carid记录输入车的序号
int carid=0;
for (int j=1;j<=rentnum;j++){
System.out.println("请输入第"+j+"辆车的序号");
carid=inputmsg.Inputter();
//判断并记录输入的车是属于哪类车
if ((cars[carid-1] instanceof PeopleCar) || (cars[carid-1] instanceof TwoCar)){
pcarname+=("\t"+cars[carid-1].name);
pcarsum+=cars[carid-1].people;
}
if((cars[carid-1] instanceof WeightCar) || (cars[carid-1] instanceof TwoCar)){
wcarname+=("\t"+cars[carid-1].name);
wcarsum+=cars[carid-1].weight;
}
pricesum+=cars[carid-1].price;
}
System.out.println("请输入租车的天数:");
int rentday=inputmsg.Inputter();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
System.out.println(pcarname+"\t共载人:"+pcarsum+"人");
System.out.println("***载货的车有:");
System.out.println(wcarname+"\t共载货:"+wcarsum+"吨");
System.out.println("***租车总价格:"+pricesum*rentday);
};
}
}
最后运行效果如图...感觉这样运行起来才会跟老师想要的作业一模一样
不过其实我自己还发现了一点漏洞.就好比如一开始租车,如果顾客不输入1或者0,而是输入3,4,5等等能不能够重新输入...这里其实是可以改善的,但是我还没想好要不要另外创建一个类或者在inputter类里面控制.不知道大侠没有没有什么好的想法?方便大家一起交流交流
热门评论
您输入的有误,请您输入0或者1,这段提示过后再次输错,会有问题