父类
public class Che {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPicter() {
return picter;
}
public void setPicter(double picter) {
this.picter = picter;
}
public int getHuo() {
return huo;
}
public void setHuo(int huo) {
this.huo = huo;
}
public int getPreson() {
return preson;
}
public void setPreson(int preson) {
this.preson = preson;
}
public double picter;
public int huo;
public int preson;
}
汽车类
public class Qiche extends Che{
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPicter() {
return picter;
}
public void setPicter(double picter) {
this.picter = picter;
}
public int getPreson() {
return preson;
}
public void setPreson(int preson) {
this.preson = preson;
}
String name;
double picter;
int preson;
Qiche(String name,double picter,int preson){
this.name=name;
this.picter=picter;
this.preson=preson;
}
}
火车类
public class Huoche extends Che {
String name;
double picter;
int huo;
Huoche(String name,double picter,int huo){
this.name=name;
this.picter=picter;
this.huo=huo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPicter() {
return picter;
}
public void setPicter(double picter) {
this.picter = picter;
}
public int getHuo() {
return huo;
}
public void setHuo(int huo) {
this.huo = huo;
}
}
皮卡车类
public class Pika extends Che{
String name;
double picter;
int huo;
int preson;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPicter() {
return picter;
}
public void setPicter(double picter) {
this.picter = picter;
}
public int getHuo() {
return huo;
}
public void setHuo(int huo) {
this.huo = huo;
}
public int getPreson() {
return preson;
}
public void setPreson(int preson) {
this.preson = preson;
}
Pika(String name,double picter,int preson,int huo){
this.name=name;
this.picter=picter;
this.preson=preson;
this.huo=huo;
}
}
测试类主程序
public class Dome {
public static void main(String[] args) {
TreeMap<Integer, Che> tm = new TreeMap<Integer,Che>();
Che[] c = {new Qiche("奧迪A4",500.0,4),
new Qiche("馬自達6",400.0,4),
new Pika("皮卡雪6",450.0,4,2),
new Qiche("景龍",800.0,20), //定義 Che類型的數組容器
new Huoche("松花江",400.0,4),
new Huoche("依維柯",1000.0,20)};
System.out.println("欢迎使用哒哒租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner sa = new Scanner(System.in);
int a = sa.nextInt();
if(a==0){
System.out.println("欢迎下次继续使用!!");
}else if(a==1){
System.out.println("您可租車的類型及其價目表");
System.out.println("序号\t"+"汽车名称\t"+"租金\t"+"容量");
//遍历数组
for(int x = 0;x<c.length;x++){
//判断是否 时 汽车类
if(c[x] instanceof Qiche){
System.out.println((x+1)+"\t"+c[x].getName()+"\t"+c[x].getPicter()+"/天\t"+"載人"+c[x].getPreson());
tm.put(x+1, c[x]); //把这个添加进treemap集合中
}
//判断是否是火车类
else if(c[x] instanceof Huoche){
System.out.println((x+1)+"\t"+c[x].getName()+"\t"+c[x].getPicter()+"/天\t"+"载货"+c[x].getHuo()+"頓");
tm.put(x+1, c[x]);//把这个添加进treemap集合中
}else{
System.out.println((x+1)+"\t"+c[x].getName()+"\t"+c[x].getPicter()+"/天\t"+"载货"+c[x].getHuo()+"頓"+"載人"+c[x].getPreson());
tm.put(x+1, c[x]);//把这个添加进treemap集合中
}
}
double zong =0;
int ren =0;
int pikaren =0;//定义这写是为了存储数值
int huo=0;
int pikahuo = 0;
StringBuilder sb1 = new StringBuilder();//存储汽车类的名字
StringBuilder sb2 = new StringBuilder();//存储火车类的名字
StringBuilder sb3 = new StringBuilder();//存储皮卡车类的名字
System.out.println("請輸入租車的數量");
int shu = sa.nextInt();
for(int x= 0;x<shu;x++){
System.out.println("請輸入第"+(x+1)+"的序號");
int xu = sa.nextInt();
//遍历 map 集合
Set<Map.Entry<Integer, Che>> en = tm.entrySet();
Iterator<Map.Entry<Integer, Che>> it = en.iterator();
while(it.hasNext()){
Map.Entry<Integer,Che> mm = it.next();
int key = mm.getKey();//得到map集合的键值
if(xu==key){//判断是否相同
Che ch = mm.getValue(); //获得车子的值
//判断车子的类型是哪个 车型
if(ch instanceof Qiche){
sb1.append(ch.getName()+" ");//存储汽车类的名字
ren = ren +ch.getPreson();//计算机车的人数
zong = zong + ch.getPicter();//计算 车的金钱
}else if(ch instanceof Huoche){
sb2.append(ch.getName()+" ");//存储汽车类的名字
zong = zong + ch.getPicter();//计算 车的金钱
huo = huo + ch.getHuo();//计算车的货物
}else if(ch instanceof Pika){
sb3.append(ch.getName()+" ");//存储汽车类的名字
ren = ren +ch.getPreson();
zong = zong + ch.getPicter();//计算 车的金钱
pikaren = pikaren + ch.getPreson();//计算机车的人数
pikahuo = pikahuo + ch.getHuo();//计算车的货物
}
}
}
}
System.out.println("請輸入租車的天數");
int day = sa.nextInt();
System.out.println("您的賬單是------------");
if(sb1.length()!=0){
System.out.println("*****可載人的車有:");
System.out.println(sb1.toString()+"可載人:"+ren+"人");
}
if(sb3.length()!=0){
System.out.println("其中皮卡車有:"+sb3.toString()+"可載貨:"+pikahuo+"可載人:"+pikaren);
}
if(sb2.length()!=0){
System.out.println("*****可載貨的車有:");
System.out.println(sb2.toString()+"可載貨:"+huo+"頓");
System.out.println("總計需要:"+zong*day);
}
}else{
System.out.println("请输入 0 或者 1 ");
}
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章