For this project, you’re going to create a client-server connection using Java c
ID: 3799597 • Letter: F
Question
For this project, you’re going to create a client-server connection using Java code. For this project, we’re going to create an application for online shopping.
The idea here is simple. A user should be able to browse a small selection of fruits from an online vender, choose which one(s) to purchase and how many, and end the transaction. This is how it’s going to work. A user will log on to the online store (which we will assume is done prior to the start of the application). For the purposes of this assignment, we’re going to use ‘localhost’ for the IP. You may use any PORT number you wish as long as it’s not reserved.
When the application starts running, the user is logged in and given a list of fruits. The user may then select any of these options:
• Display items chosen to buy
• Purchase Bananas ($2 each)
• Purchase Oranges ($3 each)
• Purchase Pineapples ($5 each)
• Purchase Watermelons ($8 each)
• Finish Shopping
For the purposes of this assignment, we’re going to assume that the user has an unlimited amount of money for shopping purposes. The store, however, does not. When the server connects, the client will get the inventory from the store’s server so that the user will know how many of each item is in stock. When the user selects to purchase an item, the user will be prompted for the quantity to buy. Upon giving a quantity, the user’s cart should be updated with the number of the specific fruit and the client’s inventory should decrease by the amount purchased. Finally, when the user decides to finish shopping, the program should display the total cost of the items the user purchased and close the connection. When your program starts, the server should accept a connection from the client, and then generate a random inventory of items. Each item should have a random stock assigned from 10 to 20. The inventory values should be sent to the client, which will then display them to the user within the menu of purchase options. When a purchase is made, the inventory should be updated in the client for the item purchased. The user’s current list of items should be stored on the client end and displayed neatly when the user chooses to see it. Finally, when the user is done shopping, the final list of items should be sent to the server, which will calculate the total price and update its own inventory, then send the total price to the client which will display the total and then finish execution.
There are a few guidelines that you must follow for this program. When you have finished creating the program, make sure to check back over these guidelines to make sure that everything has been accounted for. The guidelines are as follows:
• The program must be a console-based application. No GUIs allowed.
• The program must loop through the options until the user selects to exit.
• You may get user input any way you choose – however, you must catch invalid entries. This means that if your options are 1-6, then a user entry ‘F’ should be caught and give an invalid entry feedback, then allow the user to reenter their choice. Valid entries must also be checked when asking for quantities – positive integers only.
• Your program must account for the amount of inventory in the store. If a user chooses to buy 10 pineapples and the store only has 8, then an error message should show and the purchase should not be completed.
• Make sure that in the natural runtime of your program when the server is disconnected that all sockets, scanners, and any other form of I/O is closed – leaving these open can cause a security leak that, while harmless on a localhost, can be dangerous on other IPs.
Explanation / Answer
below mentioned source code is for noraml shopping cart.....
package x.x.shopping.session;
import static x.x.shopping.db.DbController.
COL_CATEGORY_ID;
import static x.x.shopping.model.Model.MODEL;
import static x.x.shopping.model.SalesTaxMap.getSalesTaxRate;
import java.math.BigDecimal;
import java.math.MathContext;
import x.x.shopping.model.Product;
import x.x.shopping.model.User;
public final class SessionModel {
private static final BigDecimal BD_ZERO = new BigDecimal("0.00");
private static final BigDecimal BD_SHIPPING_CHARGES_DEFAULT = new BigDecimal( "6.99");
private static final MathContext ROUNDING_POLICY= new MathContext(2);
private final Products products = new Products();
private String categoryId ;
private User user;
private String zip;
private BigDecimal orderTotal = BD_ZERO;
private BigDecimal salesTax = BD_ZERO;
private BigDecimal deliveryCharge = BD_SHIPPING_CHARGES_DEFAULT;
private BigDecimal finalTotal = BD_ZERO;
private boolean orderPlaced = false;
private void calculate() {
BigDecimal total =BD_ZERO;
for(final Product product:products) total = total.add(product.getPrice());
orderTotal= total;
final BigDecimal salesTaxRate = (zip == null ) ? BD_ZERO :getSalesTaxRate(zip);
salesTax=total.multiply(salesTaxRate).round(ROUNDING_POLICY).setScale(2);
finalTotal= total.add(salesTax).add(deliveryCharge);
}
public final Products getProductsForCategory()
{
return MODEL.queryProducts(COL_CATEGORY_ID,categoryId);
}
public final void addProduct(final String id)
{
final Product product = MODEL.getProduct(id);
if ((product ==null) ||products.contains(product))
return;
products.add(product);
calculate();
orderInProgress();
}
public final void deleteProduct(final String id)
{
final Product product =products.removeById(id);
if(product !=null) calculate();
orderInProgress();
}
public final User getUser() {
return user;
}
public final boolean isLoggedIn()
{
return user!=null;
}
public finalUser login(final String username,final String password)
{
user=MODEL.getUser(username,password);
return user;
}
public final User register(final String username,final String password)
{
user=MODEL.registerUser(username,password);
return user;
}
public final void setCategoryId(final String id)
{
categoryId= id;
}
public final String getCategoryName()
{
return (categoryId==null) ?"Online Shopping Center":MODEL.getCategories().getCategoryName(categoryId);
}
public final Products getProducts()
{
return products;
}
public final boolean hasProducts()
{
return !products.isEmpty();
}
public final booleanisLoggedInAndHasProducts()
{
return isLoggedIn() && hasProducts();
}
public final String getZip()
{
return zip;
}
public final voidsetZip(final String z)
{
zip= z; calculate();}
public final BigDecimal getSalesTax()
{
return salesTax;
}
public final BigDecimal getDeliveryCharge() {
return deliveryCharge;
}
public final void setDeliveryCharge(final BigDecimal charge)
{
deliveryCharge = charge; calculate();
}
public final BigDecimal getOrderTotal()
{
return orderTotal;
}
public finalBigDecimal getFinalTotal()
{
return finalTotal;
}
public void reinitialize()
{
products.clear();
setZip(null);
salesTax=BD_ZERO;
deliveryCharge=BD_ZERO;
finalTotal=BD_ZERO;
setOrderPlaced(true);
}
public boolean isOrderPlaced()
{
return orderPlaced;
}
private void setOrderPlaced( final boolean op)
{
this.orderPlaced= op;
}
private void orderInProgress()
{
setOrderPlaced(false);
}
}
this is for normal online fruit shopping:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.