为了账号安全,请及时绑定邮箱和手机立即绑定

纯萌新 我感觉我的代码好繁琐 有很多地方需要优化 改进

package project4;

public abstract class Car {

public abstract void price();

}

package project4;


public class Aodi extends Car {

    public int num = 1;

    public String name = "奥迪A4";

    public int rent = 500;

    public int manned = 4;

@Override

public void price() {

// TODO Auto-generated method stub

       System.out.println( +num+".   "+name+"   "+rent+"/天   载人:" +manned + "人" );

}


}


package project4;


public class Mazida extends Car {

public int num = 2;

    public String name = "马自达6";

    public int rent = 400;

    public int manned = 4;

    @Override

public void price() {

// TODO Auto-generated method stub

    System.out.println( +num+".   "+name+"  "+rent+"/天   载人:" +manned + "人" );

}


}

package project4;


public class Pikaxue extends Car {

public int num = 3;

    public String name = "皮卡雪6";

    public int rent = 450;

   public  int manned = 4;

    public float cargo = 2.0f;

    @Override

public void price() {

// TODO Auto-generated method stub

    System.out.println( +num+".   "+name+"  "+rent+"/天   载人:" +manned + "载货:"+ cargo +"吨" );

}


}

package project4;


public class Jinlong extends Car {

public int num = 4;

    public String name = "金龙";

    public int rent = 800;

    public int manned = 20;

@Override

public void price() {

// TODO Auto-generated method stub

System.out.println( +num+".   "+name+"     "+rent+"/天   载人:" +manned + "人" );

}


}

package project4;


public class Songhuajiang extends Car {

public int num = 5;

    public String name = "松花江";

    public int rent = 400;

    public float cargo = 4.0f;

    @Override

public void price() {

// TODO Auto-generated method stub

    System.out.println( +num+".   "+name+"   "+rent+"/天   载货:" +cargo + "吨" );

}


}

package project4;


public class Yiweike extends Car {

public int num = 6;

    public String name = "依维柯";

    public int rent = 1000;

    public float cargo = 20.0f;

@Override

public void price() {

// TODO Auto-generated method stub

System.out.println( +num+".   "+name+"  "+rent+"/天   载货:" +cargo + "吨" );

}


}

package project4;

import java.util.Scanner;

public class Initclass {


public static void main(String[] args) {

// TODO Auto-generated method stub

         System.out.println("欢迎使用达达租车系统");

         System.out.println("您是否要租车:1是 0否");

         Scanner input = new Scanner(System.in);//键盘输入

         int a = input.nextInt();

         if(a==0) 

         {

             System.out.println("谢谢您的光临");         

         }

         else if(a==1)

         {

        System.out.println("您可租车的类型及其价目表:");

        System.out.println("序号 汽车名称 租金     容量");

        Aodi car1 = new Aodi();

        car1.price();

        Mazida car2 = new Mazida();

        car2.price();

        Pikaxue car3 = new Pikaxue();

        car3.price();

        Jinlong car4 = new Jinlong();

        car4.price();

        Songhuajiang car5 = new Songhuajiang();

        car5.price();

        Yiweike car6 = new Yiweike();

        car6.price();

        System.out.println("请输入您要租汽车的数量");

        int n = input.nextInt();

        int[] numbers = new int[100];//建立数组保存汽车序号

         

        for(int i=1;i<=n;i++)//循环输入客户所选汽车序号

        {

         

        System.out.println("请输入第"+i+"辆车的序号");

        int numb = input.nextInt();

        if(numb<0||numb>6)

        {

        System.out.println("输入序号错误");

        break;

        }

        numbers[i] = numb;

        }

        System.out.println("请输入租车天数");

        int day = input.nextInt();

        System.out.println("您的账单:");

        System.out.println("***可载人的车有:");

        int j;

        int ma=0;

        int money=0;

        for(j=1;j<=n;j++)

        {

       

        if(numbers[j] == car1.num)

        {

        ma = ma + car1.manned;

        System.out.print(car1.name);

        money = money +car1.rent * day;

        }

        if(numbers[j] == car2.num)

        {

        ma = ma + car2.manned;

            System.out.print(car2.name);

            money = money +car2.rent * day;

        }

        if(numbers[j] == car3.num)

        {

        ma = ma + car3.manned;

            System.out.print(car3.name);

            money = money +car3.rent * day;

        }

        if(numbers[j] == car4.num)

        {

        ma = ma + car4.manned;

            System.out.print(car4.name);

            money = money +car4.rent * day;

        }

        System.out.print(" ");

       

        }

        System.out.println("共载人:"+ ma +"人");

        System.out.println("***载货的车有:");

        int k;

        float weight=0;

             for(k=1;k<=n;k++)

             {

            if(numbers[k] == car3.num)

            {

            weight = weight + car3.cargo;

            System.out.print(car3.name);

            }

            if(numbers[k] == car5.num)

            {

            weight = weight + car5.cargo;

            System.out.print(car5.name);

            money = money + car5.rent * day;

            }

            if(numbers[k] == car6.num)

            {

            weight = weight + car6.cargo;

            System.out.print(car6.name);

            money = money + car6.rent * day;

            }

            System.out.print(" ");

             

             }

             System.out.println("共载货:"+weight);

             

            System.out.println("***租车总价值:"+ money + "元"); 

         }

        System.out.println("欢迎您的下次光临");

         

    }


}


正在回答

4 回答

import java.util.Scanner;
class Car
{
 private String name;//名称
 private int rent;//金额
 private int manned;//载人
 private int cargo;//载客
 public void setName(String name)
 { this.name= name;}
 public String getName()
 { return name;}

 public void setRent(int rent)
 { this. rent= rent;}
 public int getRent()
 { return rent;}

 public void setManned(int manned)
 { this. manned= manned;}
 public int getManned()
 { return manned;}

 public void setCargo(int cargo)
 { this. cargo= cargo;}
 public int getCargo()
 { return cargo;}
  void show(){}
}
class PassengerCar extends Car//载人车
{
 PassengerCar(String name,int rent,int manned)
 {
  this.setName(name);
  this.setRent(rent);
  this.setManned(manned);
 }
 public void show()//载人车的输出信息
 {
  System.out.println(getName()+"\t\t"+getRent()+"元/天\t载人"+getManned()+"人");
 }

}
class Pickup extends Car//载人载货车
{
 Pickup(String name,int rent,int manned,int cargo)
 {
  this.setName(name);
  this.setRent(rent);
  this.setManned(manned);
  this.setCargo(cargo);
 }
 public void show()//载人载货车的输出信息
 {
  System.out.println(getName()+"\t\t"+getRent()+"元/天\t载人"+getManned()+"人 载货:"+getCargo()+"吨");
 }

}

class Truck extends Car//载货车
{
 Truck(String name,int rent,int cargo)
 {
  this.setName(name);
  this.setRent(rent);
  this.setCargo(cargo);
 }
 public void show()//载货车的输出信息
 {
  System.out.println(getName()+"\t\t"+getRent()+"元/天\t载货:"+getCargo()+"吨");
 }

}

class RentCarsTest3
{
 public static void main(String[] args)
 {
  Car[] car={new PassengerCar("奥迪4",500,4),new PassengerCar("马自达6",400,4),
     new Pickup("皮卡雪6",450,4,2),new PassengerCar("金龙",800,20),
     new Truck("松花江",400,4),new Truck("依维河",1000,20)};//创建对象数组,并初始化
  
  entering(car);
  carRental(car);
  
  
 }
 public static void vehicle(Car[] car)//打印车辆信息
 {
  System.out.println("\n序号\t汽车名称\t租金\t\t容量");
  for (int x=0;x<car.length ;x++ )
  { 
   System.out.print(x+1+".\t");
   car[x].show();
  }
 }
 public static void entering(Car[] car)//判断是否租车
 {
  int x=0;
  System.out.println("欢迎使用哒哒租车系统:\n您是否要租车:1、是 0否");
  do
  {  
   Scanner p=new Scanner(System.in);
   int entering=p.nextInt();
   if (entering==1)
   {
    vehicle(car);
    break;
   }
   else if (entering!=0)
   {
    System.out.println("您的输入有误!请重新输入:\n您是否要租车:1、是 0否");
    x=1;
   }
   else
   { System.out.println("谢谢您的使用");
    System.exit(0);
   }
  }while (x==1);
 }

 public static void carRental(Car[] car)//租车情况
 {
  int rentnum=0,mannednum=0,cargonum=0;//总价格,载人数,载客数
  Scanner b=new Scanner(System.in);
  System.out.println("请输入要租汽车的数量:");
  int number=b.nextInt();//车辆数量
  int[] sequence=new int[number];//车辆序号数组
  for (int x=0;x<number ;x++ )
  {
   int y=x+1;
   System.out.println("请输入要租的第"+y+"辆车的序号");
   Scanner c=new Scanner(System.in);
   sequence[x]=c.nextInt();//存储用户输入的序号
  }
  System.out.println("请输入要租汽车的天数:");
  int day=b.nextInt();//天数
   
   //打印账单
  System.out.println("您的账单:\n***可载人的车有:");
  for (int x=0;x<number;x++ )
  {
   if (car[sequence[x]-1] instanceof PassengerCar||car[sequence[x]-1] instanceof Pickup)//判断 car[sequence[x]-1] 是不是PassengerCar类型,或者是不是Pickup类型
   {
    System.out.print(car[sequence[x]-1].getName()+"\t");
    mannednum+=car[sequence[x]-1].getManned();
   }
  }
  System.out.println("总载人:"+mannednum+"人\n***可载货的车有:");
  for (int x=0;x<number;x++ )
  {
   if (car[sequence[x]-1] instanceof Truck||car[sequence[x]-1] instanceof Pickup)  
   {
    System.out.print(car[sequence[x]-1].getName()+"\t");
    cargonum+=car[sequence[x]-1].getCargo();
   }
   rentnum+=car[sequence[x]-1].getRent();
  }
  rentnum=rentnum*day;
  System.out.println("总载货:"+cargonum+"吨\n***租车总价格:"+rentnum);  
 }
}


0 回复 有任何疑惑可以回复我~

萌新不在是萌新了

0 回复 有任何疑惑可以回复我~

学的比我好多了。

0 回复 有任何疑惑可以回复我~

思路很清晰。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

纯萌新 我感觉我的代码好繁琐 有很多地方需要优化 改进

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信