花了大半天,勉强是算实现所有功能,感觉还是很繁琐

来源:12-1 综合练习

无限记录者

2019-09-10 16:41

package com.runoob.test;
public class Cheku {
String name;
double price;
double load;
int manN;
public Cheku(int xuhao) {
switch (xuhao) {
case 1:
this.name="奥迪A6";
this.price=500;
this.load=0;
this.manN=4;
break;
case 2:
this.name="马自达6";
this.price=400;
this.load=0;
this.manN=4;
break;
case 3:
this.name="皮卡雪6";
this.price=450;
this.load=2;
this.manN=4;
break;
case 4:
this.name="金龙";
this.price=800;
this.load=0;
this.manN=4;
break;
case 5:
this.name="松花江";
this.price=400;
this.load=4;
this.manN=0;
break;
case 6:
this.name="依维柯";
this.price=1000;
this.load=20;
this.manN=0;
break;
}
}
}
package com.runoob.test;
import java.util.Scanner;
import java.util.Arrays;
public class Dada {
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 ans=input.nextInt();
if(ans==1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("1,奥迪A4500元/天载人:4人");
System.out.println("2,马自达6400元/天载人:4人");
System.out.println("3,皮卡雪6450元/天载人:4人 载货:2吨");
System.out.println("4,金龙800元/天载人:20人");
System.out.println("5,松花江400元/天载货:4吨");
System.out.println("6,依维柯1000元/天载货:20吨");
System.out.println("请输入要租的车辆数量");
int Ans=input.nextInt();//确定创建对象的次数
Cheku [] list=new Cheku[Ans];//这个对象数组用于保存创建好的对象
int loadSum=0;//货车的数量
int manSum=0;//客车的数量
double sumPrice=0;//保存累计的金额
//通过创建的对象
for(int i=0;i<Ans;i++) {
System.out.println("请输入要购买的车辆对应序号");
int xuhao=input.nextInt();
list[i]=new Cheku(xuhao);
if(list[i].load==0) {
manSum++;//累计客车的数量
sumPrice+=list[i].price;
}
if(list[i].manN==0) {
loadSum++;//累计货车的数量
sumPrice+=list[i].price;
}
if(list[i].load!=0&&list[i].manN!=0) {
manSum++;
loadSum++;
sumPrice+=list[i].price;
}
}
String loadListName[]=new String[loadSum];//通过货车数量建立货车数组
String manListName[]=new String[manSum];//通过客车数量建立客车数组
int man_sum=0;//用于保存累计的可载客人数
int load_sum=0;//用于保存累计的可载货重量
int j=0;
int o=0;
for(int i=0;i<list.length;i++) {
if(list[i].load==0) {
manListName[j]=list[i].name;
man_sum+=list[i].manN;
j++;
}
if(list[i].manN==0) {
loadListName[o]=list[i].name;
load_sum+=list[i].load;
o++;
}
if(list[i].load!=0&&list[i].manN!=0) {
manListName[j]=list[i].name;
loadListName[o]=list[i].name;
j++;
o++;
}
}
System.out.println("您的账单:");
System.out.println("您所订购的可载人的车辆:"+Arrays.deepToString(manListName));
System.out.println("共可载人"+man_sum+"人");
System.out.println("您所订购的可载货的车辆:"+Arrays.deepToString(loadListName));
System.out.println("共可载重"+load_sum+"吨");
System.out.println("您总共需要支付:"+sumPrice+"元");
}
else {
System.out.println("期待为您服务!");
}
}
}


写回答 关注

2回答

  • zhyyyq
    2019-09-10 20:59:37
    已采纳

    package com.imooc;


    import java.util.HashMap;

    import java.util.Iterator;

    import java.util.Scanner;


    public class Main {

    public static HashMap<Integer,Car> carList  = new HashMap<Integer,Car>();

    public static void main(String[] args) {

    if(login())

    {

    init();

    System.out.println("您可租赁的车辆的类型和价目如下所示");

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

    Iterator<Integer> it =carList.keySet().iterator();

    while(it.hasNext())

    {

    int i =  it.next();

    System.out.print(i+".");

    carList.get(i).show();

    }

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

    Scanner in = new Scanner(System.in);

    int sum=0;

    for(int i=1 ,b=in.nextInt();i<=b;i++)

    {

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

    sum+=carList.get(in.nextInt()).price;

    }

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

    System.out.println("您的账单为"+sum*in.nextInt());

    in.close();

    }

    }

    public static boolean login() {

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

    System.out.println("恁是否要租车:1yes 2no");

    Scanner in = new Scanner(System.in);

    while(true) {

    int a = in.nextInt();

    if(a==1)

    {

    return true;

    }else if(a==0) {

    return false;

    }

    else {

    System.out.println("plz input the valid number!");

    }

    }

    }

    public static void init() {

    carList.put(1, new WagenCar("奥迪A4", 500, 4));

    carList.put(2, new WagenCar("马自达6", 400, 40));

    carList.put(3, new PickUpCar("皮卡路6", 4500, 2, 4));

    carList.put(4, new WagenCar("金龙", 800, 20));

    carList.put(5, new CargoCar("松花江", 400, 4));

    carList.put(6, new CargoCar("依维柯", 1000, 20));

    }

    }

    这样写

    无限记录者

    哦哦 好的 多谢指点

    2019-09-11 11:24:49

    共 1 条回复 >

  • zhyyyq
    2019-09-10 17:17:15

    别用switch,用hashMap放键值对<number,car>

    weixin...

    能不能把carList的代码也发一下谢谢了

    2019-09-25 22:00:46

    共 2 条回复 >

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530093 学习 · 6086 问题

查看课程

相似问题