Question: BASE CODE: OrderTransaction.java import java.io.Serializable; public class OrderTransaction implements Serializable { private static int id; private int orderID=0; private String date; private String orderType; private

BASE CODE:

OrderTransaction.java

import java.io.Serializable;

public class OrderTransaction implements Serializable {

private static int id;

private int orderID=0;

private String date;

private String orderType;

private int numCopies;

private double cost=20;

public OrderTransaction(String date, String orderType, int numCopies, double cost) {

id++;

orderID=id;

this.date=date;

this.orderType=orderType;

this.numCopies=numCopies;

this.cost=cost;

}

public String toString() {

return orderID + " " + date + " " + " " + orderType + " " + numCopies + " " + cost + " ";

}

}

BookOrder.java

import java.util.ArrayList;

public class BookOrder {

private String customerName;

private int orderNumber=0;

private static int id;

private double totalCost;

private ArrayList order = new ArrayList();

private int numCopies;

//private String date;

//private String orderType;

public BookOrder(String customerName, int numCopies, double totalCost) {

id++;

orderNumber=id;

this.customerName=customerName;

this.totalCost=totalCost;

this.numCopies=numCopies;

OrderTransaction o = new OrderTransaction("16/08/2017", "Create Order", 5,100);

order.add(o);

}

public void add(String date, int amount, double cost ) {

OrderTransaction o = new OrderTransaction(date, "Add", amount, cost);

order.add(o);

numCopies += amount;

totalCost += cost;

}

public void remove(String date, int amount, double cost) throws InvalidOperationException {

if(numCopies

throw new InvalidOperationException ("Remove amount is greater than number of books in the order");

}

else {

numCopies -= amount;

totalCost -= cost;

OrderTransaction o = new OrderTransaction(date, "Remove", amount, cost);

order.add(o);

}

}

public String getTransactionDetail() {

String transaction = " ";

for (int i = 0; i < order.size(); i++) {

transaction += order.get(i).toString();

}

return transaction;

}

public String toString() {

return "Order Name: " + orderNumber + "Customer Name: " + customerName + "Number of Books: " + numCopies + "Total Cost: " + totalCost;

}

}

Test.java

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.util.ArrayList;

public class Test {

public static void main(String args[]) throws IOException, ClassNotFoundException {

ArrayList orderTransactions = new ArrayList();

OrderTransaction one = new OrderTransaction("16/08/2017", "Create order",5,100);

OrderTransaction two = new OrderTransaction("22/08/2017", "Remove",2,40);

OrderTransaction three = new OrderTransaction("23/09/2017", "Add",3,60);

BookOrder first = new BookOrder("Jenny, Lee", 4, 80);

orderTransactions.add(one.toString());

orderTransactions.add(two.toString());

orderTransactions.add(three.toString());

FileOutputStream fos1 = new FileOutputStream("transactions.txt");

ObjectOutputStream oos1 = new ObjectOutputStream(fos1);

oos1.writeObject(orderTransactions);

oos1.flush();

oos1.close();

FileInputStream fis1 = new FileInputStream("transactions.txt");

ObjectInputStream ois1 = new ObjectInputStream(fis1);

ArrayList orderTransactions2 = (ArrayList) ois1.readObject();

ois1.close();

System.out.print("ArrayList: " + " ");

for (int i=0; i

System.out.print(orderTransactions2.get(i) + " ");

//print each element

}

try {

first.add("22/08/2017", 3, 60);

first.remove("23/08/2017", 4, 80);

first.remove("01/09/2017", 5, 100);

first.toString();

FileOutputStream fos2 = new FileOutputStream("orderdetails.txt");

ObjectOutputStream oos2 = new ObjectOutputStream(fos2);

oos2.writeObject(first);

oos2.flush();

oos2.close();

FileInputStream fis2 = new FileInputStream("orderdetails.txt");

ObjectInputStream ois2 = new ObjectInputStream(fis2);

BookOrder first2 = (BookOrder) ois2.readObject();

System.out.println(first2 + " ");

ois2.close();

} catch (InvalidOperationException e) {

System.out.println(e.getMessage() + " ");

}

}

}

My toString method of my BookOrder.java class is not printing out, only the exception in the add method. Can someone help me with this? Thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!