customers.txt: 1001 Burt\'s Hardware 100 Main St. Bangor 1002 Hideaway Rentals 2
ID: 3807581 • Letter: C
Question
customers.txt:
1001
Burt's Hardware
100 Main St.
Bangor
1002
Hideaway Rentals
25 Center St.
Bangor
1003
Regal Appliances
200 Highland Ave.
Portland
1004
Nantucket Apartments
33 Wyler Ave.
Portland
1005
Applebee Handware
42 Liverton Lane
Bangor
1006
Assisted Living
11 Gagnon Road
Portland
1007
Potter Lane
300 Headway Blvd.
Portland
1008
Prince Apartments
10 Outer Broadway
Bangor
1009
Quincy Grocery
75b Kelly Rd
Bangor
1010
Eldercare Assc.
456 Leighton Rd.
Portland
1011
Coastal Living
Elderberry Lane
Portland
Customer.java:
package assg7;
public class Customer {
private int custID; // customer identification
private String name; // customer name
private String address; // customer address
private String town; // customer town
public Customer ( ) // POST: empty customer
{ custID = 0;
name = new String ( );
address = new String ( );
town = new String ( );
}
// accessors and mutators
public int getCustID() {
return custID;
}
public void setCustID(int custID) {
this.custID = custID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public String toString ( ) // POST: return string representation
{ return custID + " " + name + " " + town;
}
}
orders.txt:
1 1001 3
2 1003 1
3 1010 4
4 1006 2
5 1005 6
6 1002 2
7 1008 5
8 1011 2
9 1009 4
10 1007 1
Order.java:
package assg7;
public class Order {
private int orderID; // order identification
private int custID; // customer identification
private int nRefrigerators; // number of refrigerators in order
// accessors and mutators
public int getorderID() {
return orderID;
}
public void setorderID(int ordrID) {
this.orderID = ordrID;
}
public int getcustID() {
return custID;
}
public void setcustID(int c) {
this.custID = c;
}
public int getnRefrigerators() {
return nRefrigerators;
}
public void setnRefrigerators(int nRefrigerators) {
this.nRefrigerators = nRefrigerators;
}
public String toString() // POST: return string representation
{ return (orderID + " " + custID + " " + nRefrigerators);
}
}
CircurlarArrayQueue.java:
package assg7;
public class CircularArrayQueue<T> implements QueueADT<T> {
private final static int CAPACITY = 5; // initial capacity of queue
private int front, rear; // index to front and rear
private int count; // number of items in queue
private T[] queue; // reference to array that implements circular queue
public CircularArrayQueue () // POST: empty queue of default capacity
{ this (CAPACITY); }
@SuppressWarnings("unchecked")
public CircularArrayQueue (int cap) // PRE: cap > 0
{ front = rear = count = 0; // POST: empty queue of capacity cap
queue = (T[]) (new Object[cap]);
}
public void enqueue (T element) // POST: add element to rear of queue
{ if (size() == queue.length)
expandCapacity ( );
queue[rear] = element;
rear = (rear + 1) % queue.length;
count++;
}
public T dequeue ( ) throws EmptyCollectionException // PRE: queue is not empty
{ if (isEmpty()) // POST: remove and return front element
throw new EmptyCollectionException ();
T result = queue[front];
queue[front] = null;
front = (front+1) % queue.length;
count--;
return result;
}
public T getFront ( ) throws EmptyCollectionException // PRE: queue is not empty
{ if (isEmpty()) // POST: return front element
throw new EmptyCollectionException ();
T result = queue[front];
return result;
}
@SuppressWarnings("all")
private void expandCapacity () // POST: resize array to double the capacity
{ T[] larger = (T[]) (new Object [queue.length*2]);
for (int k=0; k<count; k++)
{ larger[k] = queue[front];
front = (front+1) % queue.length;
}
front = 0;
rear = count;
queue = larger;
}
public boolean isEmpty () // POST: return true if queue is empty, else false
{ return count == 0; }
public int size () // POST: return number of elements
{ return count; }
}
EmptyCollectionException.java:
@SuppressWarnings ("serial")
public class EmptyCollectionException extends RuntimeException {
public EmptyCollectionException ( )
{
super ("The collection is empty.");
}
}
QueueADT.java:
package assg7;
public interface QueueADT<T>
{ public void enqueue(T element); // POST: add element to rear of queue
public T dequeue( ); // PRE: queue is not empty
// POST: remove and return front element
public T getFront( ); // PRE: queue is not empty
// POST: return front element
public boolean isEmpty( ); // POST: return true if queue is empty
public int size( ); // POST: return number of elements
}
Truck.java:
package assg7;
import java.util.Stack;
public class Truck {
private String license; // license number
private String destination; // destination city
private int capacity; // number of refrigerators truck holds
private int nFrig; // number of refrigerators on truck
private Stack<Order> contents; // stack of contents
public Truck ( ) // POST: empty truck
{ license = new String ();
destination = new String ();
capacity = 0;
nFrig = 0;
contents = new Stack<Order> ();
}
public Truck (String lic, int cap) // POST: empty truck with license and capacity
{ license = lic;
capacity = cap;
nFrig = 0;
destination = new String ();
contents = new Stack<Order> ();
}
public Truck (String lic, String des, int cap) // POST: empty truck with license, capacity, destination
{ license = lic;
destination = des;
capacity = cap;
nFrig = 0;
contents = new Stack<Order> ();
}
// accessors and mutators
public int getnFrig() {
return nFrig; }
public void setnFrig(int nFrig) {
this.nFrig = nFrig; }
public String getLicense() {
return license; }
public void setLicense(String license) {
this.license = license; }
public String getDestination() {
return destination; }
public void setDestination(String destination) {
this.destination = destination; }
public int getCapacity() {
return capacity; }
public void setCapacity(int capacity) {
this.capacity = capacity; }
public Stack<Order> getContents() {
return contents; }
public void setContents(Stack<Order> contents) {
this.contents = contents; }
public void addOrder (Order o) // PRE: order fits on truck
{ contents.push(o); // POST: add order to truck
nFrig = nFrig + o.getnRefrigerators(); // update number of refrigerators on truck
}
public boolean isFull () // POST: return true if truck is at capacity
{ return capacity == nFrig; }
public String toString () // POST: return string representation
{ return license + "*" + destination + "*" + "" + nFrig; }
}
Explanation / Answer
Here are the additional classes need for the question. Code is with comments. If the answer helped, please dont forget to rate the answer. Thank you very much.
RentalTruck.java
package assg7;
public class RentalTruck extends Truck {
protected double rentalCost;
public RentalTruck(String lic,int capacity)
{
super(lic,capacity);
}
public double getCost()
{
return rentalCost;
}
public void setCost(double cost)
{
rentalCost=cost;
}
}
TruckScheduler.java
package assg7;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
public class TruckScheduler {
final static double RENTAL_COST=50.0 ; //the cost for rental truck
final static int TRUCK_CAPACITY=6; //the load capacity of each truck
final static Random random=new Random();//random number generator
ArrayList <Customer> customers ;//a list of customers
ArrayList<Truck> loadingZone;//a list of trucks in loadin zone
CircularArrayQueue<Order> orders;//daily quue of orders
CircularArrayQueue<Truck> garage; //queue of trucks
String ordersFile,customersFile; //the filenmae for customers and orders file
double costofRentalTrucks; //the total cost of all rental trucks
public TruckScheduler(String custFile,String orderFile)
{
customers=new ArrayList<Customer>(); //a list of customers
loadingZone=new ArrayList<Truck>();//a list of trucks in loadin zone
orders=new CircularArrayQueue<Order>();//daily quue of orders
garage=new CircularArrayQueue<Truck>(); //queue of trucks
//initially add the 3 trucks that the company has into the queue
garage.enqueue(new Truck("aaaaa" ,TRUCK_CAPACITY));
garage.enqueue(new Truck("bbbbb" ,TRUCK_CAPACITY));
garage.enqueue(new Truck("ccccc" ,TRUCK_CAPACITY));
ordersFile=orderFile;
customersFile=custFile;
}
private void loadCustomers(String custFile) throws FileNotFoundException
{
Scanner scanner=new Scanner(new File(custFile)); //open the customer file
int id;
String name,address,city;
Customer c;
while(scanner.hasNext()) //while there is more data
{
id=scanner.nextInt(); // get id
scanner.nextLine();//clear out the character
name=scanner.nextLine(); //get name , name can have spaces, so use nextline
address=scanner.nextLine();//get address
city=scanner.nextLine();
c=new Customer(); //create a customer and set all values
c.setCustID(id);
c.setName(name);
c.setAddress(address);
c.setTown(city);
customers.add(c);//add it to list of customers
}
scanner.close();
}
//reads the orders data for a single order and constructs an Order object and returns it
private Order readOrder(Scanner scanner)
{
int oid,custid,qty;
//read orderid,customer id and quanatity from the input file
oid=scanner.nextInt();
custid=scanner.nextInt();
qty=scanner.nextInt();
Order o=new Order(); //create an Order object and set its values
o.setcustID(custid);
o.setorderID(oid);
o.setnRefrigerators(qty);
return o;
}
//reads the order file and process each order
public void processOrders() throws FileNotFoundException
{
Order order;
Truck truck;
Customer customer;
loadCustomers(customersFile); //load all customers from customer file
Scanner scanner=new Scanner(new File(ordersFile));
while(scanner.hasNext())
{
order=readOrder(scanner);
customer=findCustomer(order.getcustID()); //get the customer based on customer id from the order
System.out.println("Processing order "+customer.getCustID()+" "+customer.getName()+" "+customer.getTown()+" "+order.getnRefrigerators()+" refrigerators.");
truck=findTruck(customer.getTown(), order.getnRefrigerators()); //find / get a truck to fulfill this order city and qty
truck.addOrder(order); //load the truck with this order
System.out.println("Truck "+truck.getLicense()+" has "+truck.getnFrig()+ " refrigerators.");
if(truck.isFull()) //check if truck is full, then send it out for delivery
{
System.out.println("Truck "+truck.getLicense()+" is now full.");
loadingZone.remove(truck);///first remove it from loading zone
printReport(truck);
}
System.out.println("********");
}
scanner.close();
//print out any trucks remaining still in loading zone
System.out.println(" Remaining Truck Delivery Report");
while(!loadingZone.isEmpty())
{
truck=loadingZone.remove(0);//always remove the 1st element
printReport(truck);
}
//display the rental cost
System.out.println("Cost of "+(int)costofRentalTrucks/RENTAL_COST+" rental trucks is $"+String.format("%.2f", costofRentalTrucks)); //format to have 2 decimal places
}
//searches the arraylist of customers to find the customer with given id. If found, returns the customer object
//otherwise returns null
private Customer findCustomer(int customerId)
{
Customer c;
for(int i=0;i<customers.size();i++)
{
c=customers.get(i);
if(c.getCustID()==customerId)//match found
return c;
}
return null;//not found so far
}
//searches first in the loading zone if any truck to same city as specified is availabe with enough space to load the quantity specified by
//requiredspace. If such a truck was found, it it returned. Otherwise a new truck from garage is fetched. If garage is empty, then a new
//rental truck is fetched . When every a new truck is fetched which is not in from loading zone, the loading zone is updated with the new truck.
//and also the truck's destination is set the specified city. This truck is returned
private Truck findTruck(String city,int requiredSpace)
{
Truck truck;
int available;
if(loadingZone.isEmpty()) //check if loadig zone is empty
{
System.out.println("No truck available in loading zone.");
}
else
{
for(int i=0;i<loadingZone.size();i++)
{
truck=loadingZone.get(i);
if(truck.getDestination().equalsIgnoreCase(city)) //if the current truck it to same city that is specified
{
//check if it has enough space
available=truck.getCapacity()-truck.getnFrig();
if(available>=requiredSpace)
{
System.out.println("Using truck "+truck.getLicense()+" from loading zone.");
return truck;
}
}
}
}
//either loading zone was empty or non of the truck there could satisfy the order criteria - city+quanttiy
//so fetch another truck.
//first check in garage
if(garage.isEmpty())
{
System.out.println("Garage is empty.");
truck=getRentalTruck(); //get a rental truck since garage is empty
System.out.println("Got rental truck "+truck.getLicense());
//update the total rental cost
costofRentalTrucks+=RENTAL_COST;
}
else
{
truck=garage.dequeue(); //get truck from garage
System.out.println("Getting truck "+truck.getLicense()+" from garage.");
}
truck.setDestination(city); //set the destination for the truck
loadingZone.add(truck);//update the loading zone with the truck
return truck;
}
//function to create a new rental truck with randomly generated licence number
private Truck getRentalTruck()
{
String licence="";
int n;
for(int i=1;i<6;i++) //generate a 5 digit licence number
{
n=random.nextInt(9)+1; //generate random numbers from 1-9
licence+=n;
}
RentalTruck truck=new RentalTruck(licence,TRUCK_CAPACITY);
return truck;
}
//prints the delivery report of a truck
private void printReport(Truck truck)
{
//display licence and destination
System.out.println("Report of deliveries by truck: "+truck.getLicense());
System.out.println("Destination: "+truck.getDestination());
System.out.println("_____________________________________________");
Stack<Order> orders=truck.getContents(); //get the load of this truck
Customer customer;
Order order;
//display each of the orders
while(!orders.isEmpty())
{
order=orders.pop();
customer=findCustomer(order.getcustID());
System.out.println("Customer: "+customer.getCustID()+" "+customer.getName()+" "+order.getnRefrigerators()+" refrigerators");
}
System.out.println("_____________________________________________ ");
}
public static void main(String[] args) {
TruckScheduler scheduler=new TruckScheduler("customers.txt", "orders.txt"); //create a new scheduler
try {
scheduler.processOrders();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
sample output for customers.txt and orders.txt given in question
Processing order 1001 Burt's Hardware Bangor 3 refrigerators.
No truck available in loading zone.
Getting truck aaaaa from garage.
Truck aaaaa has 3 refrigerators.
********
Processing order 1003 Regal Appliances Portland 1 refrigerators.
Getting truck bbbbb from garage.
Truck bbbbb has 1 refrigerators.
********
Processing order 1010 Eldercare Assc. Portland 4 refrigerators.
Using truck bbbbb from loading zone.
Truck bbbbb has 5 refrigerators.
********
Processing order 1006 Assisted Living Portland 2 refrigerators.
Getting truck ccccc from garage.
Truck ccccc has 2 refrigerators.
********
Processing order 1005 Applebee Handware Bangor 6 refrigerators.
Garage is empty.
Got rental truck 32462
Truck 32462 has 6 refrigerators.
Truck 32462 is now full.
Report of deliveries by truck: 32462
Destination: Bangor
_____________________________________________
Customer: 1005 Applebee Handware 6 refrigerators
_____________________________________________
********
Processing order 1002 Hideaway Rentals Bangor 2 refrigerators.
Using truck aaaaa from loading zone.
Truck aaaaa has 5 refrigerators.
********
Processing order 1008 Prince Apartments Bangor 5 refrigerators.
Garage is empty.
Got rental truck 83345
Truck 83345 has 5 refrigerators.
********
Processing order 1011 Coastal Living Portland 2 refrigerators.
Using truck ccccc from loading zone.
Truck ccccc has 4 refrigerators.
********
Processing order 1009 Quincy Grocery Bangor 4 refrigerators.
Garage is empty.
Got rental truck 54198
Truck 54198 has 4 refrigerators.
********
Processing order 1007 Potter Lane Portland 1 refrigerators.
Using truck bbbbb from loading zone.
Truck bbbbb has 6 refrigerators.
Truck bbbbb is now full.
Report of deliveries by truck: bbbbb
Destination: Portland
_____________________________________________
Customer: 1007 Potter Lane 1 refrigerators
Customer: 1010 Eldercare Assc. 4 refrigerators
Customer: 1003 Regal Appliances 1 refrigerators
_____________________________________________
********
Remaining Truck Delivery Report
Report of deliveries by truck: aaaaa
Destination: Bangor
_____________________________________________
Customer: 1002 Hideaway Rentals 2 refrigerators
Customer: 1001 Burt's Hardware 3 refrigerators
_____________________________________________
Report of deliveries by truck: ccccc
Destination: Portland
_____________________________________________
Customer: 1011 Coastal Living 2 refrigerators
Customer: 1006 Assisted Living 2 refrigerators
_____________________________________________
Report of deliveries by truck: 83345
Destination: Bangor
_____________________________________________
Customer: 1008 Prince Apartments 5 refrigerators
_____________________________________________
Report of deliveries by truck: 54198
Destination: Bangor
_____________________________________________
Customer: 1009 Quincy Grocery 4 refrigerators
_____________________________________________
Cost of 3.0 rental trucks is $150.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.