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

Write a program that manipulates a database of product records. Records are stor

ID: 3624833 • Letter: W

Question

Write a program that manipulates a database of product records. Records are stored in a binary file. Each record consists of these items:

Product name: 30 characters at two bytes each = 60 bytes
Price: one double = 8 bytes
Quantity: one int = 8 bytes
The program should allow the user to add a record, find a record that matches a product name, and change the price and quantity of a product by a given amount.

Here is a sample program run:

A)dd record F)ind record Change P)rice Change Q)uantity E)xit: A
Name: Blackwell Toaster
Price: 19.95
Quantity: 50
A)dd record F)ind record Change P)rice Change Q)uantity E)xit: A
Name: ZapXpress Microwave
Price: 79.95
Quantity: 20
A)dd record F)ind record Change P)rice Change Q)uantity E)xit: F
Name: Blackwell Toaster
Price: 19.95
Quantity: 50
A)dd record F)ind record Change P)rice Change Q)uantity E)xit: P
New price: 21.95
A)dd record F)ind record Change P)rice Change Q)uantity E)xit: Q
New quantity: 20
A)dd record F)ind record Change P)rice Change Q)uantity E)xit: F
Name: Blackwell Toaster
Price: 21.95
Quantity: 20
A)dd record F)ind record Change P)rice Change Q)uantity E)xit: E
Use the following class in your solution:

public class Product
{
/**
Constructs a product with empty name and 0 price and
quantity.
*/
public Product()
{
name = "";
price = 0;
quantity = 0;
}

/**
Constructs a product with the given name, price and
quantity.
@param aName product name
@param aPrice product price
@param aQuantity product quantity
*/
public Product(String aName, double aPrice, int aQuantity)
{
name = aName;
price = aPrice;
quantity = aQuantity;
}

/**
Returns the product name.
@return the product name
*/
public String getName()
{
return name;
}

/**
Returns the product price.
@return the product price
*/
public double getPrice()
{
return price;
}

/**
Returns the product quantity.
@return the product quantity
*/

public int getQuantity()
{
return quantity;
}

/**
Sets the product price.
@param newPrice the new product price
*/
public void setPrice(double newPrice)
{
price = newPrice;
}

/**
Sets the product quantity.
@param newQuantity the new product quantity
*/
public void setQuantity(int newQuantity)
{
quantity = newQuantity;
}

private String name;
private double price;
private int quantity;
}

Explanation / Answer


import java.io.*;
class Product
{
/**
Constructs a product with empty name and 0 price and
quantity.
*/
public Product()
{
name = "";
price = 0;
quantity = 0;
}

/**
Constructs a product with the given name, price and
quantity.
@param aName product name
@param aPrice product price
@param aQuantity product quantity
*/
public Product(String aName, double aPrice, int aQuantity)
{
name = aName;
price = aPrice;
quantity = aQuantity;
}

/**
Returns the product name.
@return the product name
*/
public String getName()
{
return name;
}

/**
Returns the product price.
@return the product price
*/
public double getPrice()
{
return price;
}

/**
Returns the product quantity.
@return the product quantity
*/

public int getQuantity()
{
return quantity;
}

/**
Sets the product price.
@param newPrice the new product price
*/
public void setPrice(double newPrice)
{
price = newPrice;
}

/**
Sets the product quantity.
@param newQuantity the new product quantity
*/
public void setQuantity(int newQuantity)
{
quantity = newQuantity;
}

private String name;
private double price;
private int quantity;
}

public class ProductDemo {

   public static DataInputStream dis;
    public static void main(String[] args) throws Exception {

        dis=new DataInputStream(System.in);
        do
        {
        System.out.print("A)dd record F)ind record Change P)rice Change Q)uantity E)xit:");
        char ch=(dis.readLine()).charAt(0);
        switch(ch)
        {
            case 'A':   Add(); break;
            case 'F': Find(); break;
            case 'E':System.exit(0);
            default:System.out.println("Invalid choice");
        }
        }while(true);
       
    }

    public static void Add() throws Exception
    {
       
           System.out.print("Name: ");
           String n=dis.readLine();
           System.out.print("Price: ");
           double p=Double.parseDouble(dis.readLine());
           System.out.print("Quantity: ");
           int q=Integer.parseInt(dis.readLine());
           Product prod=new Product(n,p,q);
           PrintWriter pw=new PrintWriter(new FileOutputStream("Product.txt",true));
           pw.println(prod.getName());
           pw.println(""+prod.getPrice());
           pw.println(""+prod.getQuantity());
           pw.close();

    }

    public static void Find() throws Exception
    {
         System.out.print("Name: ");
           String n=dis.readLine();
           BufferedReader br=new BufferedReader(new FileReader("Product.txt"));
          
           String line;
           while((line=br.readLine())!=null)
           {
               if(line.equals(n))
               {
                   System.out.println("Price: "+br.readLine());
                   System.out.println("Quantity: "+br.readLine());
                   break;
               }
           }
           br.close();
    }
}

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