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)    }
Java Counter Program I can't get my program to add the number of times a number...
Java Counter Program I can't get my program to add the number of times a number was landed on for my if statements for No12 through No2. My Code: import java.util.Scanner; import java.util.Random;    import java.lang.*;       public class Dice    {               public static void main(String[] args)        {            Scanner in = new Scanner(System.in);            int Continue = 1;            //randomnum = new Random();           ...
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
JAVA PROGRAMMING Is there a way I can use a method to place a user input...
JAVA PROGRAMMING Is there a way I can use a method to place a user input variable into an array? Then call the same method to print the array? I'm new to Java programming I'm not sure how to continue. For example: import java.util.Scanner; public class PartayScanner { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter pokemon 1:"); String pokemon1 = scan.nextLine(); System.out.println("Enter pokemon 2:"); String pokemon2 = scan.nextLine(); System.out.println("Enter pokemon 3:"); String pokemon3 = scan.nextLine();...
Translate the following steps into a Java program Get the monthly_fees Get the minutes Get the...
Translate the following steps into a Java program Get the monthly_fees Get the minutes Get the rate If minutes is less than or equal to 1000 then bill = monthly_fees Otherwise if minutes is between 1000 and 2000 (not including 1000) bill = (minutes - 1000)*rate + monthly_fees Otherwise bill = (minutes - 1000)*rate * 1.5 + monthly_fees Display “The bill is ” + bill Display “Thank you for using our program”
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?
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT