Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SIMPLE CODING HELP NEEDED FOR JAVA PROGRAM here is the program description: Writ

ID: 3802360 • Letter: S

Question

SIMPLE CODING HELP NEEDED FOR JAVA PROGRAM

here is the program description:

Write a program that supports the following operations:

int add(string login, string time, int priority, int size, int handle): add a new request to a queue; return a sequential ID# if successful, or FULL if the total file size of all pending requests exceeds the maximum spool size of 50MB. The input argument handle is not used in this exercise, so set it to NULL. The ID cycles in the range of 1 - 1024.

int print_next(): return EMPTY if the queue is empty, or return the ID of the pending highest-priority request and delete it from the queue.

int find_next(): return EMPTY if the queue is empty, or return the ID of the pending highest-priority request.

int cancel(string login): delete all the requests made by the login user; return the number of deleted requests if successful, or return NONE if not found.

int cancel(int ID): delete the request with the ID; return 0 if successful, or return NONE if not found.

String status(): return a string containing information about all pending requests' ID, login, creation time, priority, file size, and file handle; return “EMPTY” if there's no pending request. The order of the print requests returned does not matter.

Error codes: -1 for FULL, -2 for EMPTY, -3 for NONE. (If you add more error codes, clearly state them in your program code.)  

Your program should read the commands one by one from a file

AN EXAMPLE OF PROGRAM INPUT

EXAMPLE OF PROGRAM OUTPUT

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.march;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Sam
*/
public class PriorityQueue {
    class Store {
        String login;
        String time;
        int priority;
        int size;
        int handle;
        int id;

        public Store(String login, String time, int priority, int size, int handle,int id) {
            this.id = id;
            this.login = login;
            this.time = time;
            this.priority = priority;
            this.size = size;
            this.handle = handle;
        }

        @Override
        public String toString() {
            return "Store{" + "login=" + login + ", time=" + time + ", priority=" + priority + ", size=" + size + ", handle=" + handle + ", id=" + id + '}';
        }
      
      
      
    }
  
    ArrayList<Store> arrayList;
    int totalSize;
    int id;
    public PriorityQueue() {
        totalSize = 0;
        id = 0;
        arrayList = new ArrayList<>();
    }
  
    int add(String login, String time, int priority, int size, int handle) {
        if (totalSize + size > 50) //Change here to match unit (Bytes or MB)
            return -1;
        totalSize += size;
        id++;
        if (id == 1025)
            id = 1;
        arrayList.add(new Store(login, time, priority, size, handle, id));
        return id;
    }
  
    int print_next(){
        if (arrayList.size()==0)
            return -2;
        Store remove = arrayList.get(0);
        for (int i = 1; i<arrayList.size(); i++)
            if (remove.priority < arrayList.get(i).priority)
                remove = arrayList.get(i);
        totalSize -= remove.size;
        arrayList.remove(remove);
        return remove.id;
    }
  
    int find_next() {
        if (arrayList.size()==0)
            return -2;
        Store find = arrayList.get(0);
        for (int i = 1; i<arrayList.size(); i++)
            if (find.priority < arrayList.get(i).priority)
                find = arrayList.get(i);
        return find.id;
    }
  
    int cancel(String login){
        int count = 0;
        for (int i = 0; i<arrayList.size(); i++)
            if (arrayList.get(i).login.equals(login)){
                arrayList.remove(i);
                count++;
            }
        if (count == 0)
            return -3;
        return count;
    }
  
    int cancel(int ID){
        for (int i = 0; i<arrayList.size(); i++)
            if (arrayList.get(i).id == id){
                arrayList.remove(i);
                return 0;
            }
        return -3;
    }
  
    String status(){
        if (arrayList.isEmpty())
            return "EMPTY";
        String string=arrayList.get(0).toString();
        for (int i = 1; i<arrayList.size(); i++)
            string = string + " " + arrayList.get(i).toString();
        return string;
    }
  
    public static void main(String[] args) {
        PriorityQueue pq = new PriorityQueue();
        try {
            BufferedReader br = new BufferedReader(new FileReader("FILE NAME HERE"));
            String line;
            String token[];
            while ((line=br.readLine())!=null) {
                token = line.split(" ");
                if (token[0].equalsIgnoreCase("add"))
                    System.out.println(pq.add(token[1], token[2], Integer.parseInt(token[3]), Integer.parseInt(token[4]), Integer.parseInt(token[5])));
                else if (token[0].equalsIgnoreCase("print_next"))
                    System.out.println(pq.print_next());
                else if (token[0].equalsIgnoreCase("find_next"))
                    System.out.println(pq.find_next());
                else if (token[0].equalsIgnoreCase("canel"))
                    try {
                        System.out.println(pq.cancel(Integer.parseInt(token[1])));
                    } catch (Exception ex){
                        System.out.println(pq.cancel(token[1]));
                    }
                else if (token[0].equalsIgnoreCase("status"))
                    System.out.println(pq.status());
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PriorityQueue.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PriorityQueue.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


NOTE: Since the input size unit is not mentioned, I have considered it to be in MB and not Bytes