栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java入门课程第二季作业

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java入门课程第二季作业

哒哒租车系统
细细看了6-1的需求之后耗时3.5h将功能写出,然后又花费1.5h解耦合和其他小优化便于日后维护以及扩展新功能
基本思想:

  1. 多态的使用。利用多态,尽可能让代码更加清晰。多态的基本使用:继承-重写-向上转型。这个案例多态主要用在属性以及pri()方法上:建立Car[]数组(引用指向子类对象,向上转型),然后可以利用此数组直接去访问id等父类属性,实现统一处理;另外子类对象中已重写父类重新方法pri(),通过Car[]数组(向上转型),遍历调用pri()方法即可实现可租车型信息输出。
  2. 解耦合,实现一个类的功能尽可能的明朗。如主函数类Car_RentSystem,其中主函数只有一条语句,实例化一个Car_RentSystem对象,功能调用放在其构造方法中。功能实现的主要语句分离到Car_Admin类中,日后如需扩展功能,可直接在此类中进行而无须对更改主函数所在类以及Car类

Car类

package rent_car_system;

public abstract class Car {
    
    int id = 0;
    String name = null;
    int price = 0;
    int cargo_capacity = 0;
    int passenger_capacity = 0;

    
    public void Car(int id, String name, int price, int passenger_capacity, int cargo_capacity) {
 this.id = id;
 this.name = name;
 this.price = price;
 this.passenger_capacity = passenger_capacity;
 this.cargo_capacity = cargo_capacity;
    }

    
    public abstract void pri();
}


class Passenger_Car extends Car {
    public Passenger_Car(int id, String name, int price, int capacity) {
 super.Car(id, name, price, capacity, 0);
    }
    public void pri(){
 System.out.println(id + "t" + name + "t" + price + " 元/天t" + passenger_capacity + "人t");
    }
}

class Cargo_Car extends Car {
    public Cargo_Car(int id, String name, int price, int capacity) {
 super.Car(id, name, price, 0, capacity);
    }
    public void pri(){
 System.out.println(id + "t" + name + "t" + price + " 元/天t" + cargo_capacity + "吨t");
    }
}

class Pickup_Car extends Car {
    public Pickup_Car(int id, String name, int price, int passenger_capacity, int cargo_capacity) {
 super.Car(id, name, price, passenger_capacity, cargo_capacity);
    }
    public void pri(){
 System.out.println(id + "t" + name + "t" + price + " 元/天t" +
  passenger_capacity + "人t" + "&t" + cargo_capacity + "吨t");
    }
}

管理系统功能实现类

package rent_car_system;

import java.util.Scanner;



class Car_Admin{
    int sum_passenger=0;
    int sum_cargo=0;
    int sum_price=0;
    StringBuilder pri_passenger=new StringBuilder();
    StringBuilder pri_cargo=new StringBuilder();
    Scanner scanner = new Scanner(System.in);
    public Car[] Car_message(){
 
 Car[] cars = {
  new Passenger_Car(1, "奥 迪", 500, 4),
  new Passenger_Car(2, "马自达", 400, 6),
  new Pickup_Car(3,"皮 卡",450,4,4),
  new Passenger_Car(4, "金 龙", 800, 15),
  new Cargo_Car(5, "松花江", 400, 10),
  new Cargo_Car(6, "依维柯", 1000, 20)
 };
 System.out.println("你可租车的类型及价目表");
 System.out.println("ID  汽车品牌    价格    容量");
 for (int i=0;i0) {
  pri_passenger.append(cars[id-1].name+"t");
     }
     //如果是载货车型,则添加至字符串尾部
     if (cars[id-1].cargo_capacity >0) {
  pri_cargo.append(cars[id-1].name+"t");
     }
 }

 //此部分语句功能为明细输出
 System.out.println("----------租车明细表----------");
 System.out.println("载客类型:"+pri_passenger+"n共载人:"+sum_passenger+"人");
 System.out.println("载货类型:"+pri_cargo+"n共载货:"+sum_cargo+"吨");
 System.out.println("----------------------------");

 //调用同类中的方法,计算总价格
 this.price_bill(sum_price);
    }

    
    public void price_bill(int sum_price){
 System.out.println("请输入你要租的天数:");
 int days=scanner.nextInt();
 System.out.println("-----租车总价格-----");
 System.out.println("您的租车费用为:"+days*sum_price+"元");
    }

}

主函数类

package rent_car_system;

import java.util.Scanner;



public class Car_RentSystem {
    Scanner scanner = new Scanner(System.in);

    
    public static void main(String[] args) {
 Car_RentSystem car_rentSystem = new Car_RentSystem();
    }

    
    public Car_RentSystem() {
 System.out.println("-----欢迎使用哒哒租车系统-----");
 System.out.println("是否要租车:输入'Y'进入系统");
 //建立死循环,当用户不想租车或已选定方案时跳出循环
 
 while (true) {
     if (scanner.next().equals("Y")) {
  //初始化系统,展示可选车型等信息
  Car_Admin car_admin = new Car_Admin();
  Car[] cars = car_admin.Car_message();
  //提供租车数量选择功能,并打印出租车明细表及总价格
  car_admin.car_admin_function(cars);
     } else {
  //安全退出此系统
  System.out.println("稍后退出系统,感谢您的使用");
  System.exit(0);
     }
     System.out.println();
     System.out.println("查看其他搭配及价格?输入'N'退出系统");
     if(scanner.next().equals("N")){
  break;
     }
 }
    }
}

执行结果如下所示:

/usr/lib/jvm/java-12-oracle/bin/java -javaagent:/opt/idea/idea-IC-192.5728.98/lib/idea_rt.jar=41569:/opt/idea/idea-IC-192.5728.98/bin -Dfile.encoding=UTF-8 -classpath /home/yq/IdeaProjects/out/production/SortCollection:/opt/idea/idea-IC-192.5728.98/plugins/Kotlin/kotlinc/lib/kotlin-stdlib.jar:/opt/idea/idea-IC-192.5728.98/plugins/Kotlin/kotlinc/lib/kotlin-reflect.jar:/opt/idea/idea-IC-192.5728.98/plugins/Kotlin/kotlinc/lib/kotlin-test.jar:/opt/idea/idea-IC-192.5728.98/plugins/Kotlin/kotlinc/lib/kotlin-stdlib-jdk7.jar:/opt/idea/idea-IC-192.5728.98/plugins/Kotlin/kotlinc/lib/kotlin-stdlib-jdk8.jar rent_car_system.Car_RentSystem
-----欢迎使用哒哒租车系统-----
是否要租车:输入'Y'进入系统
Y
你可租车的类型及价目表
ID  汽车品牌    价格    容量
1	奥 迪	500 元/天	4人	
2	马自达	400 元/天	6人	
3	皮 卡	450 元/天	4人	&	4吨	
4	金 龙	800 元/天	15人	
5	松花江	400 元/天	10吨	
6	依维柯	1000 元/天	20吨	
请输入你要租的汽车数量:
3
请输入第1辆车的序号
1
请输入第2辆车的序号
2
请输入第3辆车的序号
3
----------租车明细表----------
载客类型:奥 迪	马自达	皮 卡	
共载人:14人
载货类型:皮 卡	
共载货:4吨
----------------------------
请输入你要租的天数:
3
-----租车总价格-----
您的租车费用为:4050元

查看其他搭配及价格?输入'N'退出系统
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号