0% found this document useful (0 votes)
44 views5 pages

AssignmentOnTreeMap Darshan 2YX

The document details a shopping cart program that allows a user to view available items, purchase items, add new items, and view an invoice. The program uses classes like Item, ItemPurchased, and ItemService to store item details, add items to a cart, and display menus for user input.

Uploaded by

Darshan N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

AssignmentOnTreeMap Darshan 2YX

The document details a shopping cart program that allows a user to view available items, purchase items, add new items, and view an invoice. The program uses classes like Item, ItemPurchased, and ItemService to store item details, add items to a cart, and display menus for user input.

Uploaded by

Darshan N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Output

<=============================================>
1->Purchase Item
2->Display item Available
3->Add Item
4->CheckOut

Enter your choice..


2
1 => Item Id: 1 =>Item Name: Rice =>Item price: 50.0
2 => Item Id: 2 =>Item Name: Oil =>Item price: 157.0
3 => Item Id: 3 =>Item Name: Fish =>Item price: 120.0
4 => Item Id: 4 =>Item Name: Eggs =>Item price: 5.4
5 => Item Id: 5 =>Item Name: IceCream =>Item price: 35.0
1->Purchase Item
2->Display item Available
3->Add Item
4->CheckOut

Enter your choice..


1
Enter Item Id
1
Enter quantity
20
1->Purchase Item
2->Display item Available
3->Add Item
4->CheckOut

Enter your choice..


1
Enter Item Id
5
Enter quantity
10
1->Purchase Item
2->Display item Available
3->Add Item
4->CheckOut

Enter your choice..


1
Enter Item Id
2
Enter quantity
3
1->Purchase Item
2->Display item Available
3->Add Item
4->CheckOut

Enter your choice..


1
Enter Item Id
3
Enter quantity
2
1->Purchase Item
2->Display item Available
3->Add Item
4->CheckOut

Enter your choice..


1
Enter Item Id
23
Item not found
The available item are
1 => Item Id: 1 =>Item Name: Rice =>Item price: 50.0
2 => Item Id: 2 =>Item Name: Oil =>Item price: 157.0
3 => Item Id: 3 =>Item Name: Fish =>Item price: 120.0
4 => Item Id: 4 =>Item Name: Eggs =>Item price: 5.4
5 => Item Id: 5 =>Item Name: IceCream =>Item price: 35.0
1->Purchase Item
2->Display item Available
3->Add Item
4->CheckOut

Enter your choice..


4
Your Invoice
------------------------------------------------------
ID NAME QTY PRICE
1 Rice 20 1000.000000
5 IceCream 10 350.000000
2 Oil 3 471.000000
3 Fish 2 240.000000

Total Items Purchased-:4


Total price-----------:2061.0
GST 15%---------------:309.15
The total payableBill-:2370.15
------------------------------------------------------
Thank you for shopping with us, have a good day
<=======================================================================>

<========================Programs=======================================>

Item.java
===================================================
package com.intellect.training.maps;
public class Item {
private int id;
private String name;
private float uprice;
public Item(int id, String name, float uprice) {
this.id = id;
this.name = name;
this.uprice = uprice;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getUprice() {
return uprice;
}
public void setUprice(float uprice) {
this.uprice = uprice;
}

public String toString(){


return("Item Id: "+this.getId()+" =>"+"Item Name: "+this.getName()+"
=>"+"Item price: "+this.getUprice());
}
}
====================================================================
ItemPurchased.java
====================================================================
package com.intellect.training.maps;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class ItemPurchased{
private static float totalPrice=0;
TreeMap<String,Item> tmap=new TreeMap<>();
Item item;
int arrId[]=new int[10];
String arrName[]= new String[10];
int arrQty[]=new int[10];
float arrPrice[]=new float[10];
static int count=0;
static Scanner sc=new Scanner(System.in);

public void addDetails(){


System.out.println("Enter id");
int id=sc.nextInt();
System.out.println("Enter Name");
String name=sc.next();
System.out.println("Enter Price");
float uprice=sc.nextFloat();
String key=String.valueOf(id);
item=new Item(id, name, uprice);
tmap.put(key, item);
}
public void displayItems(){
Set<Map.Entry<String,Item>> data=tmap.entrySet();
for( Map.Entry<String, Item> values:data){
System.out.println(values.getKey()+" => "+values.getValue());
}
}
public void purchaseItem(){

System.out.println("Enter Item Id");


int id=sc.nextInt();
String key=String.valueOf(id);
boolean status=tmap.containsKey(key);
if(status==true){
String keydetails=tmap.ceilingKey(key);
System.out.println("Enter quantity");
int qty=sc.nextInt();
float uprice=tmap.get(keydetails).getUprice();
String name=tmap.get(keydetails).getName();
totalPrice=totalPrice+uprice*qty;
arrId[count]=id;
arrName[count]=name;
arrQty[count]=qty;
arrPrice[count]=(uprice*qty);
count++;

}
else{
System.out.println("Item not found");
System.out.println("The available item are");
displayItems();
}
}
public void totalPrice(){
System.out.println("Your Invoice");
System.out.println("------------------------------------------------------");
System.out.println("ID NAME QTY PRICE");
for(int i=0;i<count;i++){
System.out.printf("%-4d %-7s %-6d %-
5f",arrId[i],arrName[i],arrQty[i],arrPrice[i]);
System.out.println();
}
System.out.println();
System.out.println("Total Items Purchased-:"+(count+1));
System.out.println("Total price-----------:"+totalPrice);
System.out.println("GST 15%---------------:"+totalPrice*0.15);
System.out.println("The total payableBill-:"+((totalPrice*0.15)+totalPrice));
System.out.println("------------------------------------------------------");
System.out.println("Thank you for shopping with us, have a good day");
}
}
===================================================================================
====================
ItemService.java
===================================================================================
====================
package com.intellect.training.maps;
public class ItemService {
public static void main(String[] args) {
int choice;
ItemPurchased itempurchase=new ItemPurchased();
do {
System.out.println("1->Purchase Item\n2->Display item Available\n3->Add
Item\n4->CheckOut\n");
System.out.println("Enter your choice.. ");
choice = ItemPurchased.sc.nextInt();
switch(choice) {
case 1:itempurchase.purchaseItem();
break;
case 2: itempurchase.displayItems();
break;
case 3: itempurchase.addDetails();
break;
case 4: itempurchase.totalPrice();
break;
default:System.out.println("Invalid Choice try again...");
};
}while(choice!=4);
}
}
===========================================END=====================================
======================

You might also like