DaWang
2015-07-28 17:22
package com.immoc; /* * 汽车父类 */ public abstract class Car { private int carMoney; //汽车租金 private String carName; //汽车名字 private int carNumb; //汽车序列号 private int carryNum; //载货量 private int passengerNum; //载客人数 public void setCarMoney(int carMoney) { this.carMoney = carMoney; } public int getCarMoney() { return carMoney; } public void setCarName(String carName) { this.carName = carName; } public String getCarName() { return carName; } public void setCarNumb(int carNumb) { this.carNumb = carNumb; } public int getCarNumb() { return carNumb; } public void setPassengerNum(int passengerNum) { this.passengerNum = passengerNum; } public int getPassengerNum() { return passengerNum; } public void setCarryNum(int carryNum) { this.carryNum = carryNum; } public int getCarryNum() { return carryNum; } public abstract void showInfo(); }
package com.immoc; /* * 载客汽车 */ public class passengerCar extends Car { public passengerCar(int carNumb,String carName,int carMoney,int passengerNum){ this.setCarNumb(carNumb); this.setCarName(carName); this.setCarMoney(carMoney); this.setPassengerNum(passengerNum); } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println(getCarNumb()+"\t"+getCarName()+"\t"+getCarMoney()+"/天"+"\t"+"\t"+"载人:"+getPassengerNum()+"人"); } }
package com.immoc; /* * 载货汽车 */ public class Truck extends Car { public Truck(int carNumb,String carName,int carMoney,int carryNum){ this.setCarNumb(carNumb); this.setCarName(carName); this.setCarMoney(carMoney); this.setCarryNum(carryNum); } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println(getCarNumb()+"\t"+getCarName()+"\t"+getCarMoney()+"/天"+"\t"+"\t"+"载货"+getCarryNum()+"吨"); } }
package com.immoc; /* * 皮卡汽车 */ public class Pickup extends Car { public Pickup(int carNumb,String carName,int carMoney,int carryNum,int passengerNum){ this.setCarNumb(carNumb); this.setCarName(carName); this.setCarMoney(carMoney); this.setPassengerNum(passengerNum); this.setCarryNum(carryNum); } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println(getCarNumb()+"\t"+getCarName()+"\t"+getCarMoney()+"/天"+"\t"+"\t"+"载货"+getCarryNum()+"吨"+"\t"+"载人"+getPassengerNum()+"人"); } }
package com.immoc; import java.util.Scanner; public class TestCar { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("欢迎使用嗒嗒租车系统!"); System.out.println("您是否需要租车?1:是;0:否"); Scanner s=new Scanner(System.in); int input=s.nextInt(); if(input==0){ System.out.println("感谢您使用嗒嗒租车系统,下次再见!"); } else if(input==1){ System.out.println("以下是您可以租车的类型及其价目表:"); Car []carlist={new passengerCar(1,"奥迪A4",500,4),new passengerCar(2,"马自达6",400,4),new Pickup(3,"皮卡雪6",450,4,2),new passengerCar(4,"金龙 ",800,20), new Truck(5,"松花江",400,4),new Truck(6,"依维柯",1000,20)}; System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"\t"+"容量"); //通过遍历数组,打印输出可供出租的汽车清单及其信息内容 for(int i=0;i<carlist.length;i++){ System.out.print((i+1) + ".\t"); //输出车辆信息 carlist[i].showInfo(); } int totalMoney=0; //价格 //获取租车数量 System.out.println("请您输入你需要租得汽车序数量:"); int carNum=s.nextInt(); //将客户选择的车辆对象放入choiceCar数组 Car[] choiceCar = new Car[carNum]; for(int i=0;i<carNum;i++){ System.out.println("请输入第"+(i+1)+"辆车的序号:"); int num=s.nextInt(); choiceCar[i]=carlist[num-1]; if(choiceCar[i] instanceof passengerCar){ totalMoney+=choiceCar[i].getCarMoney(); } if(choiceCar[i] instanceof Pickup){ totalMoney+=choiceCar[i].getCarMoney(); } if(choiceCar[i] instanceof Truck){ totalMoney+=choiceCar[i].getCarMoney(); } } //获取租车天数 System.out.println("请输入想要租车的天数:"); int rentDay = s.nextInt(); //租车天数 //输出资费 System.out.println("您需要支付总价为:"+totalMoney*rentDay+"元"); } } }
nice code!思路很清晰!
真厉害啊
Java入门第二季 升级版
530554 学习 · 6091 问题
相似问题