package kf; public abstract class AllCar{ private int num; private String carName; private int rent; private int Manned; private int Tonnage; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getRent() { return rent; } public void setRent(int rent) { this.rent = rent; } public int getManned() { return Manned; } public void setManned(int manned) { Manned = manned; } public int getTonnage() { return Tonnage; } public void setTonnage(int tonnage) { Tonnage = tonnage; } public void Caritem(){} } class Car extends AllCar{ public void Caritem(int num,String carName,int rent,int manned){ setNum(num); setCarName(carName); setRent(rent); setManned(manned); System.out.println(getNum()+"."+" "+getCarName()+" "+getRent()+"元/天"+" "+"载人:"+getManned()+"人"); } } class PassengerCar extends AllCar{ public void Caritem(int num,String carName,int rent,int manned){ setNum(num); setCarName(carName); setRent(rent); setManned(manned); System.out.println(getNum()+"."+" "+getCarName()+" "+getRent()+"元/天"+" "+"载人:"+getManned()+"人"); } } class Truck extends AllCar{ public void Caritem(int num,String carName,int rent,int tonnage){ setNum(num); setCarName(carName); setRent(rent); setTonnage(tonnage); System.out.println(getNum()+"."+" "+getCarName()+" "+getRent()+"元/天"+" "+"载货:"+getTonnage()+"吨"); } } class Pickup extends AllCar{ public void Caritem(int num,String carName,int rent,int manned,int tonnage){ setNum(num); setCarName(carName); setRent(rent); setManned(manned); setTonnage(tonnage); System.out.println(getNum()+"."+" "+getCarName()+" "+getRent()+"元/天"+" "+"载人:"+getManned()+"人"+" "+"载货:"+getTonnage()+"吨"); } }
package kf; import java.util.Scanner; public class CarRental { public static void main(String[] args) { System.out.println("欢迎使用租车系统:"); System.out.println("您是否要租车:1是 0否"); try { Scanner sc= new Scanner(System.in); int use=sc.nextInt(); if(use==1){ System.out.println("您可租车的类型及其价目表:"); System.out.println("序号"+" "+"汽车名称"+" "+"租金"+" "+"容量"); Car c1=new Car(); Car c2=new Car(); c1.Caritem(1, "奥迪A4", 500, 4); c2.Caritem(2, "马自达6", 400, 4); Pickup c3 =new Pickup(); c3.Caritem(3, "皮卡雪6", 450, 4, 2); PassengerCar c4= new PassengerCar(); c4.Caritem(4, "金龙", 800, 20); Truck c5=new Truck(); Truck c6=new Truck(); c5.Caritem(5, "松花江", 400, 4); c6.Caritem(6, "依维柯", 1000, 20); StringBuffer buf1 = new StringBuffer(""); StringBuffer buf2 = new StringBuffer(""); System.out.println("请输入您要租汽车的数量:"); int totalpeople=0; int totalthing=0; int totalpay=0; int carneed=sc.nextInt(); int[] arr=new int[carneed]; for (int i = 1; i <= carneed; i++) { System.out.println("请输入第"+i+"辆车的序号:"); int k=sc.nextInt(); arr[i-1]=k; switch(k){ case 1: totalpeople+=c1.getManned(); buf1.append(c1.getCarName()+" "); totalpay+=c1.getRent(); break; case 2: totalpeople+=c2.getManned(); buf1.append(c2.getCarName()+" "); totalpay+=c2.getRent(); break; case 3: totalpeople+=c3.getManned(); buf1.append(c3.getCarName()+" "); totalthing+=c3.getTonnage(); buf2.append(c3.getCarName()+" "); totalpay+=c3.getRent(); break; case 4: totalpeople+=c1.getManned(); buf1.append(c4.getCarName()+" "); totalpay+=c4.getRent(); break; case 5: totalpeople+=c1.getTonnage(); buf2.append(c5.getCarName()+" "); totalpay+=c5.getRent(); break; case 6: totalpeople+=c1.getTonnage(); buf2.append(c6.getCarName()+" "); totalpay+=c6.getRent(); break; } } System.out.println("请输入租车天数:"); int day=sc.nextInt(); sc.close(); System.out.println("***可载人的车有:"); System.out.print(buf1); System.out.println("共载:"+totalpeople+"人"); System.out.println("***载货的车有:"); System.out.print(buf2); System.out.println("共载货:"+totalthing+"吨"); System.out.println("***租车总价格:"+day*totalpay+"元"); } } catch (Exception e) { System.out.println("输入的不是数字"); } } }
可以参考一下本章节中置顶的帖子,而且有大神的点评