技术支持票务计划

时间:2014-07-14 20:56:35

标签: java nullpointerexception null ticket-system

**底部的新编辑信息**

我创建了一个模拟电子技术支持系统的技术支持计划。它应该通过票证具有客户端请求支持并将其分配给适当的代理。它有四个类(客户端,代理,票证和实际的技术支持系统)。

门票类:

/**
 * Ticket.java
 *
 * This class is part of Programming
 * Assignment 6 - Tech Support System
 * for CS1440 Summer 2014.
 *
 * @author Brandon C. Eason
 * @author
 * @version
 */

/**
 * This class represents a support ticket
 * in the tech support ticket.
 */
public class Ticket
{
//the number of minutes for resolving a premium ticket
private static final int PREMIUM = 30;
//the number of minutes for resolving a basic ticket
private static final int BASIC = 60;
//the number of tickets generated
private static int ticketNumber;
//the ticket id, built from the current date and ticket number
private String id;
//the problem description
private String description;
//the number of minutes since the ticket was entered
private int minutes;
//the client that requested this ticket
private Client requester;
//the agent assigned to this ticket
private Agent solver;

/**
 * Fully parameterized constructor for Ticket.
 *
 * @param date - the date this ticket was created
 * @param description - this ticket's problem description
 * @param minutes - the number of minutes since this ticket was entered
 * @param requester - the client that requested this ticket
 */
public Ticket(String date, String description, int minutes,
              Client requester)
{
    ticketNumber++;
    id = date + "-" + ticketNumber;
    this.description = description;
    this.minutes = minutes;
    this.requester = new Client(requester);
}

/**
 * Accessor for ticket number.
 *
 * @return the number of tickets generated
 */
public static int getNumTickets()
{
    return ticketNumber;
}

/**
 * Accessor for id.
 *
 * @return this ticket's id
 */
public String getID()
{
    return id;
}

/**
 * Accessor for description.
 *
 * @return this ticket's description
 */
public String getDescription()
{
    return description;
}

/**
 * Accessor for minutes.
 *
 * @return the number of minutes since this ticket was entered
 */
public int getMinutes()
{
    return minutes;
}

/**
 * Accessor for the Client requesting the ticket.
 *
 * @return a copy of the Client requesting this ticket
 */
public Client getClient()
{
    return requester;
}

/**
 * Assign this ticket to an agent.  The method makes a copy of the
 * Agent parameter and sets the solver field to that copy.
 *
 * @param solver - the agent this ticket is assigned to
 */
public void setAgent(Agent solver)
{
    String name = solver.getName();
    String id = solver.getID();
    String specialty = solver.getSpecialty();
    int time = solver.getTime();
    solver = new Agent(name, id, specialty, time);

}

/**
 * Determine the minutes this ticket is overdue.
 * Basic service level clients should have their
 * tickets resolved within 60 minutes.  Premium
 * service level clients should have their tickets
 * resolved within 30 minutes.
 *
 * @return the number of minutes the ticket is overdue
 */
public int timeOverdue()
{
    int timeOverdue;
    if(requester.hasPremium())
    {
        timeOverdue = (PREMIUM - this.minutes);
    }
    else
    {
        timeOverdue = (BASIC - this.minutes);
    }

    return timeOverdue;

}

/**
 * Return a string predicting the resolution time of the ticket.
 *
 * @return a prediction of the resolution time
 */
public String prediction()
{
    String predict = "Resolution Time: ";
    int minutes = timeOverdue() + solver.getTime();
    if (minutes > 0)
    {
        predict += minutes + " minutes behind schedule";
    }
    else if (minutes < 0)
    {
        predict += Math.abs(minutes) + " minutes ahead of schedule";
    }
    else
    {
        predict += "On time";
    }
    return predict;
}

/**
 * Determine whether this ticket is overdue.
 *
 * @return true if this ticket is overdue
 */
public boolean isOverdue()
{
    return timeOverdue() > 0;
}

/**
 * Build a string representation of this ticket.
 *
 * @return this ticket's data
 */
public String toString()
{
    //begin with id and description
    String data = "ID#: " + getID();
    data += "\r\nProblem: " + getDescription();

    //add time overdue, if any
    if (isOverdue())
    {
        data += "\r\nOverdue: " + timeOverdue() + " minutes";
    }

    //add client data
    data += "\r\n------------";
    data += "\r\nRequested by\r\n";
    data += "------------\r\n";
    data += requester;


    //add agent data
    data += "\r\n-----------";
    data += "\r\nAssigned to\r\n";
    data += "-----------\r\n";
    data += solver;

    //add projected resolution time
    data += "\r\n" + prediction();

    return data;
}

/**
 * Determine if this ticket is a duplicate of another.
 * Tickets are duplicates if they have the same description
 * and Client.
 *
 * @param other - the other Ticket
 * @return true if this ticket is a duplicate of the other
 */
public boolean equals(Ticket other)
{
    if(description.equals(other.description) && requester.equals(other.requester))
    {
        return true;
    }
    else
    {
        return false;
    }
}

}

代理类:

/**
 * Agent.java
 *
 * This class is part of Programming
 * Assignment 6 - Tech Support System
 * for CS1440 Summer 2014.
 *
 * @author Brandon C. Eason
 * @author
 * @version
 */

/**
 * This clas represents an agent profile
 * in the tech support system.
 */
public class Agent
{
//the agent's name
private String name;
//the agent's id number
private String id;
//the agent's support specialty
private String specialty;
//the agent's average turnaround time in whole minutes
private int time;

/**
 * Fully parameterized constructor for Agent.
 *
 * @param name - this agent's name
 * @param id - this agent's id number
 * @param specialty - this agent's support specialty
 * @param time - this agent's average turnaround time in minutes
 */
public Agent(String name, String id, String specialty, int time)
{
    this.name = name;
    this.id = id;
    this.specialty = specialty;
    this.time = time;
}

/**
 * Create a copy of this Agent.
 *
 * @return a new Agent that is a copy of this one
 */
public Agent copy()
{
    Agent copyAgent = new Agent(name, id, specialty, time);
    return copyAgent;
}

/**
 * Accessor for name.
 *
 * @return this agent's name
 */
public String getName()
{
    return name;
}

/**
 * Accessor for id number.
 *
 * @return this agent's id number
 */
public String getID()
{
    return id;
}

/**
 * Accessor for specialty.
 *
 * @return this agent's support specialty
 */
public String getSpecialty()
{
    return specialty;
}

/**
 * Accessor for average turnaround time.
 *
 * @return this agent's average turnaround time
 */
public int getTime()
{
    return time;
}

/**
 * Builds a string representation of this agent's data.
 *
 * @return this agent's data
 */
public String toString()
{
    String str = "Agents's name: " + this.name
                + "/nAgent's ID: " + this.id
                + "/nSupport specialty: " + this.specialty
                + "/nAverage service time:" + this.time;
    return str;
}

}

客户端类:

/**
 * Client.java
 *
 * This class is part of Programming
 * Assignment 6 - Tech Support System
 * for CS1440 Summer 2014.
 *
 * @author Brandon C. Eason
 * @author
 * @version
 */

/**
 * This class represents a client profile in
 * the tech support system with information
 * about the client and the client's
 * computer system.
 */
public class Client
{
//the client's full name
private String name;
//the client's phone number
private String phone;
//the client's computer type
private String computer;
//true if a premium client
private boolean premium;

/**
 * Fully parameterized constructor for
 * Client.
 *
 * @param name - this client's name
 * @param phone - this client's phone number
 * @param computer - this client's computer type
 * @param premium - whether client has premium service
 */
public Client(String name, String phone, String computer, boolean premium)
{
    this.name = name;
    this.phone = phone;
    this.computer = computer;
    this.premium = premium;
}

/**
 * Constructor for when computer type is not specified.
 *
 * @param name - this client's name
 * @param phone - this client's phone
 * @param premium - whether client has premium service
 */
public Client(String name, String phone, boolean premium)
{
    this(name, phone, "Unspecified", premium);
}

/**
 * Constructor that creates a copy of the Client passed in.
 *
 * @param object - the client to be copied
 */
public Client(Client object)
{
    name = object.name;
    computer = object.computer;
    phone = object.phone;
    premium = object.premium;
}

/**
 * Accessor for name.
 *
 * @return this client's name
 */
public String getName()
{
    return name;
}

/**
 * Accessor for phone.
 *
 * @return this client's phone number
 */
public String getPhone()
{
    return phone;
}

/**
 * Accessor for computer type.
 *
 * @return this client's computer type
 */
public String getComputer()
{
    return computer;
}

/**
 * Determine if this client receives
 * premium service.
 *
 * @return true if this client receives premium service.
 */
public boolean hasPremium()
{
    return premium;
}

/**
 * Builds a printable String representation of
 * this client's data.
 *
 * @return this client's data
 */
public String toString()
{
    String service;
    if(this.premium)
    {
        service = "Premium";
    }
        else
    {
        service = "Basic";
    }
    String str = "Client's name: " + this.name
                + "/nClient's phone: " + this.phone
                + "/nCSystem type: " + this.computer
                + "/nService:" + service;
   return str;
}

/**
 * Determine if this Client is the same as another.
 * Client's are equal if they have the same name,
 * phone number, computer type, and service level.
 *
 * @param other - the other Client
 * @return true if this client equals the other
 */
public boolean equals(Client other)
{
    if(name.equals(other.name) && phone.equals(other.phone)
        && computer.equals(other.computer) && premium == (other.premium))
    {
        return true;
    }
    else
    {
        return false;
    }


}

}

此类通过读取文件中的请求,将其分配给代理,并将其写入与日期一起生成的报告来处理故障单。每种专业都有值班代理(Mac,Windows PC或任何系统)。

TicketSupportSystem类:

/**
 * TechSupportSystem.java
 *
 * This class is part of Programming
 * Assignment 6 - Tech Support System
 * for CS1440 Summer 2013.
 *
 * @author Brandon C. Eason
 * @author
 * @version
 */

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;

/**
 * This class runs a tech support system
 * which reads in tickets entered by clients,
 * assigns them to an agent to be resolved,
 * and writes them to a report.
 */
public class TechSupportSystem
{
//for keyboard input
private Scanner keyboard;
//for file input
private Scanner input;
//for writing to a file
private PrintWriter output;
//for appending to a file
private FileWriter append;
//for opening files
private File inputFile;
//today's date
private String date;
//first agent
private Agent agentOne;
//second agent
private Agent agentTwo;
//third agent
private Agent agentThree;
//the number of tickets assigned
private int numTickets;

/**
 * Constructor for TechSupportSystem.
 *
 * @throws IOException - file not found
 */
public TechSupportSystem() throws IOException
{
    System.out.println("------------------------------\n");
    System.out.println("Welcome to Ticket Manager Lite\n");
    System.out.println("------------------------------\n");
    keyboard = new Scanner(System.in);
    getDate();
    getAgents();
    enableWriting();
    processTickets();
    output.close();
    System.out.println("\nTotal tickets processed: "
                       + Ticket.getNumTickets());
    System.out.println("\nTotal tickets assigned: " + numTickets);
}

/**
 * Get today's date from the dispatcher (the user of this system).
 */
private void getDate()
{
    date = "";

    while (date.length() != 8)
    {
        System.out.print("Enter today's date(MMDDYYYY format): ");
        date = keyboard.nextLine();
    }
}

/**
 * Verify file for opening and open it.
 *
 * @param fileName - the file to be opened
 * @return true if valid file
 */
private boolean openFile(String fileName)
{
    inputFile = new File(fileName);
    return inputFile.exists();
}

/**
 * Prepare file writing.
 *
 * @throws IOException - file not found
 */
private void enableWriting() throws IOException
{
    append = new FileWriter("report.txt", true);
    output = new PrintWriter(append);

    output.println("--------------------------");
    output.println("Ticket Report for " + date);
    output.println("--------------------------\r\n");
}

/**
 * Read in agents on duty.
 *
 * @throws IOException - file not found
 */
private void getAgents() throws IOException
{
    String fileName;
    do
    {
        System.out.print("Enter the name of the agent duty file: ");
        fileName = keyboard.nextLine();
    } while (!openFile(fileName));

    input = new Scanner(inputFile);

    agentOne = readAgent();
    agentTwo = readAgent();
    agentThree = readAgent();

    input.close();
}

/**
 * Read a single agent.
 *
 * @return the agent that was read
 */
private Agent readAgent()
{
    String name = input.nextLine();
    String id = input.nextLine();
    String specialty = input.nextLine();
    int time = input.nextInt();
    input.nextLine();
    return new Agent(name, id, specialty, time);
}

/**
 * Read in the day's tickets from a file, two
 * at a time, check for duplicates, assign the
 * tickets to an agent, and write them to a report.
 *
 * @throws IOException - file not found
 */
private void processTickets() throws IOException
{
    String fileName;
    Ticket currentTicket;
    Ticket lastTicket = null;

    do
    {
        System.out.print("Enter the name of the ticket file: ");
        fileName = keyboard.nextLine();
    } while (!openFile(fileName));

    System.out.println();

    input = new Scanner(inputFile);

    while (input.hasNext())
    {
        currentTicket = readTicket();

        if (lastTicket == null || !currentTicket.equals(lastTicket))
        {
                assign(currentTicket);
                output.println(currentTicket + "\r\n");
        }
        lastTicket = currentTicket;
    }

    input.close();
}

/**
 * Read in a single ticket.
 *
 * @return the ticket that was read in
 */
private Ticket readTicket()
{
    Client requester;
    String description;
    int minutes;

    requester = readClient();
    description = input.nextLine();
    minutes = input.nextInt();
    input.nextLine();

    return new Ticket(date, description, minutes, requester);
}

/**
 * Read in a single client.
 *
 * @return the client that was read in
 */
private Client readClient()
{
    String name;
    String phone;
    String computer;
    String premium;
    boolean hasPremium = false;

    name = input.nextLine();
    phone = input.nextLine();
    computer = input.nextLine();
    premium = input.nextLine();

    if (premium.equals("Premium"))
    {
        hasPremium = true;
    }

    if (computer.length() == 0)
    {
        return new Client(name, phone, hasPremium);
    }
    else
    {
        return new Client(name, phone, computer, hasPremium);
    }
}

/**
 * Assign a ticket to an agent.
 *
 * @param ticket - the ticket to be assigned
 */
private void assign(Ticket ticket)
{
    Client requester = ticket.getClient();
    String computer = requester.getComputer();
    Agent solver;

    if (agentOne.getSpecialty().equals(computer))
    {
        solver = agentOne;
    }
    else if (agentTwo.getSpecialty().equals(computer))
    {
        solver = agentTwo;
    }
    else
    {
        solver = agentThree;
    }

    ticket.setAgent(solver);
    System.out.println("Ticket assigned to " + solver.getName() + ".");
    numTickets++;
}

/**
 * Starts the tech support system.
 *
 * @param args - not used
 * @throws IOException - file not found
 */
public static void main(String[] args) throws IOException
{
    new TechSupportSystem();
}
}

一切都编译正确,但是当我运行它时,在我告诉它文件并且它读取它们之后它有一个错误。它给出的错误是:

------------------------------

Welcome to Ticket Manager Lite

------------------------------

Enter today's date(MMDDYYYY format): 12121221
Enter the name of the agent duty file: agents.txt
Enter the name of the ticket file: tickets.txt

Ticket assigned to Dee.
Exception in thread "main" java.lang.NullPointerException
    at Ticket.prediction(Ticket.java:153)
    at Ticket.toString(Ticket.java:210)
    at java.lang.String.valueOf(String.java:2979)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at TechSupportSystem.processTickets(TechSupportSystem.java:178)
    at TechSupportSystem.<init>(TechSupportSystem.java:62)
    at TechSupportSystem.main(TechSupportSystem.java:275)
Press any key to continue . . . 

所以我知道它与TechSupportSystem类中分配给lastTicket的null值有关,而且在预测中有些偏差。我只是不知道要改变什么,这将使它适用于整个processTickets和预测方法。我一直在盯着这段代码几个小时,只是在实际问题上保持大脑放屁。至少有人能指出我正确的方向吗?

感谢你在这篇长篇文章上的时间。

** ***编辑 我修复了nullexception错误。当我运行它时,一切正常。它向report.txt报告了正确的信息,但是搞乱的是它没有分配解算器。每个票证的解算器都为空。 我认为我的

有问题
 /**
 * Assign this ticket to an agent.  The method makes a copy of the
 * Agent parameter and sets the solver field to that copy.
 *
 * @param solver - the agent this ticket is assigned to
 */
public void setAgent(Agent solver)
{
    String name = solver.getName();
    String id = solver.getID();
    String specialty = solver.getSpecialty();
    int time = solver.getTime();
    solver = new Agent(name, id, specialty, time);
}

无法弄清楚是什么。

1 个答案:

答案 0 :(得分:0)

您很可能在没有解算器的情况下尝试打印故障单。即解算器为null

public String prediction() {
    if(solver == null) return "no solver";

BTW如果您使用调试器,您应该能够在几分钟内看到问题所在。我建议你学习如何使用它,因为它可以节省你几个小时的挫折感。


最简单的解决方法是捕获异常,直到您可以修复它。

String predict;
try {
    predict = prediction();
} catch (Exception e) {
    predict = e.toString();
}
data += "\r\n" + predict;