package?com.runoob.test;
public?class?Cheku?{
String?name;
double?price;
double?load;
int?manN;
public?Cheku(int?xuhao)?{
switch?(xuhao)?{
case?1:
this.name="奧迪A6";
this.price=500;
this.load=0;
this.manN=4;
break;
case?2:
this.name="馬自達(dá)6";
this.price=400;
this.load=0;
this.manN=4;
break;
case?3:
this.name="皮卡雪6";
this.price=450;
this.load=2;
this.manN=4;
break;
case?4:
this.name="金龍";
this.price=800;
this.load=0;
this.manN=4;
break;
case?5:
this.name="松花江";
this.price=400;
this.load=4;
this.manN=0;
break;
case?6:
this.name="依維柯";
this.price=1000;
this.load=20;
this.manN=0;
break;
}
}
}
package?com.runoob.test;
import?java.util.Scanner;
import?java.util.Arrays;
public?class?Dada?{
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
System.out.println("歡迎使用答答租車系統(tǒng):");
System.out.println("您是否要租車:1-是??0-否");
Scanner?input=new?Scanner(System.in);
int?ans=input.nextInt();
if(ans==1)?{
System.out.println("您可租車的類型及其價(jià)目表:");
System.out.println("1,奧迪A4500元/天載人:4人");
System.out.println("2,馬自達(dá)6400元/天載人:4人");
System.out.println("3,皮卡雪6450元/天載人:4人?載貨:2噸");
System.out.println("4,金龍800元/天載人:20人");
System.out.println("5,松花江400元/天載貨:4噸");
System.out.println("6,依維柯1000元/天載貨:20噸");
System.out.println("請(qǐng)輸入要租的車輛數(shù)量");
int?Ans=input.nextInt();//確定創(chuàng)建對(duì)象的次數(shù)
Cheku?[]?list=new?Cheku[Ans];//這個(gè)對(duì)象數(shù)組用于保存創(chuàng)建好的對(duì)象
int?loadSum=0;//貨車的數(shù)量
int?manSum=0;//客車的數(shù)量
double?sumPrice=0;//保存累計(jì)的金額
//通過(guò)創(chuàng)建的對(duì)象
for(int?i=0;i<Ans;i++)?{
System.out.println("請(qǐng)輸入要購(gòu)買的車輛對(duì)應(yīng)序號(hào)");
int?xuhao=input.nextInt();
list[i]=new?Cheku(xuhao);
if(list[i].load==0)?{
manSum++;//累計(jì)客車的數(shù)量
sumPrice+=list[i].price;
}
if(list[i].manN==0)?{
loadSum++;//累計(jì)貨車的數(shù)量
sumPrice+=list[i].price;
}
if(list[i].load!=0&&list[i].manN!=0)?{
manSum++;
loadSum++;
sumPrice+=list[i].price;
}
}
String?loadListName[]=new?String[loadSum];//通過(guò)貨車數(shù)量建立貨車數(shù)組
String?manListName[]=new?String[manSum];//通過(guò)客車數(shù)量建立客車數(shù)組
int?man_sum=0;//用于保存累計(jì)的可載客人數(shù)
int?load_sum=0;//用于保存累計(jì)的可載貨重量
int?j=0;
int?o=0;
for(int?i=0;i<list.length;i++)?{
if(list[i].load==0)?{
manListName[j]=list[i].name;
man_sum+=list[i].manN;
j++;
}
if(list[i].manN==0)?{
loadListName[o]=list[i].name;
load_sum+=list[i].load;
o++;
}
if(list[i].load!=0&&list[i].manN!=0)?{
manListName[j]=list[i].name;
loadListName[o]=list[i].name;
j++;
o++;
}
}
System.out.println("您的賬單:");
System.out.println("您所訂購(gòu)的可載人的車輛:"+Arrays.deepToString(manListName));
System.out.println("共可載人"+man_sum+"人");
System.out.println("您所訂購(gòu)的可載貨的車輛:"+Arrays.deepToString(loadListName));
System.out.println("共可載重"+load_sum+"噸");
System.out.println("您總共需要支付:"+sumPrice+"元");
}
else?{
System.out.println("期待為您服務(wù)!");
}
}
}
2019-09-10
package com.imooc;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
public static HashMap<Integer,Car> carList? = new HashMap<Integer,Car>();
public static void main(String[] args) {
if(login())
{
init();
System.out.println("您可租賃的車輛的類型和價(jià)目如下所示");
System.out.println("序號(hào) 汽車名稱 租金 容量");
Iterator<Integer> it =carList.keySet().iterator();
while(it.hasNext())
{
int i =? it.next();
System.out.print(i+".");
carList.get(i).show();
}
System.out.println("請(qǐng)輸入租車的數(shù)量:");
Scanner in = new Scanner(System.in);
int sum=0;
for(int i=1 ,b=in.nextInt();i<=b;i++)
{
System.out.println("請(qǐng)輸入第"+i+"輛車的序號(hào):");
sum+=carList.get(in.nextInt()).price;
}
System.out.println("請(qǐng)輸入租車的天數(shù):");
System.out.println("您的賬單為"+sum*in.nextInt());
in.close();
}
}
public static boolean login() {
System.out.println("歡迎使用噠噠租車系統(tǒng):");
System.out.println("恁是否要租車:1yes 2no");
Scanner in = new Scanner(System.in);
while(true) {
int a = in.nextInt();
if(a==1)
{
return true;
}else if(a==0) {
return false;
}
else {
System.out.println("plz input the valid number!");
}
}
}
public static void init() {
carList.put(1, new WagenCar("奧迪A4", 500, 4));
carList.put(2, new WagenCar("馬自達(dá)6", 400, 40));
carList.put(3, new PickUpCar("皮卡路6", 4500, 2, 4));
carList.put(4, new WagenCar("金龍", 800, 20));
carList.put(5, new CargoCar("松花江", 400, 4));
carList.put(6, new CargoCar("依維柯", 1000, 20));
}
}
這樣寫
2019-09-10
別用switch,用hashMap放鍵值對(duì)<number,car>