In: Computer Science
Phone.java
public class Phone {
private String brand;
private double size;
private double price;
public Phone()
{
this.brand = "";
this.size = 0.0;
this.price = 0.0;
}
public Phone(String brand, double size, double price)
{
this.brand = brand;
this.size = size;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString()
{
return("Brand: " + this.brand +
", Size: " + String.format("%.1f", this.size) +
", Price: $" + String.format("%.2f", this.price));
}
}
PhoneListMain.java (Driver class)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class PhoneListMain {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the filename: ");
String fileName = sc.nextLine().trim();
// read from file and put the data into a list of Phones
ArrayList<Phone> phones = readData(fileName);
int choice;
do
{
displayMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
System.out.println("***** ALL ITEMS IN THE CSV FILE *****\n"
+ "-------------------------------------");
displayAllItems(phones);
break;
}
case 2:
{
System.out.print("\nEnter a phone brand to search: ");
String brand = sc.nextLine().trim();
linearSearch(phones, brand);
break;
}
case 3:
{
System.out.print("\nEnter a phone brand to search: ");
String brand = sc.nextLine().trim();
binarySearch(phones, brand);
break;
}
case 4:
{
System.out.println("\nGood Bye!\n");
System.exit(0);
}
default:
System.out.println("\nInvalid selection!\n");
}
}while(choice != 4);
}
private static void displayMenu()
{
System.out.print("Choose one of the options from below:\n"
+ "1. Display all items in CSV file\n"
+ "2. Pick an item by linear search\n"
+ "3. Pick an item by binary search\n"
+ "4. Quit\n"
+ "Enter your choice: ");
}
private static ArrayList<Phone> readData(String
fileName)
{
ArrayList<Phone> phones = new ArrayList<>();
Scanner fileReader;
int count = 0;
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine().trim();
String[] data = line.split(",");
String brand = data[0];
double size = Double.parseDouble(data[1]);
double price = Double.parseDouble(data[2]);
phones.add(new Phone(brand, size, price));
// track number of lines read from file
count++;
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Couldn't find file: " + fileName +
"!\n");
System.exit(0);
}
System.out.println("Successfully read " + count + " phone data from
file.\n");
return phones;
}
private static void displayAllItems(ArrayList<Phone>
phones)
{
for(Phone phone : phones)
{
System.out.println(phone.toString());
}
System.out.println();
}
private static void linearSearch(ArrayList<Phone> phones,
String brand)
{
int index = -1;
for(int i = 0; i < phones.size(); i++)
{
if(phones.get(i).getBrand().equals(brand))
{
index = i;
break;
}
}
if(index == -1)
System.out.println("\nSorry, no phones found for brand: " + brand +
".\n");
else
System.out.println("\nMatch found:\n" +
phones.get(index).toString() + "\n");
}
private static void binarySearch(ArrayList<Phone> phones,
String brand)
{
// variable to store the index of the found element
int index = -1;
// sort the list and then implement binary search
// here, bubble sorting method is used, any sorting technique can
be used
// Copy the original list to another list so that the original list
remains unchanged
ArrayList<Phone> phonesDummy = phones;
int len = phonesDummy.size();
for(int i = 0; i < len - 1; i++)
{
for(int j = 0; j < len - i - 1; j++)
{
if(phonesDummy.get(j).getBrand().compareToIgnoreCase(phonesDummy.get(j
+ 1).getBrand()) > 0)
{
// swap the elements
Phone temp = phonesDummy.get(j);
phonesDummy.set(j, phonesDummy.get(j + 1));
phonesDummy.set(j + 1, temp);
}
}
}
// now implement the binary search
int leftHalf = 0, rightHalf = len - 1;
while(leftHalf <= rightHalf)
{
int mid = leftHalf + (rightHalf - leftHalf) / 2;
int res = brand.compareTo(phonesDummy.get(mid).getBrand());
// element found at the mid index
if(res == 0)
index = mid;
// if brand name is greater, it must be present at the right
half
// so, the left half is ignored
if(res > 0)
leftHalf = mid + 1;
// if brand name is smaller, it must be present at the left
half
// so, the right half is ignored
else
rightHalf = mid - 1;
}
if(index == -1)
System.out.println("\nSorry, no phones found for brand: " + brand +
".\n");
else
System.out.println("\nMatch found:\n" +
phones.get(index).toString() + "\n");
}
}
******************************************************************* SCREENSHOT ********************************************************
**********************************************************************************************************************************************