Write a Java program that simulates a simple online shopping system. It has 4 pa
ID: 654705 • Letter: W
Question
Write a Java program that simulates a simple online shopping system. It has 4 part of the pragram:
(a)
Using the UML diagram below as a guide, design and implement a set of classes that define various types of reading material. These classes model reading material one would purchase. Include data values that describe various attributes of the material.
ReadingMatter has three instance variables: title (type String), ISBN (type String of 13 characters) and price (type double). Include a constructor and get and set methods for these three instance variables. The class also has a toString( ) method to return a description of the reading matter.
Magazine includes an extra instance variable: editor (type String) with get and set methods for it. Also override toString( ) and content( ) methods to include Magazine?s editor details.
Book includes an extra instance variable: author (type ArrayList) with get and set methods for it. There may be more than one author so allow their names to be stored in an ArrayList. Also override toString( ) method to include Book?s author details.
TextBook includes extra instance variable answers (type boolean) with get and set methods for it. Also override toString( ) method to include TextBook details.
Novel includes extra instance variable characters (type ArrayList) with get and set methods for it. Also override toString( ) method to include all characters in the Novel.
(b)
Populate your online shopping system with at least FOUR entries from each category, e.g., BOOK, MAGAZINE, TEXTBOOK, NOVEL. At the start of your program, you should read in a text file with the required information. The format of the text file will be:
BOOK
TITLE: ?This is book a?
ISBN: ?5978230012546?
PRICE: 56.99
AUTHOR: John Denon
MAGAZINE
TITLE: ?News?
ISBN: ?1154462600125?
PRICE: 7.50
EDITOR: "Stuart, Lagoon"
TEXTBOOK
TITLE: ?Java text book?
ISBN:?9699563285452?
PRICE: 159
AUTHOR: ?Dan, Newman?, ?Adam, Sandstone"
ANSWERS: true
NOVEL
TITLE: ?A Road To The Village?
ISBN: ?549556897K?
PRICE: 22.99
AUTHOR: ?Daniell, K.P.?
CHARACTERS: ?Ron Jerrard?, ?Billy Sun?, ?Sandra Newman?
Note: The words BOOK, MAGAZINE, TEXTBOOK, and NOVEL are always presented in capital letters. There is always a blank line between entries in the file. TITLE, ISBN, PRICE, AUTHOR ANSWERS, and EDITOR are always capitalised. After one of these words will be a ?:?, followed by a blank space followed by the required data.
(c)
Define a class ShoppingCart that emulates a shopping cart. Define a method addToCart( ) to add reading materials to the cart and update the total price instance variable. Note the buyer is able to select from a list of reading material from each category. Also define a toString( ) method which returns the contents of the cart together with the summary information of the items in it.
Create a main driver class entitled CheckOut that should have a loop to continue as long as the user wants to shop. After selecting the menu item the program should prompt the user to add reading material details. When the user finishes shopping, print the cart contents and ?Please pay?..? message with the total price of the items in the cart.
(d)
After a sale is completed, update a sales file (entitle: salesFile.txt) with the total purchase price from each of the four categories, as well as the date of the last sale. For example, if the file originally contains:
2009-09-09
BOOK: 1123.90
MAGAZINE: 145.67
TEXTBOOK: 2634.60
NOVEL: 573.20
And another sale was completed on 2009-09-10 for a novel costing $21.90, the file would now show:
2009-09-10
BOOK: 1123.90
MAGAZINE: 145.67
TEXTBOOK: 2634.60
NOVEL: 595.10
It should be possible to retrieve the information from the salesFile just prior to exiting the program.
ReadingMatter Book Magazine TextBook NovelExplanation / Answer
c:
Client:
package st.hello;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.naming.*;
import javax.ejb.*;
import javax.rmi.PortableRemoteObject;
public class Client {
private static final String JNDI_NAME = "st.hello";
public static void main(String[] args) throws Exception
{
String url = "t3://localhost:7001";
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
Context ctx = new InitialContext(h);
// Lookup the beans home using JNDI
Object home = ctx.lookup(JNDI_NAME);
HelloHome hellohome = (HelloHome) PortableRemoteObject.narrow(home,HelloHome.class);
Hello hello = hellohome.create();
System.out.println(hello.sayHello("Siva"));
}
}
Item:
// Item class
package obs;
public class Item
implements java.io.Serializable
{
private String name;
private String isbn;
private String title;
private int price;
private int author;
public Item(String isbn,String title, int price)
{
this.isbn = isbn;
this.title = title;
this.price = price;
this.author = author;
}
public String setTitle()
{
System.out.println("TITLE:" +title);
}
public String setTitle()
{
System.out.println("ISBN:" +isbn);
}
public int setPrince()
{
System.out.println("PRICE:" +price);
}
public int setAuthor()
{
System.out.println("AUTHOR:" +author);
}
} // end of class
Order:
package obs.order;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
import java.util.*;
public interface Order extends EJBObject
{
public String addOrder(int userid, ArrayList items)
throws RemoteException;
public boolean cancelOrder(int ordid) throws RemoteException;
}
OrderBean:
package obs.order;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import obs.*;
public class OrderBean implements SessionBean {
public void ejbActivate() {}
public void ejbRemove() {} // end of remove
public void ejbPassivate() {}
public void setSessionContext(SessionContext ctx) {}
public void ejbCreate () throws CreateException {}
public String addOrder(int userid, ArrayList items)
{
Connection con=null;
String ordid;
PreparedStatement ps=null;
try
{
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("oracle");
con =ds.getConnection();
// get highest order id + 1 for this order
ps= con.prepareStatement("select ordid.nextval from dual");
ResultSet rs= ps.executeQuery();
rs.next();
ordid = rs.getString(1);
rs.close();
// get total amount
int totamt =0;
Item item;
Iterator itr = items.iterator();
while ( itr.hasNext())
{
item = (Item) itr.next();
totamt += item.getPrice() * item.getQty();
}
ps = con.prepareStatement("insert into orders values(?,?,sysdate,?,'n')");
ps.setString(1,ordid);
ps.setInt(2,userid);
ps.setInt(3,totamt);
ps.executeUpdate();
ps.close();
// insert into orderitems
ps = con.prepareStatement("insert into orderitem values(?,?,?,?)");
itr = items.iterator();
while ( itr.hasNext())
{
item = (Item) itr.next();
ps.setString(1,ordid);
ps.setString(2, item.getIsbn());
ps.setInt(3,item.getPrice());
ps.setInt(4,item.getQty());
ps.executeUpdate();
}
return ordid;
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{ if (ps != null ) ps.close();
if (con != null) con.close();
}
catch(Exception ex) {}
}
return null;
} // end of addOrder
public boolean cancelOrder(int ordid)
{
Connection con=null;
PreparedStatement ps=null;
try
{
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("oracle");
con =ds.getConnection();
// delete from ORDERITEM table
ps = con.prepareStatement("delete from orderitem where ordid = ?");
ps.setInt(1,ordid);
ps.executeUpdate();
ps.close();
// delete from ORDERS table
ps = con.prepareStatement("delete from orders where ordid = ?");
ps.setInt(1,ordid);
int cnt = ps.executeUpdate();
ps.close();
if (cnt == 1) return true;
else return false;
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{ if (ps != null ) ps.close();
if (con != null) con.close();
}
catch(Exception ex) {}
}
return false;
} // end of cancelOrder
} // end of OrderBean
OrderHome:
package obs.order;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface OrderHome extends EJBHome {
Order create() throws CreateException, RemoteException;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.