JAVA Program Specification You are to write a program consis

JAVA:

Program Specification

You are to write a program consisting of the following four classes. Note that some details of the class\'s implementations are left to you. Other details are implied, but not stated directly, in the program description. (That said, don\'t over think this--my JUnit class is less than 50 lines of code, and the next longest class is less than 30.)

SellableItem has 3 instance variables, a String name, a double unitPrice, and an int quantity. The design of the SellableItem class is up to you, but it must have a method that returns the total price of the entire quantity for this item (unitPrice * quantity).

Inventory is an ArrayList of SellableItems. It must contain a method that returns the sum of the total prices of all SellableItems in the list. It must have some way to add new SellableItems to the Inventory.

Utils is a class with two static methods: The first method is a void method, writeTo, that takes a Serializable object and a String filename as parameters. This method writes the given object into a file with the given name. The second method, readFrom, is a generic method with a single generic type parameter. This method takes a parameter String filename. It reads the given file and returns an object of the generic type.

The last class is a JUnit test class. Normally we would write separate JUnit classes for SellableItem, Inventory and Utils, and we would have multiple test methods in each class. But for this assignment, create just one JUnit test class. Put four (or more) JUnit test methods in the class--

one method to test SellableItem\'s method to return total price,

one to test Inventory\'s method to return the total price of all items in the inventory. Inventory for this test should contain multiple SellableItems, each with quantities of more than 1.

a third that tests Utils writeTo and readFrom methods using an Inventory object. (The test for writeTo and readFrom can be that the pre-written Inventory and the post-read Inventory return the same total price.)

a fourth that tests Utils writeTo and readFrom methods using a SellableItem object. (The test can be that the pre-written total price and the post-read total price are equal.)


Remember to follow standard best practices: follow the naming conventions, specify appropriate access control, etc. The purpose of this program is that the program passes the JUnit tests; the classes don\'t have to be generally useful (don\'t worry about toString, for example, or getters/setters that aren\'t used in the JUnit tests). But the classes shouldn\'t be completely hardwired, either. For example, SellableItem objects should be capable of having different quantity values and different unitPrices. Inventory should be able to store any number of SellableItem objects.

Hint:

Program in baby steps. As one example for how to do this, write SellableItem. Then write the JUnit test for SellableItem and use it to debug SellableItem. Then write Utils writeTo and readFrom. Then write and debug the JUnit test for writeTo and readFrom using SellableItem. Then write Inventory. Then write the JUnit test for Inventory to debug it. Finally write and debug the JUnit test for Utils writeTo and readFrom using Inventory. Please, please, please don\'t spend two hours writing all these classes and after that try to debug them.

Requirements:


Utils
has writeTo that takes the specified parameters and writes the specified file
has readFrom that is a generic method,
readFrom reads the specified file, returns an object of the generic type

JUnit tests
test SellableItem total price
test Inventory totaling the prices for multiple SellableItems
test writeTo/readFrom with an Inventory object
test writeTo/readFrom with a SellableItem object


THANK YOU!

Solution

SellableItem.java

public class SellableItem{
    public String name;
    public double unitPrice;
    public int quantity;

    public SellableItem(String name, double unitPrice, int quantity){
        this.name = name;
        this.unitPrice = unitPrice;
        this.quantity = quantity;
    }

    public double totalPrice(){
        return this.unitPrice * this.quantity;
    }
}

Inventory.java

import java.util.ArrayList;
public class Inventory{
    ArrayList<SellableItem> list;

    public Inventory(ArrayList<SellableItem> sellableItems){
        this.list = sellableItems;
    }

    public double totalPrice(){
        double total = 0;
        for (SellableItem sellableItem : sellableItems){
            total += sellableItem.quantity * sellableItem.unitPrice;
        }
        return total;
    }

    public void addSellableItem(SellableItem sellableItem){
        this.list.add(sellableItem);
    }
}

Utils.java

import java.io.*;

public class Utils{
    public static void writeTo(Object object, String fileName){
        FileOutputStream fileOut = new FileOutputStream(fileName);
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(object);
        out.close();
        fileOut.close();
    }

    public static Object readFrom(String fileName){
        FileInputStream fin = new FileInputStream(fileName);
        String s = \"\";
        while(fin.read()>0){
            s += String.valueOf(fin);
        }
        fin.close();
        Object object = s;
        return object;
    }
}

JUnit.java

import static org.junit.Assert.assertEquals;
import junit.framework.Test;

public class JUnit{

    @Test
    public void testSellableItem(){
        int quantity = 20;
        double priceUnit = 20.5;
        String name = \"test\";
        SellableItem sellableItem = new SellableItem(name, priceUnit, quantity);
        assertEquals(\" \", 20*20.5, sellableItem.totalPrice());
    }

    @Test
    public void testInventory(){
        int quantity = 20;
        double priceUnit = 200.5;
        String name = \"test\";
        int quantity2 = 50;
        double priceUnit2 = 244;
        String name2 = \"test2\";
        SellableItem sellableItem = new SellableItem(name, priceUnit, quantity);
        SellableItem sellableItem2 = new SellableItem(name2, priceUnit2, quantity2);
        Inventory inventory = new Inventory();
        inventory.add(sellableItem);
        inventory.add(sellableItem2);
        double total = 20*200.5+50*244;
        assertEquals(\" \", total, inventory.totalPrice());
    }

    @Test
    public void testUtilsInventory(){
        int quantity = 20;
        double priceUnit = 200.5;
        String name = \"test\";
        int quantity2 = 50;
        double priceUnit2 = 244;
        String name2 = \"test2\";
        SellableItem sellableItem = new SellableItem(name, priceUnit, quantity);
        SellableItem sellableItem2 = new SellableItem(name2, priceUnit2, quantity2);
        Inventory inventory = new Inventory();
        inventory.add(sellableItem);
        inventory.add(sellableItem2);
        String file = \"testfile.txt\";
        Utils.write(inventory, file);
        Inventory inventoryTest = Utils.read(file);
        assertEquals(\" \", inventory, inventoryTest);
    }

    @Test
    public void testUtilsSellableItem(){
        int quantity = 20;
        double priceUnit = 200.5;
        String name = \"test\";
        SellableItem sellableItem = new SellableItem(name, priceUnit, quantity);
        String file = \"testfile.txt\";
        Utils.write(sellableItem, file);
        Inventory sellableItemTest = Utils.read(file);
        assertEquals(\" \", sellableItem, sellableItemTest);
    }

}

JAVA: Program Specification You are to write a program consisting of the following four classes. Note that some details of the class\'s implementations are left
JAVA: Program Specification You are to write a program consisting of the following four classes. Note that some details of the class\'s implementations are left
JAVA: Program Specification You are to write a program consisting of the following four classes. Note that some details of the class\'s implementations are left
JAVA: Program Specification You are to write a program consisting of the following four classes. Note that some details of the class\'s implementations are left

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site