I need the following method to add the product to the array if the product is not already present. The array is sorted by id number: I have tried a few different methods for this, including trying to use a binary search to find the insertion point and am just stuck. insert(int id, String name, double price){ }  Then the following method to display a receipt that looks like this: receipt(int[] id) The displayed receipt should look like this: item 1: price item 2: price item 3: price total cost: total items: I can't figure out how to display all of that without using print statements. The test driver prints out the receipt method so this method only needs to display the information.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need the following method to add the product to the array if the product is not already present. The array is sorted by id number:
I have tried a few different methods for this, including trying to use a binary search to find the insertion point and am just stuck.

insert(int id, String name, double price){


Then the following method to display a receipt that looks like this:
receipt(int[] id)
The displayed receipt should look like this:
item 1: price
item 2: price
item 3: price
total cost:
total items:

I can't figure out how to display all of that without using print statements. The test driver prints out the receipt method so this method only needs to display the information.

import java.util.Scanner;
public class StoreProduct {
private int id;
private String name;
private double price;
public StoreProduct(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
%3D
public double getld(){
return id;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public String toString() {
return id + "\t" + name + "\t" + price;
}
}
Transcribed Image Text:import java.util.Scanner; public class StoreProduct { private int id; private String name; private double price; public StoreProduct(int id, String name, double price) { this.id = id; this.name = name; this.price = price; } %3D public double getld(){ return id; } public String getName(){ return name; } public double getPrice(){ return price; } public String toString() { return id + "\t" + name + "\t" + price; } }
Public class Store {
Private int num;
Private StoreProduct] product;
Public Store() {
Num=0;
Products = new StoreProduct[4];
}
Public void insert(int id, String name, double price){
}
public String toString() {
StringBuilder res = new StringBuilder();
res.append(num).append("/").append(products.length);
res.append("\n");
for (int į = 0; į< num; ¡++) {
res.append(products[i].toString());
res.append("\n");
}
%3D
return res.toString();
}
}
Transcribed Image Text:Public class Store { Private int num; Private StoreProduct] product; Public Store() { Num=0; Products = new StoreProduct[4]; } Public void insert(int id, String name, double price){ } public String toString() { StringBuilder res = new StringBuilder(); res.append(num).append("/").append(products.length); res.append("\n"); for (int į = 0; į< num; ¡++) { res.append(products[i].toString()); res.append("\n"); } %3D return res.toString(); } }
Expert Solution
Step 1

The program is in java

import java.util.*;
class StoreProduct implements Comparable<StoreProduct>{
  private int id;
  private String name;
  private double price;
  public StoreProduct(int id,String name,double price){
    this.id=id;
    this.name=name;
    this.price=price;

  }
  public int getid()
  {
    return id;
  }
  public String getname()
  {
    return name;
  }
  public double getprice()
  {
    return price;
  }
  public String toString()
  {
    return id+"\t"+name+"\t"+price;
  }
  public int compareTo(StoreProduct p)
  {
    return this.id - p.getid();
  }
}
class Store{
  private int num;
  private StoreProduct[] product;
  public Store()
  {
    num=0;
    product =new StoreProduct[4];
  }
public void insert(int id,String name,double price) //insert item
{
  int flag=0;
  for(int j=0;j<num;j++)
  {
  if(product[j].getid()==id)
  {
    flag=1;
  }
  }
  if(flag!=1)
  {
  product[num]=new StoreProduct(id,name,price);
  num++;
  }
  
}
public void sortitems()  // sort items
{
  Arrays.sort(product,0,num);
}
public void receipt(int[] a)// receipt function
{
  double totalcost=0;
  int n=0;
for(int i=0;i<a.length;i++)
{
  for(int j=0;j<num;j++)
  {
    if(a[i]==product[j].getid())
    {
      System.out.println(product[j].getname()+" : "+product[j].getprice());
      totalcost+=product[j].getprice();
      n++;
          }
  }
}
  System.out.println("Total cost: "+totalcost);
  System.out.println("Total item: "+n);
}
public String toString(){
  StringBuilder res=new StringBuilder();
  res.append(num).append("/").append(product.length);
  res.append("\n");
  for(int i=0;i<num;i++)
  {
    res.append(product[i].toString());
    res.append("\n");
  }
  return res.toString();
}
}
public class Main{
  public static void main(String args[]){
Store f=new Store();
f.insert(1,"Apple",30);
f.insert(3,"Orange",34.5);
f.insert(3,"pineapple",34.5);
f.insert(2,"Grapes",20);
f.insert(4,"Banana",10);
System.out.println("Items before sorting");
System.out.println(f.toString());
f.sortitems();                           //sort items in Store
System.out.println("Items after sorting");
System.out.println(f.toString());
int a[]={2,4,5};
f.receipt(a);    // print receipt for items in a
  }
}

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education