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 }

 

posted @ 2018-08-21 14:29  ccsoft  阅读(1521)  评论(1编辑  收藏  举报