Question

In: Computer Science

Java Programming: Can I get an example of a program that will allow 4 options? Example...

Java Programming: Can I get an example of a program that will allow 4 options?

Example Output:

Choose on of the options below:

1. Display all items in CSV file. (CSV file includes for example brand of phone, size, price)
2. Pick an item by linear searching
3. Pick an item by bineary searching
4. Quit Program

Solutions

Expert Solution

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 ********************************************************

**********************************************************************************************************************************************


Related Solutions

Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Can I get an example of a statement of Financial Performance or tutorial?
Can I get an example of a statement of Financial Performance or tutorial?
Can i get a statement of purpose example for a RFP proposal
Can i get a statement of purpose example for a RFP proposal
Python programming: can someone please fix my code to get it to work correctly? The program...
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks! # Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
In a Java program, how could I write a program that can assign values that would...
In a Java program, how could I write a program that can assign values that would make a rock paper scissors game work? I have a program that will generate a computer response of either rock, paper, or scissors but how can I compare a user input of "rock", "paper", or "scissors" so that we can declare either the user or the computer the winner.
Can I get an example of a M&A proposal to calculate the value of a company...
Can I get an example of a M&A proposal to calculate the value of a company using the DCF model?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT