java学习第二季的项目代码
Vehicle类。是bus、truck、pickup的父类
package pmy.test;
/*
*Project: Lease System
*Comments: 汽车类,客车、火车、皮卡类的基类
*JDK version used: JDK1.8
*Author: Pmy
*Create Date: 2016-09-13
*/
public class Vehicle {
private String s_name;
private int i_price;
public void setName(String name){
this.s_name = name;
}
public String getName(){
return s_name;
}
public void setPrice(int price){
this.i_price = price;
}
public int getPrice(){
return i_price;
}
}
bus类
/*
*Project: Lease System
*Comments: 客车类,继承于vehicle类
*JDK version used: JDK1.8
*Author: Pmy
*Create Date: 2016-09-13
*/
public class Bus extends Vehicle {
private int i_personnum;
public void setPersonnum(int personnum){
this.i_personnum = personnum;
}
public int getPersonnum(){
return i_personnum;
}
public Bus(int price, String name,int personnum){
super();
super.setPrice(price);
super.setName(name);
this.i_personnum = personnum;
}
public String toString(){
return "租用一天" + super.getName() + "的费用是:" + super.getPrice() +
"元/天。此车型载客数为:" + this.getPersonnum() + "人";
}
}
truck类
package pmy.test;
/*
*Project: Lease System
*Comments: 卡车类,继承于vehicle类
*JDK version used: JDK1.8
*Author: Pmy
*Create Date: 2016-09-13
*/
public class Truck extends Vehicle {
private int i_carrynum;
public void setCarrynum(int carrynum){
i_carrynum = carrynum;
}
public int getCarrynum(){
return i_carrynum;
}
public Truck(int price, String name, int carrynum){
super();
super.setPrice(price);
super.setName(name);
this.i_carrynum = carrynum;
}
public String toString(){
return "租用一天" + super.getName() + "的费用是:" + super.getPrice() +
"元/天。此车型载货量为:" + this.getCarrynum() + "吨";
}
}
皮卡
package pmy.test;
/*
*Project: Lease System
*Comments: 皮卡,继承于vehicle类
*JDK version used: JDK1.8
*Author: Pmy
*Create Date: 2016-09-13
*/
public class Pickup extends Vehicle {
private int i_personnum;
private int i_carrynum;
public void setPersonnum(int personnum){
this.i_personnum = personnum;
}
public void setCarrynum(int carrynum){
this.i_carrynum = carrynum;
}
public int getPersonnum(){
return i_personnum;
}
public int getCarrynum(){
return i_carrynum;
}
public Pickup(int price, String name, int personnum, int carrynum){
super();
super.setPrice(price);
super.setName(name);
this.i_personnum = personnum;
this.i_carrynum = carrynum;
}
public String toString(){
return "租用一天" + super.getName() + "的费用是:" + super.getPrice() +
"元/天。此车型载货量为:" + this.getCarrynum() + "吨" + "载客量为:" +
this.getPersonnum() + "人";
}
}```
主程序:
import java.util.Scanner;
public class Lease {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用答答租车系统");
int i_sum_price = 0; //租用汽车总价
int i_sum_carrynum = 0; //总载货数
int i_sum_personnum = 0; //总载客数
StringBuffer buffer_s_busname = new StringBuffer();
StringBuffer buffer_s_truckname = new StringBuffer();
StringBuffer buffer_s_pickupname = new StringBuffer();
//租用汽车名称
System.out.println("您是否要租车:1是 0否");
Scanner input = new Scanner(System.in);
if(input.nextInt() == 0){
System.out.println("返回");
input.close();
return;
}
System.out.println("***进入答答租车页面***:");
System.out.println("***共有以下车型供选择***:");
Bus car1 = new Bus(500, "奥迪a4", 4);
Bus car2 = new Bus(400, "马自达6", 4);
Bus car3 = new Bus(800, "金龙", 20);
Pickup car4 = new Pickup(450, "皮卡雪6", 4, 2);
Truck car5 = new Truck(400, "松花江", 4);
Truck car6 = new Truck(1000, "依维柯", 20);
Vehicle[] vehicles = {car1, car2, car3, car4, car5, car6};
for(int i=0;i<vehicles.length;i++){
System.out.println((i+1) + "、" +vehicles[i].toString());
}
System.out.println("***请输入您想租用的汽车数量***");
int i_carnum = input.nextInt(); //租用汽车的数量
int[] i_car = new int[i_carnum]; //记录租车序号
for(int i=0;i<i_carnum;i++){
System.out.println("请输入第" + (i+1) +"辆车的序号:");
int i_number = input.nextInt();
if(i_number>=1&&i_number<=6){
i_car[i] = i_number;
}else{
System.out.println("***输入序号有误,请重输***");
i--;
}
}
for(int i=0;i<i_carnum;i++){
switch(i_car[i]){
case 1:
buffer_s_busname.append(car1.getName() + " ");
i_sum_price += car1.getPrice();
i_sum_personnum += car1.getPersonnum();
break;
case 2:
buffer_s_busname.append(car2.getName() + " ");
i_sum_price += car2.getPrice();
i_sum_personnum += car2.getPersonnum();
break;
case 3:
buffer_s_busname.append(car3.getName() + " ");
i_sum_price += car3.getPrice();
i_sum_personnum += car3.getPersonnum();
break;
case 4:
buffer_s_pickupname.append(car4.getName() + " ");
i_sum_price += car4.getPrice();
i_sum_personnum += car4.getPersonnum();
i_sum_carrynum += car4.getCarrynum();
break;
case 5:
buffer_s_truckname.append(car5.getName() + " ");
i_sum_price += car5.getPrice();
i_sum_carrynum += car5.getCarrynum();
break;
case 6:
buffer_s_truckname.append(car6.getName() + " ");
i_sum_price += car6.getPrice();
i_sum_carrynum += car6.getCarrynum();
break;
default:
break;
}
}
System.out.println("***请输入租用天数***");
int i_day = input.nextInt();
System.out.println("***您的租用清单如下***");
System.out.println("***您总共租用了" + i_carnum + "辆车***");
System.out.println(buffer_s_busname);
System.out.println(buffer_s_truckname);
System.out.println(buffer_s_pickupname);
System.out.println("***总载货量为:" + i_sum_carrynum + "吨***");
System.out.println("***总载人数:" + i_sum_personnum + "人***");
System.out.println("***总价格为:" + i_sum_price*i_day + "元***");
System.out.println("***谢谢惠顾***");
input.close();
}
}