Java oop :租車系統(tǒng)

本期看點(diǎn):
// 神州租車系統(tǒng)
// 聲明一個(gè)車的數(shù)組,存儲(chǔ)各種貨車,客車和皮卡車
// 1、聲明用戶類記錄用戶名和密碼
// 2、聲明貨車類,客車類和皮卡車類繼承自車類
// 3、聲明載物和載人的接口,貨車?yán)^承載物的接口
// 客車?yán)^承載人的接口,皮卡車?yán)^承載人載物的接口
// 使用多態(tài)數(shù)組初始化車輛的信息,打印并計(jì)算租車的總價(jià)格
package a;//U12復(fù)習(xí)課2_租車系統(tǒng)
import java.util.Scanner;
public class? Student{//demo1
public static void main(String[] args) {
che xiaohuoche=new huoche(1,"小貨車",30,0,3);
che dahuoche=new huoche(2,"大貨車",50,0,5);
che xiaokeche=new keche(3,"小客車",40,4,0);
che dakeche=new keche(4,"大客車",80,10,0);
che xiaopika=new pika(5,"小皮卡",60,2,4);
che dapika=new pika(6,"大皮卡",100,4,8);
che[] c=new che[6];
c[0]=xiaohuoche;c[1]=dahuoche;c[2]=xiaokeche;
c[3]=dakeche;c[4]=xiaopika;c[5]=dapika;
yonghu yh=new yonghu();
che[] c_zuche = null;
while(true){
System.out.println("---------神舟租車系統(tǒng)-----------");
System.out.println("請(qǐng)輸入你的操作:1.登錄? 2.注冊(cè)? 3.退出系統(tǒng)");
Scanner s=new Scanner(System.in);
int num=s.nextInt();
if(num==1){
System.out.println("----執(zhí)行登錄操作----");
System.out.println("請(qǐng)輸入用戶名:");
String uname=s.next();
System.out.println("請(qǐng)輸入你的密碼");
String pwd=s.next();
if(uname.equals(yh.uname)&&pwd.equals(yh.pwd)){
System.out.println("登錄成功!");
break;
}else{
System.out.println("登錄失敗");
}
}else if(num==2){
System.out.println("----執(zhí)行注冊(cè)操作----");
System.out.println("請(qǐng)輸入用戶名:");
String uname=s.next();
System.out.println("請(qǐng)輸入你的密碼");
String pwd=s.next();
System.out.println("請(qǐng)輸入你的姓名");
String name=s.next();
yh.uname=uname;
yh.pwd=pwd;
yh.name=name;
System.out.println("注冊(cè)成功!");
}else if(num==3){
System.exit(0);
}
}
//租車操作
while(true)/*死循環(huán),while(flase)為永不循環(huán)*/{
System.out.println("歡迎你,"+yh.name+"請(qǐng)選擇你的操作:");
System.out.println("1.查看車輛列表? 2.進(jìn)行租車? 3.查看我的租車數(shù)? 4.退出系統(tǒng)");
Scanner s1=new Scanner(System.in);
int num=s1.nextInt();
if(num==1){
for(che i:c){/*相當(dāng)于for(int i=0;i<c.length;i++)*/
i.jieshao();
}
}else if(num==2){
System.out.println("請(qǐng)輸入你要租的車輛數(shù)");
int num1=s1.nextInt();
c_zuche=new che[num1];
for(int i=0;i<num1;i++){
System.out.println("請(qǐng)輸入第"+(i+1)+"車輛的編號(hào)");
int bianhao=s1.nextInt();
switch(bianhao){
case 1:
c_zuche[i]=xiaohuoche;break;
case 2:
c_zuche[i]=dahuoche;break;
case 3:
c_zuche[i]=xiaokeche;break;
case 4:
c_zuche[i]=dakeche;break;
case 5:
c_zuche[i]=xiaopika;break;
case 6:
c_zuche[i]=dapika;break;
}
}
System.out.println("租車成功!");
}else if(num==3){
if(c_zuche==null){
System.out.println("你還沒(méi)有進(jìn)行租車呢!");
}else{
int zongjiage=0;
int zongzairenliang=0;
int zongzaiwuliang=0;
for(che i:c_zuche){
i.jieshao();
zongjiage+=i.jiage;
zongzairenliang+=i.zaikeliang;
zongzaiwuliang+=i.zaiwuliang;
}
System.out.println("總價(jià)格"+zongjiage+"總載人量"+
zongzairenliang+"總載物量"+zongzaiwuliang);
}
}else{
System.exit(0);/*終止系統(tǒng),不可為break,break是跳出循環(huán)*/
}
}
}
}
class yonghu{
String uname;
String pwd;
String name;
}
abstract class che{
/*abstract抽象父類可以改為普通父類,通過(guò)以下方法:刪掉class 和void前面的abstract,在類中的方法中加上打印等語(yǔ)句等。*/
int bianhao;
String name;
int jiage;
int zaikeliang;
int zaiwuliang;
public che(int bianhao, String name, int jiage, int zaikeliang,
int zaiwuliang) {
this.bianhao = bianhao;
this.name = name;
this.jiage = jiage;/*因?yàn)閏he[] c=new che[6];聲明在前面,即全局,故this.jiage或jiage等帶點(diǎn)的在全局都可自己用?,若聲明在class中this.jiage或jiage等帶點(diǎn)的就只可在class里面直接用*/
this.zaikeliang = zaikeliang;
this.zaiwuliang=zaiwuliang;
}
abstract void jieshao();
}
class huoche extends che {
public huoche(int bianhao, String name, int jiage,?
int zaikeliang,int zaiwuliang) {
super(bianhao, name, jiage, zaikeliang,zaiwuliang);
}
void jieshao() {
System.out.println("編號(hào)"+this.bianhao+"\t名稱"+this.name+
"\t價(jià)格"+this.jiage+"\t載物量"+this.zaiwuliang);
}
}
class keche extends che{
public keche(int bianhao, String name, int jiage, int zaikeliang,
int zaiwuliang) {
super(bianhao, name, jiage, zaikeliang, zaiwuliang);
}
void jieshao() {
System.out.println("編號(hào)"+this.bianhao+"\t名稱"+
this.name+"\t價(jià)格"+this.jiage+"\t載人量"+this.zaikeliang);
}
}
class pika extends che{
public pika(int bianhao, String name, int jiage, int zaikeliang,
int zaiwuliang) {
super(bianhao, name, jiage, zaikeliang, zaiwuliang);
}
void jieshao() {
System.out.println("編號(hào)"+this.bianhao+"\t名稱"+
this.name+"\t價(jià)格"+this.jiage+"\t載人量"+this.zaikeliang+"載物量"+
this.zaiwuliang);
}
}