Chase_Java
2016-10-21 22:42
package DaRentCar2; public abstract class Car { private String name; private int rentfee; private int weight; private int person; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getRentfee() { return rentfee; } public void setRentfee(int rentfee) { this.rentfee = rentfee; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public int getPerson() { return person; } public void setPerson(int person) { this.person = person; } }
package DaRentCar2; public interface IPersonCarry { void PersonC(); }
package DaRentCar2; public interface IWeightCarry { public abstract void WeightC(); }
package DaRentCar2; public interface IWeightPersonCarry extends IWeightCarry, IPersonCarry { void WeightC(); }
package DaRentCar2; public class Carriage extends Car implements IPersonCarry { public Carriage(String name,int rentfee,int weight,int person){ this.setName(name); this.setRentfee(rentfee); this.setWeight(weight); this.setPerson(person); } @Override public void PersonC() { // TODO Auto-generated method stub System.out.println("可以载人"); } }
package DaRentCar2; public class Pickup extends Car implements IWeightPersonCarry{ public Pickup(String name,int rentfee,int weight,int person){ this.setName(name); this.setRentfee(rentfee); this.setWeight(weight); this.setPerson(person); } @Override public void PersonC() { // TODO Auto-generated method stub System.out.println("可以载人"); } @Override public void WeightC() { // TODO Auto-generated method stub System.out.println("可以载货"); } }
package DaRentCar2; public class Truck extends Car implements IWeightCarry{ public Truck(String name,int rentfee,int weight,int person){ this.setName(name); this.setRentfee(rentfee); this.setWeight(weight); this.setPerson(person); } @Override public void WeightC() { System.out.println("可以载货"); } }
package DaRentCar2; import java.util.Scanner; public class Test { public static void main(String[] args) { Car carriage1 = new Carriage("奥迪A4", 500, 0, 4); Car carriage2 = new Carriage("马自达6", 400, 0, 4); Car carriage3 = new Carriage("金龙", 800, 0, 20); Car pickup = new Pickup("皮卡雪6", 450, 2, 4); Car truck1 = new Truck("松花江", 400, 4, 0); Car truck2 = new Truck("依维柯", 1000, 20, 0); System.out.println("欢迎来到大大租车系统"); System.out.println("是否租车? 1:是 0:否"); Scanner in = new Scanner(System.in); int choose = in.nextInt(); if(choose == 0){ System.out.println("欢迎下次再来,退出租车系统"); System.exit(0); }else if(choose != 0 && choose != 1){ System.out.println("输入有误,退出系统"); System.exit(0); }else{ System.out.println("您可租的汽车类型和价目表:"); System.out.println("序号\t汽车名称\t租金\t容量"); System.out.println("1.\t"+carriage1.getName()+"\t"+carriage1.getRentfee()+"/天\t"+"载"+carriage1.getPerson()+"人"); System.out.println("2.\t"+carriage2.getName()+"\t"+carriage2.getRentfee()+"/天\t"+"载"+carriage2.getPerson()+"人"); System.out.println("3.\t"+carriage3.getName()+"\t"+carriage3.getRentfee()+"/天\t"+"载"+carriage3.getPerson()+"人"); System.out.println("4.\t"+pickup.getName()+"\t"+pickup.getRentfee()+"/天\t"+"载"+pickup.getPerson()+"人"+" 载"+pickup.getWeight()+"吨"); System.out.println("5.\t"+truck1.getName()+"\t"+truck1.getRentfee()+"/天\t"+"载"+truck1.getWeight()+"吨"); System.out.println("6.\t"+truck2.getName()+"\t"+truck2.getRentfee()+"/天\t"+"载"+truck2.getWeight()+"吨"); System.out.println("请输入您要租的车的数量"); int num = in.nextInt(); int n1=0,n2=0,n3=0,n4=0,n5=0,n6=0; for(int i =0;i<num;i++){ System.out.println("请输入您要租车的序号"); int N = in.nextInt(); if(N==1){ n1++; } if(N==2){ n2++; } if(N==3){ n3++; } if(N==4){ n4++; } if(N==5){ n5++; } if(N==6){ n6++; } } int totalPerson = carriage1.getPerson()*n1+carriage1.getPerson()*n2+carriage1.getPerson()*n3 +pickup.getPerson()*n4; int totalWeight = pickup.getWeight()*n4+truck1.getWeight()*n5+truck2.getWeight()*n6; int totalRentfee = carriage1.getRentfee()*n1+carriage2.getRentfee()*n2+ carriage3.getRentfee()*n3+pickup.getRentfee()*n4+truck1.getRentfee()*n5+ truck2.getRentfee()*n6; System.out.println("请输入租车天数"); int day = in.nextInt(); System.out.println("您的账单:"); System.out.println("***可载人的车有:"); if(n1!=0){ System.out.print(n1+"台"+carriage1.getName()+'\t'); } if(n2!=0){ System.out.print(n2+"台"+carriage2.getName()+'\t'); } if(n3!=0){ System.out.print(n3+"台"+carriage3.getName()+'\t'); } if(n4!=0){ System.out.print(n4+"台"+pickup.getName()+'\t'); } System.out.println("共载人:"+totalPerson+"人"); System.out.println("***可载货的车有:"); if(n4!=0){ System.out.print(n4+"台"+pickup.getName()+'\t'); } if(n5!=0){ System.out.print(n5+"台"+truck1.getName()+'\t'); } if(n6!=0){ System.out.print(n6+"台"+truck2.getName()+'\t'); } System.out.println("共载货:"+totalWeight+"吨"); System.out.println("总租金:"+totalRentfee*day+"元"); } } }
用到了 知识现在代码比较少感觉不出来 等方法多了就感觉到接口的有用了
用接口的引用可以指向实现了接口的对象,然后就可以引用一些方法什么的
Java入门第二季 升级版
530558 学习 · 6091 问题
相似问题