熬了一天一夜做出来的,本人小白一个,刚学一星期,挺感兴趣的,代码有待改进的地方还需要大神多指点
package com.imooc; import java.util.Scanner; //添加Scanner工具类,这里主要用来接受并返回用户输入的值 public class InitialDada { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("欢迎使用答答租车系统\n您是否要租车:1、是\t0、 否"); //创建Scanner类的对像sc,用来接受用户输入的值 Scanner sc = new Scanner(System.in); /* * 判断用户是否要租车,租车则通过循环语句输出价目表 * 通过用户输入参数来调用方法获取所需要的信息 * 统计信息然后输出 * nextInt();方法是返回用户输入值 */ if(sc.nextInt() == 1) { System.out.println("您可租车的类型及其价目表:\n序号\t汽车名称\t" + " 租金\t\t容量"); //创建所需对象 PassengerCar pc = new PassengerCar(); //创建客车对象 Truck tc = new Truck(); //创建货车对象 PickupCar pk = new PickupCar(); //创建皮卡对象 //通过循环语句导入序号参数调用方法实现与相对应的信息并输出列表 for(int j = 1;j <= 16;j++) { pc.pLoad(j); tc.tLoad(j); pk.tLoad(j); if(pc.number == j) { System.out.println(j + "\t" + pc.name + "\t " + pc.rent + "元/天\t载人:" + pc.pNumber + "人"); } if(tc.number == j) { System.out.println(j + "\t" + tc.name + "\t " + tc.rent + "元/天\t载货:" + tc.dwtc + "吨"); } if(pk.number == j){ System.out.println(j + "\t" + pk.name + "\t " + pk.rent + "元/天\t载人:" + pk.pNumber + "人\t" + "载货" + pk.dwtc); } } /* * */ System.out.println("请输入你要租车的数量:"); int s = sc.nextInt(); String passengerCar = "\n"; //定义要输出载客的车变量 String truck = "\n"; //定义要输出载货的车变量 int headcount = 0; //定义要输出总载客量变量 float totalLoad = 0.0f; //定义要输出总载货量变量 float totalPrice = 0.0f; //定义要输出总金额变量 //通过用户输入租车的数量来实现选车的次数 for(int i = 1;i <= s;i++) { System.out.println("请输入第" + i + "辆车的序号"); do { int c = sc.nextInt(); pc.name = null; tc.name = null; pk.name = null; //通过用户所选序号参数调用方法获取信息,实现总数计算 pc.pLoad(c); tc.tLoad(c); pk.tLoad(c); if(pc.name != null) { passengerCar = passengerCar + pc.name+" "; headcount = headcount + pc.pNumber; totalPrice = totalPrice + pc.rent; System.out.println("成功添加:" + pc.name); break; } else if(tc.name != null) { truck = truck + tc.name+" "; totalLoad = totalLoad + tc.dwtc; totalPrice = totalPrice + tc.rent; System.out.println("成功添加:" + tc.name); break; } else if(pk.name != null) { passengerCar = passengerCar + pk.name+" "; truck = truck + pk.name+"\t"; headcount = headcount + pk.pNumber; totalLoad = totalLoad + pk.dwtc; totalPrice = totalPrice + pk.rent; System.out.println("成功添加:" + pk.name); break; } //实现输入有误则通过循环返回重新输入 else { System.out.println("您的输入有误,没有找到您所需车型\n请重新输入第" + i + "辆车的序号"); } } while(1 > 0); } /* * */ System.out.println("请输入租车天数:"); int day = sc.nextInt(); totalPrice = totalPrice * day; /* * */ System.out.println("你的账单:"); System.out.println("********载人的车有:" + passengerCar + "\n共载人:" + headcount + "人"); System.out.println("********载货的车有:" + truck + "\n共载货:" + totalLoad + "吨"); System.out.println("********租车天数:" + day + "天" + "\n********租车总金额:" + totalPrice + "元"); } /* * 如果用户最上面选择否,这直接跳到这一部 */ System.out.println("********感谢您的光临,再见!*********"); } }
package com.imooc; public class PassengerCar{ //定义一个客车的类 public int number; //定义序号 public String name; //定义名称 public float rent; //定义租金 public int pNumber; //定义载客量 //定义载客带参的方法 public void pLoad(int number) { //if语句用来判断接受序号的参数,从而给客车属性赋值 if(number == 1) { this.number = number; this.name = "奥迪A4"; this.rent = 500.0f; this.pNumber = 4; } if(number == 2) { this.number = number; this.name = "马自达6"; this.rent = 400.0f; this.pNumber = 4; } if(number == 4) { this.number = number; this.name = "金龙"; this.rent = 800.0f; this.pNumber = 20; } if(number == 7) { this.number = number; this.name = "奔驰s600"; this.rent = 800.0f; this.pNumber = 5; } if(number == 8) { this.number = number; this.name = "宝马750i"; this.rent = 800.0f; this.pNumber = 5; } if(number == 9) { this.number = number; this.name = "劳斯莱斯"; this.rent = 1500.0f; this.pNumber = 5; } if(number == 10) { this.number = number; this.name = "宾利"; this.rent = 1500.0f; this.pNumber = 5; } if(number == 11) { this.number = number; this.name = "大众"; this.rent = 500.0f; this.pNumber = 5; } if(number == 12) { this.number = number; this.name = "保时捷"; this.rent = 600.0f; this.pNumber = 5; } if(number == 13) { this.number = number; this.name = "丰田"; this.rent = 400.0f; this.pNumber = 5; } if(number == 14) { this.number = number; this.name = "雷克萨斯"; this.rent = 500.0f; this.pNumber = 5; } if(number == 15) { this.number = number; this.name = "华晨金杯"; this.rent = 400.0f; this.pNumber = 9; } } }
package com.imooc; public class Truck { //定义一个卡车的类 public int number; //定义序号 public String name; //定义名称 public float rent; //定义租金 public float dwtc; //定义载货量 //定义载货带参的方法 public void tLoad(int number) { //if语句用来判断接受序号的参数,从而给货车属性赋值 if(number == 5) { this.number = number; this.name = "松花江"; this.rent = 400.0f; this.dwtc = 4.0f; } if(number == 6) { this.number = number; this.name = "依维柯"; this.rent = 1000.0f; this.dwtc = 20.0f; } if(number == 16) { this.number = number; this.name = "福田"; this.rent = 1000.0f; this.dwtc = 20.0f; } } }
package com.imooc; public class PickupCar extends Truck{ //定义一个皮卡的类,并且继承父类Truck(卡车类) public int pNumber; //定义载客量,父类没有的属性 //修改父类载重的方法,添加给载客量赋值 public void tLoad(int number) { //if语句用来判断接受序号的参数,从而给皮卡属性赋值 if(number == 3) { this.number = number; this.name = "皮卡雪6"; this.rent = 450.0f; this.dwtc = 2.0f; this.pNumber = 4; } } }
基本上都是用前两季的方法,就是Scanner工具类获取参数的方法是我百度学的,因为前两季没有讲怎么获取用户参数,有需要改进的地方,大神们多多指教,在imooc共同成长