Practice of station number and station name of java Metro Line 2

Keywords: less

1. The corresponding relationship between station number and station name of Guangzhou Metro Line 2 is as follows:
1 = Guangzhou south railway station, 2 = Shibi, 3 = Huijiang, 4 = Nanpu, 5 = Luoxi, 6 = Nanzhou, 7 = dongxiaonan, 8 = Jiangtai Road, 9 = Changgang, 10 = Jiangnanxi, 11 = Shiergong, 12 = Haizhu Square, 13 = front of the park, 14 = Memorial Hall, 15 = Yuexiu Park, 16 = Guangzhou railway station, 17 = Sanyuanli, 18 = Feixiang Park, 19 = Baiyun Park, 20 = Baiyun culture square
21 = Xiaogang, 22 = Jiangxia, 23 = huangbian, 24 = jiahewanggang
Store the data of the above corresponding relationship in the map set, key: stands for station number, value: stands for station name, and traverse printing
2. Rules for calculating subway fare:
2 yuan for total 3 stops (including 3 stops),
4 yuan for more than 3 stations but not more than 6 stations (including 6 stations),
If there are more than 6 stations, on the basis of 4 yuan, 1 yuan will be added for each additional 2 stations. If there are less than 2 stations, 2 stations will be added,
Top 10 yuan;
3. Print format (it is necessary to judge the last station and the arrival station entered by the keyboard. If there is no such station, prompt for re-entry,
Until station name exists): Note: 2 minutes per station
Please enter the upper station:
Shahe
The boarding station you entered: [Shahe] does not exist. Please re-enter the boarding station:
Upper ground
The boarding station you entered: [boarding] does not exist, please re-enter the boarding station:
Guangzhou South Railway Station
Please enter the arrival station:
Shahe
The destination you entered: [Shahe] does not exist. Please re-enter the destination:
Xi Er Qi
The arrival station you entered: [Xierqi] does not exist, please re-enter the arrival station:
Haizhu Square
From Guangzhou south railway station to Haizhu Square, there are 11 stations, 9 yuan, about 22 minutes

public class Station {

	private Map<Integer,String> map = new HashMap<Integer, String>();
	/**
	 * Initialize map site data
	 */
	public void initMap(){
		String s = "1=Guangzhou south railway station, 2=Shek Pik, 3=Hui Jiang, 4=Nanpu, 5=Luoxi, 6=Nan Zhou, 7=Dongxiaonan, 8=Jiangtai Road, 9=Changgang, 10=Jiangnan West, 11=Shiergong, 12=Haizhu Square, 13=In front of the park, 14=Memorial Hall, 15=Yuexiu Park, 16=Guangzhou railway station, 17=Sanyuanli, 18=Flying Park, 19=Baiyun Park, 20=Baiyun culture square, 21=Xiao Gang, 22=Jiangxia, 23=Huang Bian, 24=Wang Gang, Jiahe";
		//Store site information into map object
		String[] array = s.split(",");
		for(String str:array) {
			String[] array2 = str.split("=");
			map.put(Integer.parseInt(array2[0]), array2[1]);
		}
	}
	/**
	 * Traverse all elements of map
	 */
	public void showMap() {
		//Traverse all elements of map
		for(Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();it.hasNext();) {
			Map.Entry<Integer, String> e = it.next();
			System.out.println("The first"+e.getKey()+"Station:"+e.getValue());
		}
	}
	/**
	 * Get all site information
	 * @return
	 */
	public Collection<String> getStations(){
		//Get all sites
		return map.values();
	}
	/**
	 * Get site number according to site name
	 * @param name	Site name
	 * @return
	 */
	public int getNoByName(String name) {
		for(Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();it.hasNext();) {
			Map.Entry<Integer, String> e = it.next();
			if(e.getValue().equals(name)) {
				return e.getKey();
			}
		}
		return 0;
	}
	/**
	 * Calculate the ticket price according to the station number
	 * @param upNo		Boarding Station No
	 * @param downNo	Arrival station No
	 * @return
	 */
	public int getPrice(int upNo,int downNo) {
		int cha = Math.abs(downNo-upNo);//Absolute value
		int price = 0;
		if(cha<=3) {
			price = 2;
		}else if(cha<=6) {
			price = 4;
		}else if(cha<=16) {
			int a = (cha-6)/2;
			if((cha-6)%2!=0) {
				a++;
			}
			price = 4 + a;
		}else {
			price = 10;
		}
		return price;
	}
}

public class Test2 {

	public static void main(String[] args) {
		
		Station s = new Station();
		s.initMap();
		s.showMap();
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the upper station:");
		//Get all sites
		Collection<String> list = s.getStations();
		//Name of boarding station
		String up = null;
		//Name of arrival station
		String down = null;
		while(true) {
			up = sc.next();
			if(list.contains(up)) {
				break;
			}
			System.out.println("Boarding station you entered:["+up+"]No, please re-enter the previous station:");
		}
		System.out.println("Please enter the arrival station:");
		while(true) {
			down = sc.next();
			if(list.contains(down)) {
				break;
			}
			System.out.println("Destination you entered:["+down+"]No, please re-enter the arrival station:");
		}
		int upNo = s.getNoByName(up);
		int downNo = s.getNoByName(down);
		int cha = downNo-upNo;
		int price = s.getPrice(upNo, downNo);
		System.out.println("From ("+up+"]To ["+down+"]A total of"+cha+"Station toll"+price+"Yuan, about "+(cha*2)+"Minute");
	}
}

Published 7 original articles, won praise 6, visited 159
Private letter follow

Posted by wannalearnit on Tue, 10 Mar 2020 20:32:42 -0700