Question: Create a top down design for the following code: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Inventory { //Declaring necessary variables private int itemID;

Create a top down design for the following code:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Inventory {

//Declaring necessary variables

private int itemID;

private int quantity;

private double price;

private String description;

private int[] itemIDArray;

private int[] quantityArray;

private double[] priceArray;

private String[] descriptionArray;

//Constructor

public Inventory() {

//Initializing each of the arrays

itemIDArray = new int[100];

quantityArray = new int[100];

priceArray = new double[100];

descriptionArray = new String[100];

}

//Method to read the inventory file

public void readFile() {

//Declaring the file to be read

File file = new File("inventory.txt");

try {

//Reading the file

Scanner scan = new Scanner(file);

//Declaring index for the arrays

int index = 0;

//Looping through the file

while (scan.hasNextLine()) {

//Reading each line of the file

String line = scan.nextLine();

//Splitting the line and storing each part into the respective variables

String[] split = line.split(",");

itemID = Integer.parseInt(split[0]);

quantity = Integer.parseInt(split[1]);

price = Double.parseDouble(split[2]);

description = split[3];

//Storing each part into the respective arrays

itemIDArray[index] = itemID;

quantityArray[index] = quantity;

priceArray[index] = price;

descriptionArray[index] = description;

//Incrementing the index

index++;

}

} catch (FileNotFoundException e) {

System.out.println("File not found");

}

}

//Method to add a new product

public void addProduct() {

//Declaring index for the arrays

int index = 0;

//Looping through the arrays

while (index < itemIDArray.length) {

//Checking if the array is empty

if (itemIDArray[index] == 0) {

//Asking the user for product information

Scanner scan = new Scanner(System.in);

System.out.print("Enter the Item ID: ");

itemID = scan.nextInt();

System.out.print("Enter the Quantity: ");

quantity = scan.nextInt();

System.out.print("Enter the Price: ");

price = scan.nextDouble();

System.out.print("Enter the Description: ");

description = scan.next();

//Storing the information into the respective arrays

itemIDArray[index] = itemID;

quantityArray[index] = quantity;

priceArray[index] = price;

descriptionArray[index] = description;

//Breaking out of the loop

break;

}

index++;

}

}

//Method to delete a product

public void deleteProduct() {

//Declaring variables

int itemID;

int index = 0;

boolean found = false;

//Asking the user for the item ID of the product to be deleted

Scanner scan = new Scanner(System.in);

System.out.print("Enter the Item ID of the product to be deleted: ");

itemID = scan.nextInt();

//Looping through the array

while (index < itemIDArray.length) {

//Checking for the item ID

if (itemIDArray[index] == itemID) {

//Setting the value of the respective arrays to 0

itemIDArray[index] = 0;

quantityArray[index] = 0;

priceArray[index] = 0;

descriptionArray[index] = "";

//Setting the found flag to true

found = true;

}

index++;

}

//Checking if the item was found

if (found) {

System.out.println("Product deleted successfully!");

} else {

System.out.println("Product not found!");

}

}

//Method to restock a product

public void restockProduct() {

//Declaring variables

int itemID;

int quantity;

int index = 0;

boolean found = false;

//Asking the user for the product and quantity to be restocked

Scanner scan = new Scanner(System.in);

System.out.print("Enter the Item ID of the product to be restocked: ");

itemID = scan.nextInt();

System.out.print("Enter the Quantity to be restocked: ");

quantity = scan.nextInt();

//Looping through the array

while (index < itemIDArray.length) {

//Checking for the item ID

if (itemIDArray[index] == itemID) {

//Adding the quantity to the existing quantity

quantityArray[index] += quantity;

//Setting the found flag to true

found = true;

}

index++;

}

//Checking if the item was found

if (found) {

System.out.println("Product restocked successfully!");

} else {

System.out.println("Product not found!");

}

}

//Method to reduce quantity of a product

public void reduceProduct() {

//Declaring variables

int itemID;

int quantity;

int index = 0;

boolean found = false;

//Asking the user for the product and quantity to be reduced

Scanner scan = new Scanner(System.in);

System.out.print("Enter the Item ID of the product to be reduced: ");

itemID = scan.nextInt();

System.out.print("Enter the Quantity to be reduced: ");

quantity = scan.nextInt();

//Looping through the array

while (index < itemIDArray.length) {

//Checking for the item ID

if (itemIDArray[index] == itemID) {

//Checking if the quantity is less than the existing quantity

if (quantity < quantityArray[index]) {

//Subtracting the quantity from the existing quantity

quantityArray[index] -= quantity;

//Setting the found flag to true

found = true;

} else {

System.out.println("Quantity entered is more than the existing quantity!");

}

}

index++;

}

//Checking if the item was found

if (found) {

System.out.println("Product reduced successfully!");

} else {

System.out.println("Product not found!");

}

}

//Method to generate a bill

public void generateBill() {

//Declaring variables

int itemID;

int quantity;

int index = 0;

boolean found = false;

//Asking the user for the product and quantity to be reduced

Scanner scan = new Scanner(System.in);

System.out.print("Enter the Item ID of the product to be purchased: ");

itemID = scan.nextInt();

System.out.print("Enter the Quantity to be purchased: ");

quantity = scan.nextInt();

//Looping through the array

while (index < itemIDArray.length) {

//Checking for the item ID

if (itemIDArray[index] == itemID) {

//Checking if the quantity is less than the existing quantity

if (quantity <= quantityArray[index]) {

//Subtracting the quantity from the existing quantity

quantityArray[index] -= quantity;

//Setting the found flag to true

found = true;

} else {

System.out.println("Quantity entered is more than the existing quantity!");

}

}

index++;

}

//Checking if the item was found

if (found) {

//Declaring variables

double totalPrice = 0;

//Printing the customer information

System.out.println("Customer Information:");

System.out.println("---------------------");

System.out.println("Name: John Doe");

System.out.println("Address: 1 Main Street, Anytown, USA");

//Printing the product information

System.out.println(" Product Information: ");

System.out.println("---------------------");

System.out.println("Item ID: " + itemID);

System.out.println("Quantity: " + quantity);

System.out.println("Price: " + priceArray[index - 1]);

System.out.println("Description: " + descriptionArray[index - 1]);

//Calculating the total price

totalPrice = quantity * priceArray[index - 1];

//Printing the total price

System.out.println(" Total Price: " + totalPrice);

} else {

System.out.println("Product not found!");

}

}

//Method to print the inventory

public void printInventory() {

//Declaring index for the arrays

int index = 0;

//Printing the inventory

System.out.println("Inventory: ");

System.out.println("----------");

while (index < itemIDArray.length) {

//Checking if the array is empty

if (itemIDArray[index] != 0) {

//Printing the inventory

System.out.println("Item ID: " + itemIDArray[index]);

System.out.println("Quantity: " + quantityArray[index]);

System.out.println("Price: " + priceArray[index]);

System.out.println("Description: " + descriptionArray[index]);

System.out.println("----------");

}

index++;

}

}

//Main method

public static void main(String[] args) {

//Declaring the object

Inventory inventory = new Inventory();

//Reading the file

inventory.readFile();

//Declaring variables

int choice;

//Looping until the user wants to quit

while (true) {

//Printing the menu

System.out.println("1. Add product");

System.out.println("2. Delete product");

System.out.println("3. Restock product");

System.out.println("4. Reduce product quantity");

System.out.println("5. Generate bill");

System.out.println("6. Print inventory");

System.out.println("7. Quit");

System.out.print("Enter your choice: ");

//Reading the user's choice

Scanner scan = new Scanner(System.in);

choice = scan.nextInt();

//Checking the user's choice

switch (choice) {

case 1:

//Adding a product

inventory.addProduct();

break;

case 2:

//Deleting a product

inventory.deleteProduct();

break;

case 3:

//Restocking a product

inventory.restockProduct();

break;

case 4:

//Reducing the quantity of a product

inventory.reduceProduct();

break;

case 5:

//Generating a bill

inventory.generateBill();

break;

case 6:

//Printing the inventory

inventory.printInventory();

break;

case 7:

//Exiting the program

System.out.println("Goodbye!");

System.exit(0);

break;

default:

//Invalid choice

System.out.println("Invalid choice!");

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!