=============================================================== NEED HELP WITH T
ID: 661421 • Letter: #
Question
===============================================================
NEED HELP WITH THIS JAVA PROJECT. PLEASE ANSWER THIS
===============================================================
Write a Java program that simulates a simple online shopping system. It has 3 part of the pragram, one is the program itself and the second is GUI for the program & third part is client-server version:
(Please include screenshots of the output )
Part 1: Main Application
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.
Part 2 : GUI Application
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.
Part 3: Server and client versions
For this part of the assignment you may assume that the ONLINE Shopping System running on the server is preloaded with a group of reading material created using data from ReadingMaterilaInputFile.txt. The client sends a UDP message (the customer?s request) to the server, which is waiting to receive packets at a well-known IP address and UDP port number. (The server's port number for this assignment is 13579.) The customer request contains the category and the title of the reading material they wish to purchase.
The protocol for this problem runs on top of the Internet Protocol (IP) and the User Datagram Protocol, UDP. Recall that a protocol is an agreement among communicating parties regarding the form and meaning of the messages they send each other. It describes how messages are encoded, and how the behaviour of each participant is affected by receiving each message, as well as when messages are sent.
You may be familiar with TCP, the protocol used for the Web and many other services. UDP provides a best-effort message delivery service, instead of the reliable byte-stream service that TCP provides. UDP is ideal for protocols that require only a single message in each direction, like the need for a customer to communicate with the proposed OnLine Shopping System. Messages that stand alone, and are delivered independently from other messages (with no guarantees about order or reliability) are called datagrams (thus the name UDP).
The server, may or may, not implement the GUI. The server will validate the message received from a customer, that is, checks that there is a student number and subject code in the message. The server runs the customer?s request and then sends back a message with the title and isbn details of the first reading material in its internal listing that is able to satisfy the
customer request.
The client's initial message might be lost in the network. Therefore, if a response is not received in a certain time (10 seconds), the client times out and retransmits the request message; this may be repeated several times before the client finally gives up and declares failure.
Explanation / Answer
Here is the code that creates shopping cart . Loads data from ReadingMaterialInputFile.txt in the beginning and during checkout updates the salesFile.txt.
CheckOut.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class CheckOut {
ShoppingCart cart;
public CheckOut()
{
cart = new ShoppingCart();
}
public void loadFile(String inputfile)
{
Scanner scanner;
try {
scanner = new Scanner(new File(inputfile));
} catch (FileNotFoundException e) {
System.out.println("File not found: "+inputfile+" No data is added.");
return;
}
String type;
ReadingMatter item;
while(scanner.hasNext())
{
type = scanner.nextLine();
if(type.trim().equals(""))
continue;
switch(type)
{
case "BOOK":
item = new Book();
getBookDetails(scanner,(Book)item);
cart.addToCart(item);
break;
case "TEXTBOOK":
item = new TextBook();
getTextBookDetails(scanner,(TextBook)item);
cart.addToCart(item);
break;
case "NOVEL":
item = new Novel();
getNovelDetails(scanner,(Novel)item);
cart.addToCart(item);
break;
case "MAGAZINE":
item = new Magazine();
getMagazineDetails(scanner, (Magazine)item);
cart.addToCart(item);
break;
}
}
System.out.println("Loaded "+cart.getNumCartItems()+" items into cart from "+inputfile);
scanner.close();
}
public void printSummary()
{
System.out.println(cart.toString());
}
private void getReadingMatterDetails(Scanner scanner, ReadingMatter r)
{
String line, title, isbn;
double price;
int idx;
String authors[];
line = scanner.nextLine();
title = line.replace("TITLE:","").replaceAll(""","").trim();
r.setTitle(title);
line = scanner.nextLine();
isbn = line.replace("ISBN:","").replaceAll(""","").trim();
r.setISBN(isbn);
line = scanner.nextLine();
price = Double.parseDouble(line.replace("PRICE:","").trim());
r.setPrice(price);
}
private void getBookDetails(Scanner scanner, Book b)
{
getReadingMatterDetails(scanner, b);
String line, authors[];
line = scanner.nextLine();
authors=line.replace("AUTHOR:","").split(""");
for(int i=0;i< authors.length;i++)
if(!authors[i].replace(",", "").trim().equals(""))
b.addAuthor(authors[i].trim());
}
private void getMagazineDetails(Scanner scanner, Magazine m )
{
getReadingMatterDetails(scanner, m);
String line,editor;
line = scanner.nextLine();
editor=line.replace("EDITOR:","").replaceAll(""","").trim();
m.setEditor(editor);
}
private void getTextBookDetails(Scanner scanner, TextBook b)
{
getBookDetails(scanner, b);
String line ;
line = scanner.nextLine();
b.setAnswers(Boolean.parseBoolean(line.replace("ANSWERS:","").trim())) ;
}
private void getNovelDetails(Scanner scanner, Novel b)
{
getBookDetails(scanner, b);
String line, characters[];
line = scanner.nextLine();
characters=line.replace("CHARACTERS:","").split(""");
for(int i=0;i< characters.length;i++)
if(!characters[i].replace(",", "").trim().equals(""))
b.addCharacter(characters[i].trim());
}
public void mainMenu(Scanner keyboard)
{
int choice;
while(true)
{
System.out.println("-----------SHOPPING MALL-----------");
System.out.println("Select Menu (1-5) ");
System.out.println("1. Add Book to shopping cart");
System.out.println("2. Add Magazine to shopping cart");
System.out.println("3. Add TextBook to shopping cart");
System.out.println("4. Add Novel to shopping cart");
System.out.println("5. Exit");
System.out.print(" Your choice: ");
choice = keyboard.nextInt();
if(choice == 1)
{
cart.addToCart(bookMenu(null,keyboard,new Book()));
}
else if (choice == 2)
{
cart.addToCart(magazineMenu(keyboard,new Magazine()));
}
else if (choice == 3)
{
cart.addToCart(textbookMenu(keyboard,new TextBook()));
}
else if (choice == 4)
{
cart.addToCart(novelMenu(keyboard,new Novel()));
}
else if (choice == 5)
{
updateSalesFile();
return;
}
else
{
System.out.println("Invalid menu choice!");
}
}
}
private void updateSalesFile()
{
Scanner s;
double bookAmt=0, magAmt=0, txtAmt=0, nvlAmt=0;
try {
s = new Scanner(new File("salesFile.txt"));
s.nextLine();
bookAmt = Double.parseDouble(s.nextLine().replace("BOOK:", "").trim());
magAmt = Double.parseDouble(s.nextLine().replace("MAGAZINE:", "").trim());
txtAmt = Double.parseDouble(s.nextLine().replace("TEXTBOOK:", "").trim());
nvlAmt = Double.parseDouble(s.nextLine().replace("NOVEL:", "").trim());
s.close();
} catch (FileNotFoundException e) {
}
bookAmt += cart.getBookTotal();
magAmt +=cart.getMagazineTotal();
txtAmt += cart.getTextbookTotal();
nvlAmt += cart.getNovelTotal();
try {
PrintWriter writer = new PrintWriter(new File("salesFile.txt"));
Date date = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
writer.write(sdf.format(date)+" ");
writer.write("BOOK: "+String.format("%.2f",bookAmt)+" ");
writer.write("MAGAZINE: "+String.format("%.2f",magAmt)+" ");
writer.write("TEXTBOOK: "+String.format("%.2f",txtAmt)+" ");
writer.write("NOVEL: "+String.format("%.2f",nvlAmt)+" ");
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
System.out.println("Error writing to salesFile.txt");
}
}
private ReadingMatter commonDetails(String heading,Scanner keyboard,ReadingMatter m)
{
System.out.println(heading);
keyboard.nextLine();
System.out.print(" Title: ");
m.setTitle(keyboard.nextLine().trim());
System.out.print(" ISBN: ");
m.setISBN(keyboard.nextLine().trim());
System.out.print(" Price: ");
m.setPrice(keyboard.nextDouble());
return m;
}
private Book bookMenu(String heading, Scanner keyboard, Book b)
{
String choice;
if(heading == null)
heading = "Adding a Book";
commonDetails(heading,keyboard, b);
do
{
System.out.print(" Author : ");
keyboard.nextLine();
b.addAuthor(keyboard.nextLine().trim());
System.out.print(" Add another author ? (y/n)");
choice = keyboard.next();
}while(choice.toLowerCase().equals("y"));
return b;
}
private TextBook textbookMenu(Scanner keyboard, TextBook b)
{
bookMenu("Adding a TextBook", keyboard, b);
System.out.print(" Answers : ");
b.setAnswers(keyboard.nextBoolean());
return b;
}
private Novel novelMenu(Scanner keyboard, Novel n)
{
String choice;
bookMenu("Adding a Novel",keyboard, n);
do
{
keyboard.nextLine();
System.out.print(" Character : ");
n.addCharacter(keyboard.nextLine().trim());
System.out.print(" Add another character ? (y/n)");
choice = keyboard.next();
}while(choice.toLowerCase().equals("y"));
return n;
}
private Magazine magazineMenu(Scanner keyboard, Magazine m)
{
commonDetails("Adding a Magazine",keyboard, m);
keyboard.nextLine();
System.out.print(" Editor : ");
m.setEditor(keyboard.nextLine().trim());
return m;
}
public static void main(String[] args) {
CheckOut chkout=new CheckOut();
Scanner keyboard = new Scanner(System.in);
chkout.loadFile("ReadingMaterialInputFile.txt");
chkout.mainMenu(keyboard);
chkout.printSummary();
}
}
ShoppingCart.java
import java.util.ArrayList;
public class ShoppingCart {
protected double bookTotal;
protected double magazineTotal;
protected double textbookTotal;
protected double novelTotal;
ArrayList<ReadingMatter> cartItems;
public ShoppingCart()
{
bookTotal = 0;
magazineTotal = 0;
textbookTotal = 0;
novelTotal = 0;
cartItems = new ArrayList<ReadingMatter>();
}
public void addToCart(ReadingMatter item)
{
cartItems.add(item);
if(item instanceof Novel)
novelTotal += item.getPrice();
else if(item instanceof TextBook)
textbookTotal += item.getPrice();
else if(item instanceof Book)
bookTotal += item.getPrice();
else if(item instanceof Magazine)
magazineTotal += item.getPrice();
}
public String toString()
{
String str="There are "+getNumCartItems()+" items in the cart ";
for(int i = 0; i < cartItems.size(); i++)
str += cartItems.get(i)+" ";
str += " _______________________________ ";
str+="Total for Books: "+bookTotal+" ";
str+="Total for Magazine: "+magazineTotal+" ";
str+="Total for TextBook: "+textbookTotal+" ";
str+="Total for Novel: "+novelTotal+" ";
double grandTotal=bookTotal + magazineTotal + textbookTotal + novelTotal;
str+="Grand Total: "+String.format("%.2f", grandTotal);
str+=" Please pay "+String.format("%.2f", grandTotal);
return str;
}
public double getBookTotal() {
return bookTotal;
}
public double getMagazineTotal() {
return magazineTotal;
}
public double getTextbookTotal() {
return textbookTotal;
}
public double getNovelTotal() {
return novelTotal;
}
public double getGrandTotal()
{
return bookTotal + textbookTotal + novelTotal + magazineTotal;
}
public int getNumCartItems()
{
return cartItems.size();
}
public ReadingMatter getItem(int i)
{
return cartItems.get(i);
}
}
ReadingMaterial.java
public class ReadingMatter {
protected String title;
protected String ISBN;
protected double price;
public ReadingMatter()
{
title = "";
ISBN = "";
price = 0;
}
public ReadingMatter(String title_in, String isbn_in, double price_in)
{
title = title_in;
ISBN = isbn_in;
price = price_in;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString()
{
return " Title: "+title+" ISBN: "+ISBN +" Price: "+price;
}
}
Book.java
import java.util.ArrayList;
public class Book extends ReadingMatter {
protected ArrayList<String> authors;
public Book()
{
authors = new ArrayList<String>();
}
public Book(String title_in, String isbn_in,double price_in)
{
super(title_in,isbn_in, price_in);
authors = new ArrayList<String>();
}
public int getNumAuthors()
{
return authors.size();
}
public String getAuthor(int i)
{
return authors.get(i);
}
public void addAuthor(String name)
{
authors.add(name);
}
public String toString()
{
String str = super.toString()+" Authors: ";
for(int i=0; i<authors.size() ;i++)
str += authors.get(i)+"; ";
return str;
}
}
Textbook.java
public class TextBook extends Book {
protected boolean answers;
public TextBook()
{
answers = false;
}
public void setAnswers(boolean ans)
{
answers = ans;
}
public boolean getAnswers()
{
return answers;
}
public String toString()
{
return super.toString()+" Answers: "+answers;
}
}
Novel.java
import java.util.ArrayList;
public class Novel extends Book {
protected ArrayList<String> characters;
public Novel()
{
characters = new ArrayList<String>();
}
public int getNumCharacters()
{
return characters. size();
}
public void addCharacter(String name)
{
characters.add(name);
}
public String getCharacter(int i)
{
return characters.get(i);
}
public String toString()
{
String str=super.toString()+" Characters: ";
for(int i = 0; i < characters.size(); i++)
str += characters.get(i)+"; ";
return str;
}
}
Magazine.java
public class Magazine extends ReadingMatter {
protected String editor;
public Magazine()
{
editor = "";
}
public Magazine(String title_in, String isbn_in, double price_in, String editor_in)
{
super(title_in,isbn_in,price_in);
editor = editor_in;
}
public String getEditor() {
return editor;
}
public void setEditor(String editor) {
this.editor = editor;
}
public String toString()
{
return super.toString()+" Editor: "+editor;
}
}
Input file ReadingMaterialInputFile.txt
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"
sample outpu
Loaded 4 items into cart from ReadingMaterialInputFile.txt
-----------SHOPPING MALL-----------
Select Menu (1-5)
1. Add Book to shopping cart
2. Add Magazine to shopping cart
3. Add TextBook to shopping cart
4. Add Novel to shopping cart
5. Exit
Your choice: 1
Adding a Book
Title: book
ISBN: dkdfksdk
Price: 222
Author : autor1
Add another author ? (y/n)y
Author : author2
Add another author ? (y/n)n
-----------SHOPPING MALL-----------
Select Menu (1-5)
1. Add Book to shopping cart
2. Add Magazine to shopping cart
3. Add TextBook to shopping cart
4. Add Novel to shopping cart
5. Exit
Your choice: 4
Adding a Novel
Title: novel1
ISBN: 32932
Price: 44
Author : author5
Add another author ? (y/n)y
Author : author6
Add another author ? (y/n)n
Character : charcter1
Add another character ? (y/n)n
-----------SHOPPING MALL-----------
Select Menu (1-5)
1. Add Book to shopping cart
2. Add Magazine to shopping cart
3. Add TextBook to shopping cart
4. Add Novel to shopping cart
5. Exit
Your choice: 5
There are 6 items in the cart
Title: This is book a
ISBN: 5978230012546
Price: 56.99
Authors: John Denon;
Title: News
ISBN: 1154462600125
Price: 7.5
Editor: Stuart, Lagoon
Title: Java text book
ISBN: 9699563285452
Price: 159.0
Authors: Dan, Newman; Adam, Sandstone;
Answers: true
Title: A Road To The Village
ISBN: 549556897K
Price: 22.99
Authors: Daniell, K.P.;
Characters: Ron Jerrard; Billy Sun; Sandra Newman;
Title: book
ISBN: dkdfksdk
Price: 222.0
Authors: autor1; author2;
Title: novel1
ISBN: 32932
Price: 44.0
Authors: author5; author6;
Characters: charcter1;
_______________________________
Total for Books: 278.99
Total for Magazine: 7.5
Total for TextBook: 159.0
Total for Novel: 66.99
Grand Total: 512.48
Please pay 512.48
output file salesFile.txt
2017-04-29
BOOK: 278.99
MAGAZINE: 7.50
TEXTBOOK: 159.00
NOVEL: 66.99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.