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="馬自達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("歡迎使用答答租車系統:");
System.out.println("您是否要租車:1-是??0-否");
Scanner?input=new?Scanner(System.in);
int?ans=input.nextInt();
if(ans==1)?{
System.out.println("您可租車的類型及其價目表:");
System.out.println("1,奧迪A4500元/天載人:4人");
System.out.println("2,馬自達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("請輸入要租的車輛數量");
int?Ans=input.nextInt();//確定創建對象的次數
Cheku?[]?list=new?Cheku[Ans];//這個對象數組用于保存創建好的對象
int?loadSum=0;//貨車的數量
int?manSum=0;//客車的數量
double?sumPrice=0;//保存累計的金額
//通過創建的對象
for(int?i=0;i<Ans;i++)?{
System.out.println("請輸入要購買的車輛對應序號");
int?xuhao=input.nextInt();
list[i]=new?Cheku(xuhao);
if(list[i].load==0)?{
manSum++;//累計客車的數量
sumPrice+=list[i].price;
}
if(list[i].manN==0)?{
loadSum++;//累計貨車的數量
sumPrice+=list[i].price;
}
if(list[i].load!=0&&list[i].manN!=0)?{
manSum++;
loadSum++;
sumPrice+=list[i].price;
}
}
String?loadListName[]=new?String[loadSum];//通過貨車數量建立貨車數組
String?manListName[]=new?String[manSum];//通過客車數量建立客車數組
int?man_sum=0;//用于保存累計的可載客人數
int?load_sum=0;//用于保存累計的可載貨重量
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("您所訂購的可載人的車輛:"+Arrays.deepToString(manListName));
System.out.println("共可載人"+man_sum+"人");
System.out.println("您所訂購的可載貨的車輛:"+Arrays.deepToString(loadListName));
System.out.println("共可載重"+load_sum+"噸");
System.out.println("您總共需要支付:"+sumPrice+"元");
}
else?{
System.out.println("期待為您服務!");
}
}
}
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("您可租賃的車輛的類型和價目如下所示");
System.out.println("序號 汽車名稱 租金 容量");
Iterator<Integer> it =carList.keySet().iterator();
while(it.hasNext())
{
int i =? it.next();
System.out.print(i+".");
carList.get(i).show();
}
System.out.println("請輸入租車的數量:");
Scanner in = new Scanner(System.in);
int sum=0;
for(int i=1 ,b=in.nextInt();i<=b;i++)
{
System.out.println("請輸入第"+i+"輛車的序號:");
sum+=carList.get(in.nextInt()).price;
}
System.out.println("請輸入租車的天數:");
System.out.println("您的賬單為"+sum*in.nextInt());
in.close();
}
}
public static boolean login() {
System.out.println("歡迎使用噠噠租車系統:");
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("馬自達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放鍵值對<number,car>