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

I have been working on this assignment in Java programming and can not get it to...
I have been working on this assignment in Java programming and can not get it to work. This method attempts to DECODES an ENCODED string without the key.    public static void breakCodeCipher(String plainText){ char input[]plainText.toCharArray(); for(int j=0; j<25; j++){ for(int i=0; i<input.length; i++) if(input[i]>='a' && input[i]<='Z') input[i]=(char)('a'+((input[i]-'a')+25)%26); else if(input[i]>='A'&& input[i]<='Z') input[i]=(char)('A'+ ((input[i]-'A')+25)%26); } System.out.println(plainText)    }
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
how can I get my program to read one of 4 text files (chosen/ inputted by...
how can I get my program to read one of 4 text files (chosen/ inputted by user, game1.txt, game2, game3, or game4.txt). and place its information into variables, one of which is an array. I sort of understand this, but I don't know how to make my program know which parts of the textfile go into which variables. my 4 textfiles are in the format: the list of e4-bc need to be entered into my array. B 35 e4 e5...
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...
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...
in java pls Write a Car Dealership management program. This program will allow the employees of...
in java pls Write a Car Dealership management program. This program will allow the employees of the Dealership to add cars to their database and sell them. You will be implementing two classes: Car and CarDealership. Car will store data associated with the cars and CarDealership will be the management program. Note: 5 points of your grade is based on Coding Style. You will need to update the Starter Code to follow the standards described here. Use the "Run" button...
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT