qq_放飞心情_0
2016-03-20 13:56
package newpackage;
/**
*
* @author Administrator
*/
public class car {
private String name="";
private double money=0;
public car(){}
public car(String name,double money)
{
this.name=name;
this.money=money;
}
public void useway() { System.out.println("车"); }
public String getname(){return name;}
public double getmoney(){return money;}
public String toString()
{
return "name:"+name+" "+"money:"+money;
}
}
package newpackage;
/**
*
* @author Administrator
*/
public interface takepeople {
public int getpeoplenum();
}
package newpackage;
/**
*
* @author Administrator
*/
public interface takegoods {
public int getgoodsnum();
}
package newpackage;
/**
*
* @author Administrator
*/
public class 奥迪A4 extends car implements takepeople{
private int peoplenum=0;
public 奥迪A4()
{
super();
}
public 奥迪A4(String name,double money,int x)
{
super(name,money);
peoplenum=x;
}
public int getpeoplenum()
{
return peoplenum;
}
public String toString()
{
return "车名:奥迪A4"+super.toString()+"载人量:"+peoplenum;
}
}
package newpackage;
/**
*
* @author Administrator
*/
public class 马自达6 extends car implements takepeople{
private int peoplenum=0;
public 马自达6()
{
super();
}
public 马自达6(String name,double money,int x)
{
super(name,money);
peoplenum=x;
}
public int getpeoplenum()
{
return peoplenum;
}
public String toString()
{
return "车名:马自达6"+super.toString()+"载人量:"+peoplenum;
}
}
package newpackage;
/**
*
* @author Administrator
*/
public class 松花江货车 extends car implements takegoods {
private int goodsnum=0;
public 松花江货车()
{
super();
}
public 松花江货车(String name,double money,int x)
{
super(name,money);
goodsnum=x;
}
public int getgoodsnum()
{
return goodsnum;
}
public String toString()
{
return "车名:松花江货车"+super.toString()+"载货量:"+goodsnum;
}
}
package newpackage;
/**
*
* @author Administrator
*/
public class 皮卡雪6 extends car implements takegoods,takepeople{
private int peoplenum=0;
private int goodsnum=0;
public 皮卡雪6()
{
super();
}
public 皮卡雪6(String name,double money,int peoplenum,int goodsnum )
{
super(name,money);
this.peoplenum=peoplenum;
this.goodsnum=goodsnum;
}
public int getgoodsnum()
{
return goodsnum;
}
public int getpeoplenum()
{
return peoplenum;
}
public String toString()
{
return "车名:松花江货车"+super.toString()+"载人量"+peoplenum+"载货量:"+goodsnum;
}
}
package newpackage;
/**
*
* @author Administrator
*/
public class 金龙 extends car implements takegoods {
private int goodsnum=0;
public 金龙 ()
{
super();
}
public 金龙 (String name,double money,int x)
{
super(name,money);
goodsnum=x;
}
public int getgoodsnum()
{
return goodsnum;
}
public String toString()
{
return "车名:松花江货车"+super.toString()+"载货量:"+goodsnum;
}
}
package newpackage;
import java.util.*;
/**
*
* @author Administrator
*/
public class initail {
public static void main(String[] atgs)
{
奥迪A4 aodi=new 奥迪A4("奥迪A4",500,4);
松花江货车 shjhc=new 松花江货车("松花江货车",400,4);
皮卡雪6 pikaxue=new 皮卡雪6("皮卡雪6",450,4,2);
金龙 jinglong=new 金龙("金龙",800,20);
马自达6 mazida=new 马自达6("马自达6",400,4);
System.out.println("欢迎使用达达租车系统");
Scanner in=new Scanner(System.in);
System.out.println("您是否要租车:1是 0否");
int input=in.nextInt();
if(input==0)
return;
System.out.println("您可租车的类型和价目表");
System.out.println("1"+aodi);
System.out.println("2"+shjhc);
System.out.println("3"+pikaxue);
System.out.println("4"+jinglong);
System.out.println("5"+mazida);
System.out.println("请输入你要租车的数量");
int inp=in.nextInt();
double money=0;
int sumday=0;
for(int i=0;i<inp;i++)
{
System.out.println("请输入车的序号和天数");
int nex=in.nextInt();
int day=in.nextInt();
sumday=sumday+day;
while(nex<0||nex>5)
{
System.out.println("请重新输入车的序号");
nex=in.nextInt();
}
switch(nex)
{
case 1:
money=money+aodi.getmoney()*day;
break;
case 2:
money=money+shjhc.getmoney()*day;
break;
case 3:
money=money+pikaxue.getmoney()*day;
break;
case 4:
money=money+jinglong.getmoney()*day;
break;
case 5:
money=money+mazida.getmoney()*day;
break;
default:
break;
}
}
System.out.println("all money="+money+"yuan"+"sumday="+sumday);
}
}
代码大致功能实现了,有些细节还没弄好,我想先请教下在结构上有没有可以优化的地方。先上main函数:
package project;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Aodi tp1=new Aodi(1,"奥迪A4",500,4);
Mazida tp2=new Mazida(2,"马自达6",400,4);
Pika tp3=new Pika(3,"皮卡雪6",450,4,2);
Jinglong tp4=new Jinglong(4,"金龙",800,20);
Songhuajiang tp5=new Songhuajiang(5,"松花江",400,4);
Yiweike tp6=new Yiweike(6,"依维柯",1000,20);
/*先将各个子类实例化,并且赋上初值*/
System.out.println("您是否要租车?1.是 0.否");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if(a==0){
System.out.println("谢谢");}
else{
System.out.println("租车列表为:\n"+tp1.toString()+"\n"+
tp2.toString()+"\n"+tp3.toString()+"\n"+tp4.toString()+"\n"+
tp5.toString()+"\n"+tp6.toString());
/*将租车的信息输出*/
System.out.println("请输入租车数量:");
int b=input.nextInt();
int[]c;
c=new int[b];
System.out.println("请输入序号:");
for(int i=0;i<c.length;i++)
c[i]=input.nextInt();
/*把选择的车的序号赋给c[]数组*/
String str1="",str2="";
int sum1=0;int sum2=0;int sum3=0;
for(int i=0;i<c.length;i++){
int d=c[i];
switch (d){
case 1: {sum1=sum1+tp1.zaike;sum3=sum3+tp1.rent;break;}
case 2: {sum1=sum1+tp2.zaike;sum3=sum3+tp2.rent;break;}
case 3: {sum1=sum1+tp3.zaike;sum2=sum2+tp3.zaihuo;sum3=sum3+tp3.rent;break;}
case 4: {sum1=sum1+tp4.zaike;sum3=sum3+tp4.rent;break;}
case 5: {sum2=sum2+tp5.zaihuo;sum3=sum3+tp5.rent;break;}
case 6: {sum2=sum2+tp6.zaihuo;sum3=sum3+tp6.rent;break;}
}
/*swtich语句主要用于计算载人总人数sum1,载货总吨数sum2,和租金总数*/
str1=str1+tp1.say(d)+tp2.say(d)+ tp3.say(d)+ tp4.say(d);
str2=str2+tp3.say(d)+ tp5.say(d)+ tp6.say(d);
/*用于输出选择的车的型号,定义一个字符变量str1,代表可以载人的车的型号。
* 因为调用了子类say语句,str1变量处于for循环内,所以每次循环(d值改变)
* 都可以给str1添加一个租车的字符,str2同理*/
if (i==c.length-1){
System.out.println("可以载人的车为:"+str1+" "+"载人人数为:"+sum1);
System.out.println("可以载货的车为:"+str2+ " "+"载货吨数为:"+sum2+"\n"+"总租金为"+sum3);}
/*i==c.length-1代表循环已经结束,租车的型号都已经赋给了str1,str2,sum也都
* 计算完毕,故可以输出了*/
}
}
}
}
父类Car:
package project; public class Car { int num; String name; int rent; public Car(int num,String name,int rent){ this.num=num; this.name=name; this.rent=rent; } }
子类奥迪:
package project; public class Aodi extends Car { int zaike; public Aodi(int num, String name, int rent,int zaike) { super(num,name,rent); this.zaike=zaike; } public String toString() { return "奥迪A4: 序号:" + num + ", 型号:" + name + ", 租金" + rent + "载客:"+zaike; } public String say(int a){ if(a==1) return " 奥迪A4 "; else return ""; } }
子类马自达:
package project; public class Mazida extends Car { int zaike; public Mazida(int num, String name, int rent,int zaike) { super(num,name,rent); this.zaike=zaike; } @Override public String toString() { return "马自达6 :序号:" + num + ", 型号:" + name + ", 租金" + rent + "载客:"+zaike; } public String say(int a){ if(a==2) return " 马自达6 "; else return ""; } }
子类皮卡雪:
package project; public class Pika extends Car{ int zaike; int zaihuo; public Pika(int num, String name, int rent,int zaike,int zaihuo) { super(num,name,rent); this.zaihuo=zaihuo; this.zaike =zaike; } @Override public String toString() { return "皮卡雪6: 序号:" + num + ", 型号:" + name + ", 租金" + rent + "载客:"+zaike+"载货:"+zaihuo; } public String say(int a){ if(a==3) return " 皮卡雪6 "; else return ""; } }
后面的几个子类格式都是一样的,就不一个个写了,显得太乱。其实我觉得应该在父类里面写一个say()方法,让子类重写,但是在调用的时候貌似并不能简化多少,因为在main方法里还是要将每个子类逐一实例化,不知道大家有没有可以优化的地方,求赐教。
个人觉得你完全可以将接口中的方法在父类中设定为抽象类,车都有这种特性的,只不过有的子类实现,有的子类不实现
Java入门第二季 升级版
530558 学习 · 6091 问题
相似问题