2014-07-14 91 views
-2

**新编辑在底部的信息**技术支持票程序

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

票务类:

/** 
* 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类:

/** 
* 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空值。我只是不知道该如何改变,才能使它适用于整个流程票和预测方法。我一直盯着这个代码几个小时,只是在脑海里放肆的问题是什么。有人能指出我朝着正确的方向吗?

感谢您在这篇长文章中的时间。

**编辑*** 我修复了裸视错误。当我运行它似乎一切正常。它向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); 
} 

只是无法弄清楚什么。

+0

应该设置什么值?如果你不知道代码是什么或者它应该设置什么,我现在要删除它,因为试图修复代码,即使你不知道它是什么是徒劳的。 –

+0

可能重复[什么是空指针异常,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -我修复它) – Filburt

回答

0

很有可能您试图在没有解算器的情况下打印票证。即求解器null

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

顺便说一句,如果你使用调试器,你应该可以看到的问题是在几分钟之内的东西。我建议你学习如何使用它,因为它可以节省你数小时的挫折。


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

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