JAVA IO練習


 停車場有進場和出場的功能
1. 進場時:采用鍵盤錄入的方式,錄入汽車的品牌、顏色、車牌號。
把品牌、顏色、車牌號,以及進場時間寫入car.txt文件中。

2. 出場時:鍵盤錄入車牌號,去文件中查找該車的進場時間,並計算停車時間。

3. 30分鍾內免費,過后每小時3元,超過一小時安一小時計算。 
 
創建一個停車場類,用來記錄汽車的 品牌, 顏色, 車牌號,以及 進場時間.
 1  2 /*把品牌、顏色、車牌號,以及進場時間*/
 3 public class Tinchechang {
 4     private  String brand;
 5     private  String color;
 6     private  String num;
 7     private  String time;
 8 
 9     public Tinchechang() {
10     }
11 
12     public Tinchechang(String brand, String color, String num, String time) {
13         this.brand = brand;
14         this.color = color;
15         this.num = num;
16         this.time = time;
17     }
18 
19     @Override
20     public String toString() {
21         return "Tinchechang{" +
22                 "brand='" + brand + '\'' +
23                 ", color='" + color + '\'' +
24                 ", num='" + num + '\'' +
25                 ", time='" + time + '\'' +
26                 '}';
27     }
28 
29     public String getBrand() {
30         return brand;
31     }
32 
33     public void setBrand(String brand) {
34         this.brand = brand;
35     }
36 
37     public String getColor() {
38         return color;
39     }
40 
41     public void setColor(String color) {
42         this.color = color;
43     }
44 
45     public String getNum() {
46         return num;
47     }
48 
49     public void setNum(String num) {
50         this.num = num;
51     }
52 
53     public String getTime() {
54         return time;
55     }
56 
57     public void setTime(String time) {
58         this.time = time;
59     }
60 }

 

  1 import java.io.*;
  2 import java.util.ArrayList;
  3 import java.util.Scanner;
  4 
  5 /*
  6 * 停車場有進場和出場的功能
  7 1. 進場時:采用鍵盤錄入的方式,錄入汽車的品牌、顏色、車牌號。
  8     把品牌、顏色、車牌號,以及進場時間寫入car.txt文件中。
  9 
 10 2. 出場時:鍵盤錄入車牌號,去文件中查找該車的進場時間,並計算停車時間。
 11 
 12 3. 30分鍾內免費,過后每小時3元,超過一小時安一小時計算。
 13 * */
 14 public class Mainclass {
 15     public static void main(String[] args) throws IOException {
 16         ArrayList<Tinchechang> arrayList = new ArrayList<>();
 17 
 18         Scanner sc = new Scanner(System.in);
 19        for(;;){
 20            System.out.println("請輸進場還是出場,輸入y or n\n輸入0退出");
 21            String inpuStr =sc.nextLine();
 22            if (inpuStr.equals("y")){
 23                //進場
 24                ruChang();
 25            }
 26            else if (inpuStr.equals("n")){
 27                //出場
 28                chuChang();
 29            }
 30            else if(inpuStr.equals("0")){
 31                System.exit(0);
 32            }
 33            else
 34                System.out.println("輸入不正確!,請重新輸入");
 35 
 36        }
 37 
 38     }
 39 
 40     private static void chuChang() throws IOException {
 41         /*出場時:鍵盤錄入車牌號,去文件中查找該車的進場時間,並計算停車時間。*/
 42         ArrayList<Tinchechang> arrayList = new ArrayList<>();
 43         File file = new File("C:\\ideaProjects\\job-code\\day09\\src\\老王的題\\car.txt");
 44         FileReader fr = new FileReader(file);
 45         StringBuilder sb = new StringBuilder();
 46         int len;
 47         char arr[] = new char[1024];
 48         while((len=fr.read(arr))!=-1){
 49             sb.append(arr,0,len);
 50         }
 51         System.out.println(sb);
 52      //   System.out
 53         // .println("測試代碼 等待刪除");
 54         //切割每一行
 55         String[] sp1 = sb.toString().split("\r\n");
 56         for (String line : sp1) {
 57             String[] array = line.split(",");
 58             Tinchechang tc = new Tinchechang(array[0],array[1],array[2],array[3]);
 59             arrayList.add(tc);
 60         }
 61         //請求輸入比對
 62         System.out.println("請輸入你的車牌號");
 63         Scanner sc = new Scanner(System.in);
 64         String inputSring = sc.nextLine();
 65         String Time="";
 66         String nowTime="";
 67         /*出場時:鍵盤錄入車牌號,去文件中查找該車的進場時間,並計算停車時間。*/
 68         for (Tinchechang tcs : arrayList) {
 69             if(tcs.getNum().equals(inputSring)){
 70                 nowTime=System.currentTimeMillis()+"";
 71                 Time = tcs.getTime();
 72             }
 73         }
 74         long realTime = (Long.parseLong(nowTime)-Long.parseLong(Time))/1000/60;
 75         /*30分鍾內免費,過后每小時3元,超過一小時安一小時計算。*/
 76        // System.out.println("realTime = "+realTime);
 77         getPrice(realTime);
 78         fr.close();
 79     }
 80     /*
 81     *  計算價格
 82     * */
 83     private static void getPrice(long realTime) {
 84         if(realTime<=30){
 85             System.out.println("30分鍾以內免費!,您停車了"+realTime+"分鍾");
 86         }
 87         else if(realTime>30 && realTime <60)
 88         {
 89             System.out.println("30-60收費3元,您停車了"+realTime+"分鍾");
 90         }
 91         else {
 92             long count = realTime/60+1;
 93             System.out.println("超過一小時每小時3元收費:"+count*3+"元,您停車了"+realTime+"分鍾");
 94         }
 95     }
 96 
 97 
 98     private static void ruChang() throws IOException {
 99        /* 1. 進場時:采用鍵盤錄入的方式,錄入汽車的品牌、顏色、車牌號。
100         把品牌、顏色、車牌號,以及進場時間寫入car.txt文件中。*/
101        Scanner sc = new Scanner(System.in);
102 
103         Tinchechang tc = new Tinchechang();
104         System.out.println("請輸入汽車的品牌");
105         tc.setBrand(sc.nextLine());
106         System.out.println("請輸入汽車的顏色");
107         tc.setColor(sc.nextLine());
108         System.out.println("請輸入汽車的車牌號");
109         tc.setNum(sc.nextLine());
110         //獲取系統時間
111         long nowTime = System.currentTimeMillis();
112         tc.setTime(nowTime+"");
113         //寫入文件
114         File file = new File("C:\\ideaProjects\\job-code\\day09\\src\\老王的題\\car.txt");
115         //寫入
116         FileWriter wf = new FileWriter(file,true);
117         StringBuilder sb = new StringBuilder();
118         sb.append(tc.getBrand()).append(",").append(tc.getColor()).append(",").append(tc.getNum()).append(",").append(tc.getTime()).append("\r\n");
119         wf.write(sb.toString());
120         wf.close();
121         System.out.println("寫入成功!");
122     }
123 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM