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

I need help with this Java program. Please use only basic Java concepts. I am on

ID: 3777143 • Letter: I

Question

I need help with this Java program. Please use only basic Java concepts. I am only allowed to use what I have learned in this introductory class. To give you an idea of how advanced the class is, some of the concepts that we have learned include: class and object interactions, arrays, wrapper classes/text processing, file I/O, inheritence, polymorphism, exceptions, random access files, OOD, object serialization, abstract classes/methods, interfaces, and recursion.

Make a product class which has a price, ID, and quantity on hand. Create different types of products. Create an inventory class which keeps track of various products and can add up the inventory value.

- Create a binary format database containing the information, and the ability to save/load the database into the program.
- The program should auto-load the database that was saved from a previous session.
- The program should auto-save the database when the application closes.
- The program should generate an exception for certain error conditions.
- At a minimum, create 3 classes that have inheritance relationships, and 1 separate class for your main function, and 1 class for an exception (5 classes total at a minimum).
- The user should be able to add and delete entries in the system.

Thank you!

Explanation / Answer

Note:
   The given information about the use of database in not clear, whether it is to use Access?, SQL server? or textile?.

Solution:

//Inventoryprocess.java
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// class for inventory process
class Inventoryprocess
{
    private final Map<Integer,Product> Invstock;
   // constructor
    public Inventoryprocess() {
        this.Invstock = new HashMap<>();
    }
  
   // method to add product in inventory
    public boolean addInvProduct(int iid, InvMoney iprice, int iqty){
        if (this.Invstock.containsKey(iid)){
            return false;
        } else {
            this.Invstock.put(iid, new Product(iid, iprice, iqty));
            return true;
        }
    }
  
   // method to add the stock
    public boolean addInvStock(int iid, int ihowMany){
        if (!this.Invstock.containsKey(iid)){
            return false;
        }
      
        this.Invstock.get(iid).addInvStock(ihowMany);
        return true;
      
    }
  
   // method to reduce the stock
    public boolean reduceInvStock(int iid, int ihowMany){
        if (!this.Invstock.containsKey(iid)){
            return false;
        }
      
        return this.Invstock.get(iid).removeStock(ihowMany);
    }
  
   // method to remove the product
    public boolean removeInvProduct(int iid){
        if (!this.Invstock.containsKey(iid)){
            return false;
        }
        this.Invstock.remove(iid);
        return true;
    }
  
   // method to prin the product total value
    public void printTotalValueProduct(int iid){
        if (!this.Invstock.containsKey(iid)){
            System.out.println("No product found.");
        }
        else {
            System.out.println("Product pid: " + iid +
                    " Available: " + this.Invstock.get(iid).getQty() +
                    " Total value: " + this.Invstock.get(iid).getTotalValue());
        }    
    }
  
   // method to print the inventory total value
    public void printTotalValueInvStocks(){
        InvMoney Invtotal = new InvMoney(0, 0);
      
        for (Product aProduct: this.Invstock.values()){
            Invtotal = Invtotal.adding(aProduct.getTotalValue());
        }
      
        System.out.println("Total value of all stocks: " + Invtotal);
    }
}

// class for money
class InvMoney {

   // local variables
    private final int dlr;
    private final int cnts;

    // constructor
   public InvMoney(int dlr, int cnts) {
        this.dlr = dlr;
        this.cnts = cnts;
    }

   // method to return the dollar value
    public int dlr() {
        return dlr;
    }

   // method to return the cent value
    public int cnts() {
        return cnts;
    }

   // overide method to print the values
    @Override
    public String toString() {
        String zro = "";
        if (cnts < 10) {
            zro = "0";
        }

        return dlr + "." + zro + cnts;
    }

   // method to add the cost
    public InvMoney adding(InvMoney added){
        int euro_helper = 0;
        int cent_helper = 0;
        if ((this.cnts() + added.cnts()) > 100)
        {
            euro_helper = 1;
            cent_helper = (this.cnts() + added.cnts()) - 100;
        }
        else
            cent_helper = this.cnts() + added.cnts();
      
        return new InvMoney(this.dlr() + added.dlr() + euro_helper,
                cent_helper);
    }
  
   // method to compare the cost
    public boolean reduce(InvMoney compared) {
        if (this.dlr() < compared.dlr())
            return true;
        else if (this.dlr() > compared.dlr())
            return false;
        else
        {
            if (this.cnts() < compared.cnts())
                return true;
            else
                return false;
        }
    }
  
   // method to reduce the cost
    public InvMoney detect(InvMoney decremented) {
        int euro_helper = 0;
        int cent_helper = 0;
        if (this.reduce(decremented))
            return new InvMoney(0, 0);
        else
        {
            if (this.cnts() < decremented.cnts())
            {
                euro_helper = -1;
                cent_helper = (100 + this.cnts()) - decremented.cnts();
            }
            else
                cent_helper = this.cnts() - decremented.cnts();
          
            euro_helper += (this.dlr() - decremented.dlr());
            return new InvMoney(euro_helper, cent_helper);
        }
    }
  
   // method for multiplying the cost
    public InvMoney invmultiplyBy(int pqty){
        InvMoney Invtotal = new InvMoney(this.dlr, this.cnts);
      
        for (int i = 1; i<pqty; i++){
            Invtotal = Invtotal.adding(this);
        }
        return Invtotal;
    }
}

// class product
class Product {
  
   // local variables
    private final int pid;
    private InvMoney pprice;
    private int pqty;

   // constructor
    public Product(int pid, InvMoney pprice, int pqty) {
        this.pid = pid;
        this.pprice = pprice;
        this.pqty = pqty;
    }
  
   // method to add the stocks
    public void addInvStock(int howMany){
        this.pqty += howMany;
    }
  
   // method to remove the stocks
    public boolean removeStock(int howMany){      
        if (howMany > this.pqty){
            return false;
        }
        else{
            this.pqty -= howMany;
            return true;
        }
    }

   // method to return the product id
    public int getId() {
        return pid;
    }
  
   // method to get the total value
    public InvMoney getTotalValue(){
        return this.pprice.invmultiplyBy(this.pqty);
    }
  
   // method to update product price
    public void updatePrice(InvMoney newPrice){
        this.pprice = newPrice;
    }
  
   // method to get product quantity
    public int getQty() {
        return pqty;
    }
      
}


// class for custom exception
class customException extends Exception
{
    public customException()
   {
      System.out.println("Error occured!");
    }

}

// class to test the inventory process
public class TestMain {

   // main method
    public static void main(String[] args) {
   try
   {
       // variables and object to read input from user
        Scanner reader = new Scanner(System.in);
        Inventoryprocess Invstock = new Inventoryprocess();
        boolean quitFlag = true;
      
        String qId = "Enter product id: ";
        String qQty = "Enter product quantity: ";
        String qEuro = "Enter price in tens: ";
        String qCents = "Enter price in cents: ";
        String qMany = "How many: ";

        System.out.println("Welcome to inventory app v1.0");

       // get options from user
        while (quitFlag) {
            System.out.println("What do you want to do?");
            System.out.println("    1. add new product.");
            System.out.println("    2. add stock for a product.");
            System.out.println("    3. reduce stock for a product.");
            System.out.println("    4. remove a product from the inventory.");
            System.out.println("    5. calculate total value certain product stocks.");
            System.out.println("    6. calculate overall value of the inventory ");
            System.out.println("    q. quit the application.");

            System.out.print(">>");
            String res = reader.nextLine();

           // switch case to perfrom operation based on the option entered
            System.out.println();
            switch (res) {
                case "1":
                    int pid1 = onlyCheckInt(reader, qId);
                    int pqty1 = onlyCheckInt(reader, qQty);
                    System.out.println("Enter the pprice: ");
                    int euro1 = onlyCheckInt(reader, qEuro);
                    int cent1 = onlyCheckInt(reader, qCents);
                    Invstock.addInvProduct(pid1, new InvMoney(euro1, cent1), pqty1);
                    break;
              
                case "2":
                    int pid2 = onlyCheckInt(reader, qId);
                    int phowMany2 = onlyCheckInt(reader, qMany);
                    boolean test2 = Invstock.addInvStock(pid2, phowMany2);
                    checkIdIfOnStock(test2);
                    break;
                  
                case "3":
                    int pid3 = onlyCheckInt(reader, qId);
                    int phowMany3 = onlyCheckInt(reader, qMany);
                    boolean test3 = Invstock.reduceInvStock(pid3, phowMany3);
                    checkIdIfOnStock(test3);
                    break;
                  
                case "4":
                    int pid4 = onlyCheckInt(reader, qId);
                    boolean test4 = Invstock.removeInvProduct(pid4);
                    checkIdIfOnStock(test4);
                    break;
                  
                case "5":
                    int pid5 = onlyCheckInt(reader, qId);
                    Invstock.printTotalValueProduct(pid5);
                    break;
                  
                case "6":
                    Invstock.printTotalValueInvStocks();
                    break;
                  
                case "q":
                    quitFlag = false;
                    break;
                  
            }
        }
       throw new customException();
   }catch(customException cex)
   {
       System.out.println(cex);
   }

    }

   // method to check input
    public static int onlyCheckInt(Scanner scan, String q) {
        int pid;
        System.out.print(q);
        while (true) {
            try {
                pid = Integer.parseInt(scan.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Error: " + e.getMessage());
                System.out.print("Try again! " + q);
            }
        }
      
        return pid;
    }
  
   // methof to print if product not found
    public static void checkIdIfOnStock(boolean status){
        if (!status){
            System.out.println("No product found!");
        }
    }

}

Result:

Welcome to inventory app v1.0
What do you want to do?
    1. add new product.
    2. add stock for a product.
    3. reduce stock for a product.
    4. remove a product from the inventory.
    5. calculate total value certain product stocks.
    6. calculate overall value of the inventory
    q. quit the application.
>>1

Enter product id: 101
Enter product quantity: 12
Enter the pprice:
Enter price in tens: 123
Enter price in cents: 25
What do you want to do?
    1. add new product.
    2. add stock for a product.
    3. reduce stock for a product.
    4. remove a product from the inventory.
    5. calculate total value certain product stocks.
    6. calculate overall value of the inventory
    q. quit the application.
>>