暑假因为有事耽误了,本以为暑假完结windows程序设计的,唉,慢慢补吧,学校开了java,因为学过c++,所以觉java并不是太难,根据慕课网给的项目,做了这个租车系统
无非就是面向对象思想,定义一个抽象汽车类,然后实现各自类型的汽车类,比较基础,就不多说了
Auto.java
package hireAuto;
public abstract class Auto {
//抽象类没必要定义属性,但是取得车型,价格,容量都是一样的,不妨定义属性
String name;
int price;
int capacity;//表示载人
int capacity2;//表示载货
int a;//a为1则表示可以载人,反之不可
int b;//b为1则表示可以载货,反之不可
public String getName()
{
return name;
}
public int getPrice()
{
return price;
}
public int getCapacity()
{
return capacity;
}
public int getCapacity2()
{
return capacity2;
}
//只有运输不同
public abstract void transport();//运输,货车载人,客车载客
}
A.java
package hireAuto;
public class A extends Auto {
public A()
{
this.name="奥迪A4";
this.price=500;
this.capacity=4;
this.capacity2=0;
this.a=1;
this.b=0;
}
public void transport()
{
System.out.println(this.getName()+"\t"+this.getPrice()+"元/天\t\t"+"载人:"+this.getCapacity()+"人");
}
}
B.java
package hireAuto;
public class B extends Auto {
public B()
{
this.name="马自达6";
this.price=400;//单位:元/天
this.capacity=4;//单位:人
this.capacity2=0;
this.a=1;
this.b=0;
}
public int getPricePerDay()
{
return getPrice()*getCapacity();
}
public void transport()
{
System.out.println(this.getName()+"\t"+this.getPrice()+"元/天\t\t"+"载人:"+this.getCapacity()+"人");
}
}
C.java
package hireAuto;
public class C extends Auto {
int capacity2;//单位:吨
public C()
{
this.name="皮卡雪6";//即可载人,也可载货
this.price=450;//单位:元/天
this.capacity=4;//单位:人
this.capacity2=2;//单位:吨
this.a=1;
this.b=1;
}
public int getCapacity2()
{
return capacity2;
}
public void transport() {
// TODO Auto-generated method stub
System.out.println(getName()+"\t"+getPrice()+"元/天\t\t"+"载人:"+getCapacity()+"人 载货:"+getCapacity2()+"吨");
}
}
D.java
package hireAuto;
public class D extends Auto {
public D()
{
this.name="金龙";
this.price=800;//单位:元/天
this.capacity=20;//单位:人
this.capacity2=0;
this.a=1;
this.b=0;
}
@Override
public void transport() {
// TODO Auto-generated method stub
System.out.println(getName()+"\t"+getPrice()+"元/天\t\t"+"载人:"+getCapacity()+"人");
}
}
E.java
package hireAuto;
public class E extends Auto {
public E()
{
this.name="松花江";
this.price=400;//单位:元/天
this.capacity=0;
this.capacity2=4;
this.a=0;
this.b=1;
}
@Override
public void transport() {
// TODO Auto-generated method stub
System.out.println(getName()+"\t"+getPrice()+"元/天\t\t"+"载货:"+getCapacity()+"吨");
}
}
F.java
package hireAuto;
public class F extends Auto {
public F()
{
this.name="依维柯";
this.price=1000;//单位:元/天
this.capacity=0;//单位:吨
this.capacity2=20;
this.a=0;
this.b=1;
}
@Override
public void transport() {
// TODO Auto-generated method stub
System.out.println(getName()+"\t"+getPrice()+"元/天\t"+"载货:"+getCapacity()+"吨");
}
}
hireAutoSystem.java(相当于c++的主函数了)
package hireAuto;
import java.util.Scanner;
public class hireAutoSystem {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int count=6;//可租用的数量
System.out.println("欢迎来到滴滴租车系统");
System.out.println("是否租车:1(是),0(否)");
Scanner input=new Scanner(System.in);
int iSel=input.nextInt();
if(iSel==0)
{
System.out.println("欢迎使用此系统,再见");
return ;
}
System.out.println("下面是可租车的类型及价格等信息");
System.out.println("序号\t汽车型号\t租金\t\t容量");
Auto[] autoArr=new Auto[6];
autoArr[0]=new A();
autoArr[1]=new B();
autoArr[2]=new C();
autoArr[3]=new D();
autoArr[4]=new E();
autoArr[5]=new F();
for(int i=0;i<count;++i)
{
System.out.print(i+1+"\t");
autoArr[i].transport();
}
System.out.println("请输入要租车的数量(最大为"+count+"):");
int iCount=input.nextInt();
if(iCount>count)
{
System.out.println("对不起,输入数量有误!!!再见");
return;
}
int[] auto=new int[iCount];
System.out.println("请依次输入要租车的序号");
for(int i=0;i<iCount;++i)
auto[i]=input.nextInt()-1;
System.out.println("请输入租车天数");
int days=input.nextInt();
System.out.println("账单如下:");
System.out.println("可载人的车");
int totalPrice=0;
int iPerson=0;
int iGoods=0;
for(int i=0;i<iCount;++i)
{
totalPrice+=autoArr[auto[i]].price;
if(autoArr[auto[i]].a==1)
{
System.out.print(autoArr[auto[i]].getName()+"\t");
iPerson+=autoArr[auto[i]].getCapacity();
}
}
System.out.println("共载人:"+iPerson+"人");
System.out.println("可载货的车");
for(int i=0;i<iCount;++i)
if(autoArr[auto[i]].b==1)
{
System.out.print(autoArr[i].getName()+"\t");
iGoods+=autoArr[auto[i]].getCapacity2();
}
System.out.println("共载货:"+iGoods+"吨");
System.out.println("总价格:"+days*totalPrice);
}
}