java中的文件输出无效

时间:2017-04-16 14:10:28

标签: java

所以我的作业是创建一个停车场模拟器,它接收来自input.txt文件的查询,这些文件作为包含String查询和int parkingID的JSON对象数组传递。对于每个查询,我都需要执行操作。当我尝试将信息输出到文件时没有任何反应。你能看看我在这里做错了吗?

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

这是主要功能

public class Company{

    public static void main(String[] args) throws Exception{ 


        String content = new 
String(Files.readAllBytes(Paths.get("input.txt")));
        Gson gson= new GsonBuilder().create();
        Type listType = new TypeToken<List<Query>>() {}.getType();
        List<Query> queries = gson.fromJson(content,listType);
        PrintWriter out = new PrintWriter("output.txt");
        ParkingLot[] array = new ParkingLot[10]; 
        array[0] = new ParkingLot(0);

        int counter = 1;

       for(int i =0; i < queries.size(); i++)
        {
           try{
               String query = queries.get(i).getQuery();
             int queryParkingID = queries.get(i).getParkingID();

                   if(query.equals("car entered"))
                   {
                       array[queryParkingID].carEntered();
                   }
                   else if(query.equals("car left"))
                   {
                       array[queryParkingID].carLeft();
                   }
                   else if(query.equals("Parking lot opened"))
                   {
                       array[counter] = new ParkingLot(counter);
                       counter++;
                   }

               }

            catch(ArrayIndexOutOfBoundsException e){
                System.out.println(e);
                 String query = queries.get(i).getQuery();
                 int queryParkingID = queries.get(i).getParkingID();
                if (queryParkingID == -1)
                   {
                       if(query.equals("bandwidth"))
                       {
                           out.println(array[queryParkingID].getBandwidth());
                       }
                       else if(query.equals("bandwidth all"))
                       {
                           out.println(ParkingLot.bandwidthAll);
                       }
                       else if(query.equals("parked"))
                       {
                           out.println(array[queryParkingID].getCarsParked());
                       }
                       else if(query.equals("parked all"))
                       {
                           out.println(ParkingLot.carsParkedAll);
                       }
                       else if(query.equals("revenue"))
                       {
                           out.println(array[queryParkingID].getRevenue());
                       }
                       else if(query.equals("revenue all"))
                       {
                           out.println(ParkingLot.revenueAll);
                       }
                   }
                }
            catch(NullPointerException e){
                  System.out.println(e);
                    }

            catch(Exception e){
              System.out.println(e);
                }


        }
     out.close();
    }     



   }

这是用于存储查询的查询函数

class Query {

        private int parkingID ;
        private String query;

       public String getQuery()
        {
            return query;
        }

        public int getParkingID()
        {
            return parkingID;

}}

这是停车场类

public class ParkingLot {

private int parkingID;
private static int parkingFee = 5;
private static int maxCarCapacity = 30;
private int revenue;
private int carsParked;
static int carsParkedAll;
static int revenueAll;
private int bandwidth;
static int bandwidthAll;

public ParkingLot(int parkingID) 
{
    this.parkingID = parkingID;
    revenue = 0;
    carsParked = 0;
    carsParkedAll = 0;
}
public void carEntered()
{
    if(carsParked < maxCarCapacity )
    {

    revenue += parkingFee;
    revenueAll += parkingFee;
    carsParked++;
    carsParkedAll++; 
    bandwidth++;
    bandwidthAll++;
    }   
}
public void carLeft()
{
    if(carsParked > 0)
    {   
    carsParked--; 
    carsParkedAll--; 
    }
}   
public int getCarsParkedAll() {
        return carsParkedAll;
    }

public int getRevenueAll() {
        return revenueAll;
    }

public int getBandwidth() {
        return bandwidth;
    }
public void setBandwidth(int bandwidth) {
        this.bandwidth = bandwidth;
    }
public int getBandwidthAll() {
        return bandwidthAll;
    }

public static int getParkingFee() {
    return parkingFee;
}
public static void setParkingFee(int parkingFee) {
    ParkingLot.parkingFee = parkingFee;
}


public int getParkingID() {
    return parkingID;
}
public void setParkingID(int parkingID) {
    this.parkingID = parkingID;
}


public int getMaxCarCapacity() {
    return maxCarCapacity;
}
public void setMaxCarCapacity(int maxCarCapacity) {
    ParkingLot.maxCarCapacity = maxCarCapacity;
}


public int getRevenue() {
    return revenue;
}
public void setRevenue(int revenue) {
    this.revenue = revenue;
}

public int getCarsParked() {
    return carsParked;
}
public void setCarsParked(int carsParked) {
    this.carsParked = carsParked;
}




}

以下是查询的示例

 {"parkingID":-3,"query":"bandwidth"}

我们还必须处理异常,因此我必须将out部分移动到catch块中,以便在它达到越界异常时打印出来。

非常感谢你的帮助。

0 个答案:

没有答案