JAVA implements simple parking system code

  • 2020-06-23 00:15:54
  • OfStack

In JAVA project, the plus number needs a parking fee system, so I will sort it out and share it with you. I hope it will be helpful to you.


<h1 accuse="qTitle" style="margin: 0px; padding: 0px; font-size: 16px; font-stretch: normal; line-height: 26px; font-family: "PingFang SC", "Lantinghei SC", "Microsoft YaHei", arial,  Song typeface , sans-serif, tahoma; word-break: break-all; position: relative; color: rgb(51, 51, 51);"><pre name="code" class="html"> Only in the parking lot 1 A parking n A long narrow passage for a car, and only 1 There is a gate for cars to enter and leave. Cars are in the parking lot by vehicle arrival time  10 
 From north to south (the gate is at the southernmost end, the first to arrive at the end 1 A car is parked at the northernmost end of the garage n A car, and later cars had to wait outside on the sidewalk, 1 As soon as a car leaves, it's at the top of the sidewalk 1 A car can drive in; When a car in the parking lot to leave, after it into the vehicle must first exit the yard to give way to the car, wait for the car out of the gate, other vehicles in the original order to enter the yard, each car parked in the parking lot when it leaves the parking lot must be based on the length of time it stay paid. Try to establish a simulation program for parking lot management according to the above requirements.  
 [Basic requirements] Simulate parking lot with stack, simulate off-road road with queue, and simulate management according to input data sequence read from terminal. every 1 Group input data include 3 Data items: car "arrival" or "departure" information, car license plate number and time of arrival or departure, for each 1 The output data after the operation of the input data group is: if the vehicle arrives, it outputs the parking position of the vehicle in the parking lot or on the pavement; If the car leaves; Output the time the car stays in the parking lot and the fee payable (there is no charge for the time the car stays on the sidewalk). Stacks are implemented in a sequential structure and queues in a linked list.  
 [Test data] set n=2, The input data is :(' A' . 1 . 5 ), (' A' . 2 . 10 ), (' D' . 1 . 15 ), (' A' . 3 .  20 ),   (' A' . 4 . 25 ), (' A' . 5 . 30 ), (' D' . 2 . 35 ), (' D' . 4 . 40 ), (' E' . 0 . 0 ). every 1 Group input data include 3 The vehicle's "arrival" or "departure" information, the vehicle licence number and the time of arrival or departure, among which, A' Denotes arrival; ' D' Go away, ' E' Indicates the end of input.  
 [Implementation tips] need to be set separately 1 A stack, where cars exit from a parking lot to make way for departing cars, also USES a sequential storage structure. Input data is ordered by the time of arrival or departure. Each element in the stack represents 1 A car contains two data items: the license number of the car and the moment of entry into the parking lot. </pre><br> 
<br> 
</h1><h1><a name="t1"></a></h1> 
<pre></pre> 
<pre name="code" class="java">import java.util.Scanner; 
 
public class tingchechangxitong { 
 public static void main(String[] args){ 
 Scanner sc = new Scanner(System.in); 
 System.out.println(" Please enter garage size and hourly rate: "); 
 int size = sc.nextInt(); 
 int cost = sc.nextInt(); 
 int p = 0,q = 0,hm,sj,i=0,wz=0,j=0,bwz = 0; 
 char c; 
 System.out.print(size + " " + cost); 
 base [] ck1 = new base[size]; // The size of the garage 1 set  
 base [] ck2 = new base[size]; 
 base [] ck3 = new base[100]; // Let's say there's an infinite number of cars on the sidewalk  
 based(ck1,size); 
 based(ck2,size); 
 based(ck3,100); 
 while(true){ 
 System.out.println(" Input data: \n"); 
 c = sc.next().charAt(0); 
 
 if(c == 'e'||c == 'E'){ 
  System.out.println(" End use "); 
  break; 
 } 
 hm = sc.nextInt(); 
 sj = sc.nextInt(); 
 if(c == 'a'||c == 'A') 
 if(p<size){ 
  ck1[p].hm = hm; 
  ck1[p].sj = sj; 
  System.out.println(hm + " Car number one enters the garage " + (p+1) + "WZ"); 
  p++; 
  System.out.println(" Now the car in the garage is ( From inside to outside ) : "); 
  for( i = 0;i<p;i++){ 
  System.out.print(ck1[i].hm + " "); 
  System.out.println();} 
 }else{ 
  ck3[q].hm = hm; 
  ck3[q].sj = sj; 
  System.out.println(hm + " No. 1 entered the sidewalk "+ (q+1) + "WZ"); 
  q++; 
 } 
 if(c == 'd'||c == 'D'){ 
  for(i=0;i<size;i++){ 
  if(ck1[i].hm == hm){ 
  System.out.println(hm + " Car number one left the garage "+ (i+1) + "WZ" + " Total cost: " + (sj-ck1[i].sj)*cost); 
  p--; 
  wz=i; 
  break; 
  } 
  
  } 
  //changeWZ(ck1,wz,p);// 
  // 
  ZchangeWZ(ck1,ck2,wz,size-1);// The stack to achieve  
  // 
  System.out.println(" Now the car in the garage is ( From inside to outside ) : "); 
  for( i = 0;i<p;i++){ 
  System.out.print(ck1[i].hm + " "); 
  System.out.println(); 
 } 
  // Realize the access road to the car into the garage  
  if(p<size) 
  { 
  if(q>0) 
  { 
  //for(i=0,j=p;j<size && i<q;i++,j++){ 
  // ck1[j].hm = ck3[i].hm; 
  ck1[p].hm = ck3[0].hm; 
  // ck1[j].sj = ck3[i].sj; 
  ck1[p].sj = ck3[0].sj; 
  // bwz = i-1; 
  BDchangeWZ(ck1,ck3,bwz,q);// Queue implementation  
  p++; 
  q--; 
  System.out.println(" The sidewalk car goes into the garage "); 
  System.out.println(" Now the car in the garage is ( From inside to outside ) : "); 
  for( i = 0;i<p;i++){ 
   System.out.print(ck1[i].hm + " "); 
   System.out.println();} 
  }   
  
  /*if(q>0){ 
  ck1[size-1].hm = ck3[i].hm; 
  ck1[j].sj = ck3[i].sj; 
  bwz = i-1; 
  BDchangeWZ(ck1,ck3,bwz,q-1);// Queue implementation  
  p++; 
  q--; 
  }*/   
  } 
  if(p==0) 
  System.out.print("NULL\n"); 
 }  
 } 
 } 
 
 // 
 // data  3 3 a 1 1 a 2 2 a 3 3 a 4 4 a 5 5 
 // 
 // 
 
 /** 
 *  The corresponding function  
 * @param ck 
 * @param size 
 */ 
 // Based on the base An array of  
 public static void based(base[]ck,int size) 
 { 
 
 for(int i=0;i<size;i++) 
 { 
 ck[i] = new base(); 
 } 
 } 
 
 /* public static void changeWZ(base[]ck,int w,int p){// Directly cover the vehicle out of the garage  
 for(int i = w;i<p;i++){ 
 ck[i].hm = ck[i+1].hm; 
 } 
 }*/ 
 public static void ZchangeWZ(base[]ck1,base[]ck2,int w,int s){// Stack's thought implementation  
 int i,j; 
 for( i = s,j = 0;i>w;i--,j++){ 
 ck2[j].hm = ck1[i].hm; 
 ck2[j].sj = ck1[i].sj; 
 
 }// The car in front gives way  
 int k = j-1; 
 for(i = w, j = k;j>=0;i++,j--){ 
 ck1[i].hm = ck2[j].hm; 
 ck1[i].sj = ck2[j].sj; 
 } 
 } 
 public static void BDchangeWZ(base[]ck1,base[]ck3,int w,int s){// The idea of a queue  
 int j; 
 for(j = 0;j<s-1;j++){ 
 ck3[j].hm = ck3[j+1].hm; 
 ck3[j].sj = ck3[j+1].sj; 
 // w--; 
 }// The car in front goes into the garage  
 } 
} 
// The base class  
 class base { 
 int hm; 
 int sj; 
} 
</pre><br> 

Above is a complete JAVA parking system example code, I hope you can use to


Related articles: