Quantcast
Viewing all articles
Browse latest Browse all 104

I made a program that should send me two "best phones" from a list of phones in a text file, but only shows one?

This is a link to my java project: https://github.com/Mundanest/Java-files/tree/main/Lab%207

(This is my first time using github, as I'm not sure how else to link a java project folder so I might have messed up)(code also at bottom)

It holds 4 classes:

  • one for printing, FindBestPhone.java

  • one for creating a Phone object, Phone.java

  • one for adding phones to various lists, PhoneList.java

  • and one for parsing the text, PhoneParser.java

The main aim is to find the two best phones from a long list of phones in "phone-data.txt".

(one phone has the highest battery capacity and another with largest screen size)


I was given an example text file "phone-data-short.txt" with the best phones being the Samsung Galaxy S8 Plus and Huawei Mate 10 Pro.

My problem with it is that when I try to print the two phones (from phone-data.txt) it only prints one phone.

I'm not sure if I'm trying to print them incorrectly or if it's in the way I am storing the list after calculating the best phones.

My best guess is that the "getBestPhones" method in PhoneList.java is returning wrong or my main "FindBestPhones" is asking for the best phones incorrectly.

I've tried replacing how I return:

return Collections.unmodifiableList(allPhones);

with:

return allPhones;

in the getBestPhones method

and how I'm printing it:

System.out.println(bestPhones);

with:

for (Phone phone : bestPhones) {    System.out.println(phone.getModel());}

But nothing seems to work


Note: I'm not sure if it is in how the best phones are calculated since that was the skeleton code provided to me.

(the addPhone() and phoneIsDominated() methods were already there)

I am also unable to print the Phone's name in my main, but not getting two best phones is more of an issue.

I'm still new to Java so I'm probably missing something obvious.

Edit (code):

My main class:

import java.io.IOException;import java.util.Collection;public class FindBestPhones {    public static String PHONES_FILE = "phone-data.txt";    public static void main(String[] args) {        // TODO: use the parseFile() method FROM PhoneParser.JAVA to get the phone data from the file        // TODO: print the model names of all the best phones: getBestPhones() from PhoneList.java        // TODO: handle I/O failures by printing an error message, that might occur in parseFile()        try {            PhoneList phoneList = PhoneParser.parseFile(PHONES_FILE);            // Get the best phones from the phone list            Collection<Phone> bestPhones = phoneList.getBestPhones();            System.out.println(bestPhones);             //            PhoneList testlist = PhoneParser.parseFile("phone-test.txt");            Collection<Phone> testcoll = testlist.getAllPhones();             System.out.println(testcoll);        } catch (IOException e) {            e.printStackTrace();        }    }} 

My custom Phone object class:

/* * Phone — a model class representing a smart phone. A phone has a model name, a * screen size, and a battery capacity. */public class Phone {    private final String model;    private final double screenSize;    private final int batteryCapacity;    public Phone(String model, double screenSize, int batteryCapacity) {        // : Ensure the screenSize and batteryCapacity are positive        // by throwing an IllegalArgumentException otherwise        //TASK 1:         if (screenSize <0 || batteryCapacity <=0) {            throw new IllegalArgumentException("Screen size and/or battery capacity must not be positive");        }        this.model = model;        this.screenSize = screenSize;        this.batteryCapacity = batteryCapacity;    }     // Gets the phone's model name.    public String getModel() {        return model;     }     // Gets the phone's diagonal screen size (in inches).    public double getScreenSize() {        return screenSize;    }     // Gets the phone's battery capacity (in mAh).    public int getBatteryCapacity() {        return batteryCapacity;    }     /* Determines whether this phone "dominates" another phone, meaning     * this phone is better in one criterion, and at least as good in the     * other criterion. */    public boolean dominates(Phone other) {        // : implement this method        // Task 2:        return (this.getScreenSize() > other.getScreenSize() ||                 this.getBatteryCapacity() > other.getBatteryCapacity());    }}

My PhoneList class:

/*  PhoneList — a model class representing a list of phones. There is   an addPhone method to add a phone to the list, a getAllPhones to get a list of all  phones, and a getBestPhones method to get a list of just the ‘best’ phones. */import java.util.*;public class PhoneList {    private final List<Phone> allPhones = new ArrayList<>();    private final Set<Phone> bestPhones = new HashSet<>();    /*     * Adds a phone to the list.     */    public void addPhone(Phone phone) {        allPhones.add(phone);        // remove from bestPhones if dominated by the new phone        Iterator<Phone> iter = bestPhones.iterator();        while(iter.hasNext()) {            Phone other = iter.next();            if(phone.dominates(other)) {                iter.remove();            }        }         // only add the new phone to bestPhones if it's not dominated        if(!phoneIsDominated(phone)) {            bestPhones.add(phone);        }    }    /*     * Determines whether a phone is dominated by any other phone.     */    public boolean phoneIsDominated(Phone phone) {        // only need to compare with the undominated phones        for(Phone other : bestPhones) {            if(other.dominates(phone)) {                return true;            }        }        return false;    }    /*     * Gets all phones. The list returned is unmodifiable.       */    public List<Phone> getAllPhones() {        // : return an unmodifiable view of the list        // Task 3.1:        //return allPhones;        return Collections.unmodifiableList(allPhones);        //return allPhones;    }    /*     * Gets all phones which are not dominated by another phone. The     * collection returned is unmodifiable.       */    public Collection<Phone> getBestPhones() {        // : return an unmodifiable view of the set        //Task 3.2:        return Collections.unmodifiableSet(bestPhones);        //return bestPhones;    }}

My PhoneParser class:

/* * PhoneParser — a helper class which can parse phone data from a string, and load * all the phone data from a text file. */import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.Scanner;public class PhoneParser {    /*     * Parses a phone data string, in the following format:     *      *     model screenSize batteryCapacity     *          * The model name is encoded with underscores instead of spaces.     */    public static Phone parse(String data) {        // : parse the phone data string, and return a Phone object.        // you may use string manipulation, a regex, or a Scanner        //Task 4:        try (Scanner scanner = new Scanner(data)){            // set delimiter to split on spaces            scanner.useDelimiter(" ");            if (scanner.hasNext()) {                String model = scanner.next().replace("_"," ");                if (scanner.hasNextDouble() && scanner.hasNextInt()) {                    double screenSize = scanner.nextDouble();                    int batteryCapacity = scanner.nextInt();                    return new Phone(model, screenSize, batteryCapacity);                }            }        } catch (Exception e) {            e.printStackTrace();        }        // Returns null if parsing fails        return null;    }    /*     * Returns a PhoneList by parsing a text file containing the phone data.     */    public static PhoneList parseFile(String filename) throws IOException {        // : create a PhoneList TASK 5:        PhoneList phoneList1 = new PhoneList();        // : create a BufferedReader to read from the file TASK 5:        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {            // : for each line, parse it as a Phone and add it to the list TASK 5:            String line;            while ((line = reader.readLine()) != null) {                Phone phone = parse(line);                if (phone != null) {                    phoneList1.addPhone(phone);                }               }        }        return phoneList1;    }}

Image may be NSFW.
Clik here to view.
My text file

The consoles prints:

[Phone@2ed94a8b]

When I run the code.

I also made a discovery, I made a test text file and filled it with 4 test phones:

Image may be NSFW.
Clik here to view.
test file

and was given this in console:

[Phone@38082d64, Phone@dfd3711, Phone@42d3bd8b]

which is only picking up 3 phones instead of 4 for some reason, this is probably where my issues lie.


Viewing all articles
Browse latest Browse all 104

Trending Articles