Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this project, you’re going to create a client-server connection using Java.

ID: 3815362 • Letter: F

Question

For this project, you’re going to create a client-server connection using Java. This program is a very simple server-based program that has many uses in the modern age, such as online banking, online gaming, school registries, and many more. 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.

User-friendliness is important! Make sure that your program looks nice and easy to use.

Send regular messages to the server’s console. At the very least, send a message when the server connects, when input is received from the client, and when the server disconnects.

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.

Leave documentation and comments in your code to explain things step-by-step. This is just good practice. It doesn’t have to be a lot; just enough to explain your process in a simple way.

Explanation / Answer

ShoppingStore.java:

import java.util.HashMap;
import java.util.Random;

public class ShoppingStore {
   HashMap<String, Integer> itemInventory;

   // create the shopping store
   public ShoppingStore() {
       Random random = new Random();
       itemInventory = new HashMap<>();

       // populate random quantities of fruits between 10-20
       itemInventory.put("Banana", 10 + random.nextInt(11));
       itemInventory.put("Oranges", 10 + random.nextInt(11));
       itemInventory.put("Pineapples", 10 + random.nextInt(11));
       itemInventory.put("Watermelons", 10 + random.nextInt(11));
   }

   // checkout user cart and calculate the amount to be charged
   public int purchase(HashMap<String, Integer> cartItems) {
       int cartValue = 0;
       for (String fruit : cartItems.keySet()) {
           switch (fruit) {
               case "Banana":
                   cartValue += 2 * cartItems.get(fruit);
                   break;
               case "Oranges":
                   cartValue += 3 * cartItems.get(fruit);
                   break;
               case "Pineapples":
                   cartValue += 5 * cartItems.get(fruit);
                   break;
               case "Watermelons":
                   cartValue += 8 * cartItems.get(fruit);
                   break;
           }
          
           // update quantity in inventory
           itemInventory.put(fruit, itemInventory.get(fruit) - cartItems.get(fruit));
       }
      
       return cartValue;
   }

   public HashMap<String, Integer> getInventory() {
       return itemInventory;
   }

   public void connect() {
       System.out.println("Client is now connected to store.");
   }
  
   public void printInventory() {
       System.out.println("+++++++++++++++++ INVENTORY +++++++++++++++++++");
       for(String fruit: itemInventory.keySet()) {
           System.out.println(fruit + ": " + itemInventory.get(fruit) + " Nos.");
       }
       System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++");
   }
  

   public void disconnect() {
       System.out.println("Client is now disconnecting with store.");
   }
}



ClientPortal.java:

import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ClientPortal {

   static Scanner s = new Scanner(System.in);

   public static void main(String args[]) {
       ShoppingStore shoppingStore = new ShoppingStore();

       shoppingStore.connect();

       HashMap<String, Integer> cart = new HashMap<>();
       HashMap<String, Integer> inventory = shoppingStore.getInventory();

       int userChoice = showInventoryAndGetChoice(inventory);
       while (true) {

           if (userChoice == 1) {
               showCart(cart);
           } else if (userChoice == 2) {
               int quantity = askQuantity();
               if (quantity > inventory.get("Banana")) {
                   System.out
                           .println("Sufficient quantity not available in inventory. Available: "
                                   + inventory.get("Banana"));
               } else {
                   cart.put("Banana", quantity);
                   System.out.println("Successfully added "
                           + cart.get("Banana") + " Nos. of Banana");
               }
           } else if (userChoice == 3) {
               int quantity = askQuantity();
               if (quantity > inventory.get("Oranges")) {
                   System.out
                           .println("Sufficient quantity not available in inventory. Available: "
                                   + inventory.get("Oranges"));
               } else {
                   cart.put("Oranges", quantity);
                   System.out.println("Successfully added "
                           + cart.get("Oranges") + " Nos. of Oranges");
               }
           } else if (userChoice == 4) {
               int quantity = askQuantity();
               if (quantity > inventory.get("Pineapples")) {
                   System.out
                           .println("Sufficient quantity not available in inventory. Available: "
                                   + inventory.get("Pineapples"));
               } else {
                   cart.put("Pineapples", quantity);
                   System.out.println("Successfully added "
                           + cart.get("Pineapples") + " Nos. of Pineapples");
               }
           } else if (userChoice == 5) {
               int quantity = askQuantity();
               if (quantity > inventory.get("Watermelons")) {
                   System.out
                           .println("Sufficient quantity not available in inventory. Available: "
                                   + inventory.get("Watermelons"));
               } else {
                   cart.put("Watermelons", quantity);
                   System.out.println("Successfully added "
                           + cart.get("Watermelons") + " Nos. of Watermelons");
               }
           } else if (userChoice == 6) {
               int totalValue = shoppingStore.purchase(cart);
               System.out
                       .println(" ++++++++++++++++ Summary +++++++++++++++++++");
               showCart(cart);
               System.out.println("++++++++++++++++ Total Cart Value:"
                       + totalValue + "$ +++++++++++++++++++ ");

               inventory = shoppingStore.getInventory();
               shoppingStore.printInventory();
               break;
           }
           userChoice = showInventoryAndGetChoice(inventory);
       }

       shoppingStore.disconnect();

       s.close();
   }

   private static void showCart(HashMap<String, Integer> cart) {
       if (cart.size() != 0) {
           System.out.println(" You have purchased below items:");
           for (String fruit : cart.keySet()) {
               System.out.println(fruit + ": " + cart.get(fruit) + " Nos.");
           }
       } else {
           System.out.println(" You don't have anything in cart");
       }
   }

   public static int showInventoryAndGetChoice(
           HashMap<String, Integer> inventory) {
       System.out.println(" +++++++++++++++++ MENU +++++++++++++++++");
       System.out.println("1. Display items chosen to buy");
       System.out.println("2. Purchase Bananas ($2 each)");
       System.out.println("3. Purchase Oranges ($3 each)");
       System.out.println("4. Purchase Pineapples ($5 each)");
       System.out.println("5. Purchase Watermelons ($8 each)");
       System.out.println("6. Finish Shopping");
       System.out.print("+++++++++++++++++ Enter Choice: ");

       int choice = 0;

       while (true) {

           try {
               choice = Integer.parseInt(s.next());
           } catch (Exception e) {
               // make choice invalid
               choice = 0;
           }

           if (choice <= 0 || choice > 6) {
               System.out.print("Invalid choice.(1-6) only. Try again. Enter Choice: ");
           } else {
               break;
           }
       }

       return choice;
   }

   public static int askQuantity() {
       System.out.print(" Enter Quantity: ");

       int quantity = 0;

       while (true) {

           try {
               quantity = Integer.parseInt(s.next());
           } catch (InputMismatchException e) {
               // make quantity invalid
               quantity = 0;
           }

           if (quantity <= 0) {
               System.out.print("Invalid quantity.Positive only. Try again. Enter Quantity: ");
           } else {
               break;
           }
       }

       return quantity;
   }
}

Sample Output:

Client is now connected to store.

+++++++++++++++++ MENU +++++++++++++++++
1. Display items chosen to buy
2. Purchase Bananas ($2 each)
3. Purchase Oranges ($3 each)
4. Purchase Pineapples ($5 each)
5. Purchase Watermelons ($8 each)
6. Finish Shopping
+++++++++++++++++ Enter Choice: 2

Enter Quantity: 12
Sufficient quantity not available in inventory. Available: 10

+++++++++++++++++ MENU +++++++++++++++++
1. Display items chosen to buy
2. Purchase Bananas ($2 each)
3. Purchase Oranges ($3 each)
4. Purchase Pineapples ($5 each)
5. Purchase Watermelons ($8 each)
6. Finish Shopping
+++++++++++++++++ Enter Choice: 3

Enter Quantity: 14
Successfully added 14 Nos. of Oranges

+++++++++++++++++ MENU +++++++++++++++++
1. Display items chosen to buy
2. Purchase Bananas ($2 each)
3. Purchase Oranges ($3 each)
4. Purchase Pineapples ($5 each)
5. Purchase Watermelons ($8 each)
6. Finish Shopping
+++++++++++++++++ Enter Choice: 2

Enter Quantity: 10
Successfully added 10 Nos. of Banana

+++++++++++++++++ MENU +++++++++++++++++
1. Display items chosen to buy
2. Purchase Bananas ($2 each)
3. Purchase Oranges ($3 each)
4. Purchase Pineapples ($5 each)
5. Purchase Watermelons ($8 each)
6. Finish Shopping
+++++++++++++++++ Enter Choice: 6

++++++++++++++++ Summary +++++++++++++++++++

You have purchased below items:
Oranges: 14 Nos.
Banana: 10 Nos.
++++++++++++++++ Total Cart Value:62$ +++++++++++++++++++

+++++++++++++++++ INVENTORY +++++++++++++++++++
Pineapples: 13 Nos.
Watermelons: 17 Nos.
Oranges: 6 Nos.
Banana: 0 Nos.
+++++++++++++++++++++++++++++++++++++++++++++++
Client is now disconnecting with store.


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote