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.
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.
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
}
}
Step by step
Solved in 2 steps with 1 images