Java课程主观题作业_Java入门课程第二季作业

哒哒租车系统

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

基本思想:

多态的使用。利用多态,尽可能让代码更加清晰。多态的基本使用:继承-重写-向上转型。这个案例多态主要用在属性以及pri()方法上:建立Car[]数组(引用指向子类对象,向上转型),然后可以利用此数组直接去访问id等父类属性,实现统一处理;另外子类对象中已重写父类重新方法pri(),通过Car[]数组(向上转型),遍历调用pri()方法即可实现可租车型信息输出。

解耦合,实现一个类的功能尽可能的明朗。如主函数类Car_RentSystem,其中主函数只有一条语句,实例化一个Car_RentSystem对象,功能调用放在其构造方法中。功能实现的主要语句分离到Car_Admin类中,日后如需扩展功能,可直接在此类中进行而无须对更改主函数所在类以及Car类

Car类

package rent_car_system;

public abstract class Car {

/**

* 车型常用属性:id,name,price,cargo_capacity,passenger_capacity

*/

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;

}

/**

* 提供抽象方法,当子类向上转型时,可以调用各自的重写方法

* 实现多态步骤:1.继承;2.重写;3向上转型

*/

public abstract void pri();

}

/**

* 载客车型类,重写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的属性,如id等,父类方法

*/

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;i

cars[i].pri();

}

return cars;

}

/**

* 系统处理用户搭配信息,并输出明细

* @param cars 租车信息

*/

public void car_admin_function(Car[] cars){

//客户自由选择数量和车型

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

int number=scanner.nextInt();

for(int i=0;i

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

int id=scanner.nextInt();

//由于载货车型的载人容量已初始化为0(其他也如此),故直接相加即为所求

sum_passenger+=cars[id-1].passenger_capacity;

sum_cargo+=cars[id-1].cargo_capacity;

sum_price+=cars[id-1].price;

//如果是载客车型,则添加至字符串尾部

if (cars[id-1].passenger_capacity>0) {

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);

}

/**

* 输入天数后,即可计算总价格=天数*每天的租金

* @param 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);

/**

* 程序入口

* @param args

*/

public static void main(String[] args) {

Car_RentSystem car_rentSystem = new Car_RentSystem();

}

/**

* 提供租车系统主体管理功能

*/

public Car_RentSystem() {

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

System.out.println("是否要租车:输入'Y'进入系统");

//建立死循环,当用户不想租车或已选定方案时跳出循环

/**

* TODO:if语句块中的内容为可扩展项(其他部分均为框架),思考如何将此部分分离出来,降低耦合性??

*/

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'退出系统

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java主观判断代码的示例: ``` public class SubjectiveQuestion { private String question; private String expectedAnswer; public SubjectiveQuestion(String question, String expectedAnswer) { this.question = question; this.expectedAnswer = expectedAnswer; } public boolean isCorrect(String answer) { return answer.equals(expectedAnswer); } public String getQuestion() { return question; } public String getExpectedAnswer() { return expectedAnswer; } } ``` 这个类表示一个主观,它包含一个问和预期答案。它有一个名为isCorrect的方法,该方法接受一个实际答案并返回一个布尔值,表示该答案是否与预期答案匹配。它还有两个getter方法,分别用于获取问和预期答案。 使用该类的示例代码: ``` SubjectiveQuestion question = new SubjectiveQuestion("What is the capital of France?", "Paris"); // Answer the question with incorrect answer String answer1 = "London"; System.out.println(question.isCorrect(answer1)); // false // Answer the question with correct answer String answer2 = "Paris"; System.out.println(question.isCorrect(answer2)); // true ``` 此示例创建一个主观,即“法国的首都是什么?”,预期答案为“巴黎”。然后,它尝试使用“伦敦”和“巴黎”答案来回答问。第一个答案不正确,第二个答案是正确的。由于我们在Question类中使用的判断方法,它将返回正确的布尔值,以反映答案是否正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值