In: Computer Science
Code in Java
1. Create a class Flower with data: Name, Price, Color and properly methods.
2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower:
Write a program to use ListFlower as above menu.
Make your own main program to test all above methods.
I have implemented "Flower" class and "ListFlower" Class which will add, delete , display, search and sort the flowers by price in descending order.
Here, ListFlower class is work as a singular linkedlist.
Flower Class:-
// This class represents Flower
class Flower{
// store flower name
String name;
// store flower price
double price;
// store flower color
String color;
// store next flower
Flower next;
// initialize constructor for the flower data members
public Flower(String name, double price, String color) {
this.name = name;
this.price = price;
this.color = color;
this.next = null;
}
/*
getter and setter methods
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Flower getNext() {
return next;
}
public void setNext(Flower next) {
this.next = next;
}
}
ListFlower Class:-
class ListFlower{
// store fist flower in the linkedlist of flower
Flower head;
/**
* Add flower to the flower list
* @param f to be added
*/
public void add(Flower f){
// check whether list is empty or not?
if(head == null){
// make flower as head
head = f;
return;
}
// now add the flower at the end of the flower list
Flower flower = head;
// reach to the last flower
while(flower.next != null){
// goto the next flower
flower = flower.getNext();
}
// store new flower to the next of last flower
flower.setNext(f);
}
/**
* This method prints all the flowers from the flower list.
*/
public void display(){
// check whether list is empty or not?
if(head == null){
System.out.println("No Flower in the list!");
return;
}
// store head
Flower curr = head;
while(curr != null){
System.out.println("\nName : "+curr.getName());
System.out.println("Price : "+curr.getPrice());
System.out.println("Color : "+curr.getColor());
// goto the next flower
// if we print the last flower then dont print next flower line
if(curr.next != null)
System.out.println("\n---------Next Flower---------");
curr = curr.getNext();
}
}
/**
* This method sorts the flowers in descending order by pice
*/
public void sort(){
// if list is empty
if(head == null){
System.out.println("No FLower in the list!");
return;
}
// sort the list using selection sort in descending oreder based on flower price
for(Flower first = head; first != null; first = first.getNext()){
for(Flower second = first.getNext(); second != null; second = second.getNext()){
// compare flowers price
if(first.getPrice() < second.getPrice()){
// swap the flowers name
String tempName = first.getName();
first.setName(second.getName());
second.setName(tempName);
// swap the flower price
double tempPrice = first.getPrice();
first.setPrice(second.getPrice());
second.setPrice(tempPrice);
// swap the flowers color
String tempColor = first.getColor();
first.setColor(second.getColor());
second.setColor(tempColor);
}
}
}
System.out.println("Flowers sorted in descending order based on price");
}
/**
* This method search flower in the flower list
* @param f to be searched
* @return true if flower(f) is exist
*/
public boolean search(Flower f){
// if list is empty then retur false
if(head == null){
return false;
}
// store head
Flower curr = head;
// compare each flower equal or not?
while(curr != null){
// compare each flower value
if(curr.getName().equals(f.getName()) && curr.getPrice() == f.getPrice() && curr.getColor().equals(f.getColor())){
// here flowers data is equal then return true;
return true;
}
// goto the next flower
curr = curr.getNext();
}
// otherwise return flase
return false;
}
/**
* This method deleter flower from the flower list
* @param pos of flower which is deleted
*/
public void delete(int pos){
// check whether list is empty or not?
if(head == null){
System.out.println("No flower in the list!");
return;
}
/*
Case 1: if position is one then remove first flower
from the list
*/
if(pos == 1){
// store next of head into the head
head = head.getNext();
System.out.println(pos+" Flower is deleted");
return;
}
// store head
Flower curr = head;
// store prev flower for deletion purpose
Flower prev = curr;
int i = 1;
// searh position
while(curr != null){
// if position is match then remove curr flower from that positiion
if(i == pos){
// now store the next of curr to the next of prev
prev.setNext(curr.getNext());
System.out.println(pos+" Flower is deleted");
return;
}
// incerement i
i++;
// store prev of curr
prev = curr;
// goto the next of flower
curr = curr.getNext();
}
System.out.println("No flower is found in "+pos+" postion");
}
}
In ListFlower class, delete(int pos) method remove the flower from the list and here, postion value is start from the 1 for the flower linkedlist.
I have attached a java program which will provide menu to the user by which user can add, delete, sort, search and display the flowers from the flower list and program will be ended when the user enter "6" for the exit.
TestFlowerList.java file:-
import java.util.Scanner;
// This class represents Flower
class Flower{
// store flower name
String name;
// store flower price
double price;
// store flower color
String color;
// store next flower
Flower next;
// initialize constructor for the flower data members
public Flower(String name, double price, String color) {
this.name = name;
this.price = price;
this.color = color;
this.next = null;
}
/*
getter and setter methods
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Flower getNext() {
return next;
}
public void setNext(Flower next) {
this.next = next;
}
}
/*
This class represents flower list
using singular linked list
*/
class ListFlower{
// store fist flower in the linkedlist of flower
Flower head;
/**
* Add flower to the flower list
* @param f to be added
*/
public void add(Flower f){
// check whether list is empty or not?
if(head == null){
// make flower as head
head = f;
return;
}
// now add the flower at the end of the flower list
Flower flower = head;
// reach to the last flower
while(flower.next != null){
// goto the next flower
flower = flower.getNext();
}
// store new flower to the next of last flower
flower.setNext(f);
}
/**
* This method prints all the flowers from the flower list.
*/
public void display(){
// check whether list is empty or not?
if(head == null){
System.out.println("No Flower in the list!");
return;
}
// store head
Flower curr = head;
while(curr != null){
System.out.println("\nName : "+curr.getName());
System.out.println("Price : "+curr.getPrice());
System.out.println("Color : "+curr.getColor());
// goto the next flower
// if we print the last flower then dont print next flower line
if(curr.next != null)
System.out.println("\n---------Next Flower---------");
curr = curr.getNext();
}
}
/**
* This method sorts the flowers in descending order by pice
*/
public void sort(){
// if list is empty
if(head == null){
System.out.println("No FLower in the list!");
return;
}
// sort the list using selection sort in descending oreder based on flower price
for(Flower first = head; first != null; first = first.getNext()){
for(Flower second = first.getNext(); second != null; second = second.getNext()){
// compare flowers price
if(first.getPrice() < second.getPrice()){
// swap the flowers name
String tempName = first.getName();
first.setName(second.getName());
second.setName(tempName);
// swap the flower price
double tempPrice = first.getPrice();
first.setPrice(second.getPrice());
second.setPrice(tempPrice);
// swap the flowers color
String tempColor = first.getColor();
first.setColor(second.getColor());
second.setColor(tempColor);
}
}
}
System.out.println("Flowers sorted in descending order based on price");
}
/**
* This method search flower in the flower list
* @param f to be searched
* @return true if flower(f) is exist
*/
public boolean search(Flower f){
// if list is empty then retur false
if(head == null){
return false;
}
// store head
Flower curr = head;
// compare each flower equal or not?
while(curr != null){
// compare each flower value
if(curr.getName().equals(f.getName()) && curr.getPrice() == f.getPrice() && curr.getColor().equals(f.getColor())){
// here flowers data is equal then return true;
return true;
}
// goto the next flower
curr = curr.getNext();
}
// otherwise return flase
return false;
}
/**
* This method deleter flower from the flower list
* @param pos of flower which is deleted
*/
public void delete(int pos){
// check whether list is empty or not?
if(head == null){
System.out.println("No flower in the list!");
return;
}
/*
Case 1: if position is one then remove first flower
from the list
*/
if(pos == 1){
// store next of head into the head
head = head.getNext();
System.out.println(pos+" Flower is deleted");
return;
}
// store head
Flower curr = head;
// store prev flower for deletion purpose
Flower prev = curr;
int i = 1;
// searh position
while(curr != null){
// if position is match then remove curr flower from that positiion
if(i == pos){
// now store the next of curr to the next of prev
prev.setNext(curr.getNext());
System.out.println(pos+" Flower is deleted");
return;
}
// incerement i
i++;
// store prev of curr
prev = curr;
// goto the next of flower
curr = curr.getNext();
}
System.out.println("No flower is found in "+pos+" postion");
}
}
/*
This class provide menu for the user by which
user can perforem operations on the ListFlower class
*/
public class TestFlowerList {
public static void main(String[] args) {
// create an object of ListFlower class
ListFlower listFlower = new ListFlower();
// create an object of scanner class for user input
Scanner sc = new Scanner(System.in);
int ch = 0;
// create menu for the flower list
do{
System.out.println("\n1. Add flower");
System.out.println("2. Display all flowers");
System.out.println("3. Sort flower list by price in descending order");
System.out.println("4. Search flower");
System.out.println("5. Delete flower");
System.out.println("6. Exit");
// get the choice from the user
System.out.print("Enter Choice ::");
ch = sc.nextInt();
System.out.println();
sc.nextLine();
switch(ch){
case 1:
// get the flower name from the user
System.out.print("Enter flower name::");
String name = sc.nextLine();
// get the flower price from the user
System.out.print("Enter flower price::");
double price = sc.nextDouble();
sc.nextLine();
// get the flower color from the user
System.out.print("Enter flower color::");
String color = sc.nextLine();
// create flower using flower class object
Flower newFlower = new Flower(name, price, color);
// add the flower by calling add() method
listFlower.add(newFlower);
break;
case 2:
// call display() method for diplaying flowers
listFlower.display();
break;
case 3:
// sort the flower list by price using sort() method in descending order
listFlower.sort();
break;
case 4:
// get the flower name from the user
System.out.print("Enter searched flower name::");
String searchedName = sc.nextLine();
// get the flower price from the user
System.out.print("Enter searched flower price::");
double searchedPrice = sc.nextDouble();
sc.nextLine();
// get the flower color from the user
System.out.print("Enter searched flower color::");
String searchedColor = sc.nextLine();
// create searched object of flower class
Flower searchedFlower = new Flower(searchedName, searchedPrice, searchedColor);
// call the search() method for searched flower
if(listFlower.search(searchedFlower)){
System.out.println("Searched flower is in the flower list");
}else{
System.out.println("Searched flower is not in the flower list");
}
break;
case 5:
// get the deleted flower position from the user
System.out.print("Enter positio for deleteion ::");
int pos = sc.nextInt();
// call the delete() method
listFlower.delete(pos);
break;
case 6:
System.out.println("---------------Exit---------------");
break;
default:
System.out.println("Invalid choice");
}
}while(ch != 6);
}
}
Ouput:-
1> Add flowers detail when user choice "1".
2> Display flowers when user choice "2".
3> Sort flowers when user choice "3".
4> Search flower when user choice "4".
5> delete flower when user choice "5".
6> End of the program when user choose "6".
I hope you will understand the above program.
Do you feel needful and useful then please upvote me.
Thank you.