Write a Java program that simulates a simple online shopping system. It has 2 pa
ID: 660578 • Letter: W
Question
Write a Java program that simulates a simple online shopping system. It has 2 part of the pragram, one is the program itself and the other is GUI for the program: (Please include screenshots of the output if you want to answere the question.)
First part:
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.
Create these class files:
--------------------------------------
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.
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.
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.
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.
Second part: GUI
Design an appropriate GUI application to set up and run the proposed OnLine Shopping System that would allow the customer to select the reading material of their choice.
NOTE: It is expected that you should not need to rewrite any of the classes from Part 1. Rather you will create instances of the relevant classes when you need them for Part 2.
The GUI for this program should create at least two new classes one being the JFrame for displaying the GUI JPanel, and the GUI JPanel with its components.
The GUI needs to be able to allow:
*** preload reading material from information read from a file such as ReadingMaterialInputFile.txt;
*** collect a customer's request for reading material in a particular category;
*** run a search to find all available reading material for a match;
*** print out the results of the search to the screen; and
*** calculate purchase price and write out the results to a file such as salesFile.txt.
Remember to incorporate as much error checking in your code to prevent problem user input.
Explanation / Answer
package p11;
import java.io.*;
import java.lang.*;
import java.net.*;
import java.security.*;
import sun.misc.*;
public class B
{
int total;
String productname[]=new String[10],productnameout[]=new String[10],code[]=new String[10],codeout[]=new String[10];
int itemno,countin,countout,noout[]=new int[10],no[]=new int[10],tot,tot1,t;
String customname[]=new String[5],customerid[]=new String[5];
int currentstackin[]=new int[10],currentstackout[]=new int[10],rs[]=new int[10],rss[]=new int[10],h;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,k;
int m;
int q;
B()
{
itemno=0;
countin=0;
countout=0;
tot=tot1=0;
total=0;
n=k=m=q=h=t=0;
}
public void stockin()throws IOException
{
try
{
int n;
System.out.println("Enter the no of item going to stockin");
n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
System.out.println("Enter the product name");
productname[i]=br.readLine();
System.out.println("Enter the product code");
code[i]=br.readLine();
System.out.println("Enter the product price");
rs[i]=Integer.parseInt(br.readLine());
System.out.println("Enter the no of product");
no[i]=Integer.parseInt(br.readLine());
tot+=rs[i]*no[i];
itemno+=no[i];
countin++;
System.out.println("Total amount of the stockin RS "+tot);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
public void stockout()throws IOException
{
System.out.println("Available products");
if(countin>0)
{
for(int i=0;i<countin;i++)
{
System.out.println("Product name: "+productname[i]);
System.out.println("Product code: "+code[i]);
System.out.println("No of Product: "+no[i]);
System.out.println("Product price: "+rs[i]);
}
do
{
System.out.println("Enter the customer name");
customname[q]=br.readLine();
System.out.println("Enter the customer id");
customerid[q]=br.readLine();
q++;t++;
System.out.println("Enter the no of item going to stockout");
n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
System.out.println("Enter the product code");
codeout[i]=br.readLine();
System.out.println("Enter the retail price of the item");
rss[i]=Integer.parseInt(br.readLine());
for(int j=0;j<countin;j++)
{
if(codeout[i].equals(code[j]))
{
System.out.println("Enter the product name");
productnameout[i]=br.readLine();
System.out.println("Enter the no of product ");
noout[i]=Integer.parseInt(br.readLine());
//System.out.println("Enter the retail price of the item");
//rss[i]=Integer.parseInt(br.readLine());
if(noout[i]<=no[j])
{
itemno-=noout[i];
no[j]-=noout[i];
tot1+=rs[j]*noout[i];
System.out.println("total stockout price is"+tot1);
if(no[j]==0)
countin--;//no of product
countout++;//how much they took
}
else
{System.out.println("No sufficient stock");q--;t--;}
}
// else
// {System.out.println("Invalid code for the product");q--;t--;}
}
//tot+=rs[i]*no[i];
//System.out.println("total price is"+tot1);
}
System.out.println("for continuing online shopping press 1");
k=Integer.parseInt(br.readLine());
}while(k==1);
}
else
System.out.println("stock is empty");
}
public void inventory()throws IOException
{
for(int i=0;i<countin;i++)
{
System.out.println("Product name: "+productname[i]);
System.out.println("Product code: "+code[i]);
System.out.println("Product price: "+rs[i]);
System.out.println("No of Product: "+no[i]);
}
System.out.println("Current item number"+itemno);
System.out.println("Current product number"+countin);
if(countin>0)
{
System.out.println("Stockin report");
for(int i=0;i<countin;i++)
{
System.out.println("Product name: "+productname[i]); System.out.println("Product code: "+code[i]);
System.out.println("Product price: "+rs[i]);
System.out.println("No of Product: "+no[i]);
//System.out.println("customer name"+customname[i]);
//System.out.println("customer id"+customerid[i]);
total+=rs[i]*no[i];
}
System.out.println("Total price for the stockin items: "+total);
}}
public void salesreport()throws IOException
{
//int i;
//int m=0;
int po[]=new int[10];
if(countout>0)
{
for(int i=0;i<countin;i++)
{
System.out.println("Sales report");
System.out.println("customer name"+customname[i]);
System.out.println("customer id"+customerid[i]);
System.out.println("Product name: "+productname[i]); System.out.println("Product code: "+code[i]);
System.out.println("Product price: "+rss[i]);
System.out.println("No of Product: "+noout[i]);
po[i]+=noout[i]*rss[i];
System.out.println("total price to pay for that product"+po[i]);
System.out.println();
}
//System.out.println("total price to pay for the customer"+po[i]);
}
else
System.out.println("Stock empty");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.