完成了一個簡單的租車系統。實現簡單的租車功能,用戶選擇租車序號和每輛車所租的天數,計算計程車所需要的費用。 /* * 使用gbk編碼 */ package adad; import java.util.Scanner; import javax.print.DocFlavor.INPUT_STREA
完成了一個簡單的租車系統。實現簡單的租車功能,用戶選擇租車序號和每輛車所租的天數,計算計程車所需要的費用。
/* * 使用gbk編碼 */ package adad; import java.util.Scanner; import javax.print.DocFlavor.INPUT_STREAM; import org.omg.CORBA.PUBLIC_MEMBER; /* * 創建汽車類 */ abstract class car{ public void man(){ System.out.println(""); } public void huo() { System.out.println(""); } public abstract void Jisuan(int day); } /* * 創建一個汽車類的子類 */ class aodi extends car{ int xuhao=1; //設置第一輛車的序號 String name="奧迪A4"; int price=500; //設置汽車的租金 int rongliang=4; //設置汽車的容量 double money; public void man(){ System.out.println(xuhao+" "+name+" "+price+"元/天"+" 載人:"+rongliang+"人"); } public void Jisuan(int day) { money =price*day; } } class mazida extends car{ int xuhao=2; String name="馬自達6"; int price=400; int rongliang=4; double money; public void man(){ System.out.println(xuhao+" "+name+" "+price+"元/天"+" 載人:"+rongliang+"人"); } public void Jisuan(int day) { money =price*day; } } class pika extends car{ int xuhao=3; String name="皮卡"; int price=400; int rongliang=4; int huoliang=2; double money; public void man(){ System.out.println(xuhao+" "+name+" "+price+"元/天"+" 載人:"+rongliang+"人"+" 載貨:"+huoliang+"噸"); } public void Jisuan(int day) { money =price*day; } } class jinlong extends car{ int xuhao=4; String name="金龍"; int price=800; int rongliang=20; double money; public void man(){ System.out.println(xuhao+" "+name+" "+price+"元/天"+" 載人:"+rongliang+"人"); } public void Jisuan(int day) { money =price*day; } } class songhuajiang extends car{ int xuhao=5; String name="松花江"; int price=400; int huoliang=4; double money; public void huo(){ System.out.println(xuhao+" "+name+" "+price+"元/天"+" 載貨:"+huoliang+"噸"); } public void Jisuan(int day) { money =price*day; } } class yiweike extends car{ int xuhao=6; String name="依維柯"; int price=1000; int huoliang=20; double money; public void huo(){ System.out.println(xuhao+" "+name+" "+price+"元/天"+" 載貨:"+huoliang+"噸"); } public void Jisuan(int day) { money =price*day; } } public class dadadad { public static void main(String[] args) { aodi ao=new aodi();mazida ma=new mazida();pika pi=new pika();jinlong jin=new jinlong();songhuajiang song=new songhuajiang();yiweike yi=new yiweike(); Scanner scan=new Scanner(System.in); System.out.println("歡迎使用租車系統:"); System.out.println("您是否租車:1是 0不是"); int sum=scan.nextInt(); if (sum==1) { System.out.println("您可租車的類型及價目:"); System.out.println("序號 "+"汽車名稱 "+" 租金 "+" 容量"); ao.man(); ma.man(); pi.man(); jin.man(); song.huo(); yi.huo(); }else { System.out.println("謝謝!"); System.exit(0); } System.out.println("請輸入需要租車的數量:"); int much=scan.nextInt(); for(int i=1;i<=much;i++){ System.out.println("請輸入第"+i+"輛車的編號:"); int Id=scan.nextInt(); System.out.println("請輸入第"+i+"輛車的租車天數:"); int day=scan.nextInt(); switch (Id) { case 1: ao.Jisuan(day); break; case 2: ma.Jisuan(day); break; case 3: pi.Jisuan(day); break; case 4: jin.Jisuan(day); break; case 5: song.Jisuan(day); break; case 6: yi.Jisuan(day); break; } } double money=ao.money+ma.money+pi.money+jin.money+song.money+yi.money; System.out.println("***********************"); System.out.println("租車總價為:"+money); } }
實現結果如下: