Results 1 to 14 of 14
  1. #1
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Getting Values out of a List of Maps & Assigning to a variable.

    I have two values stored in a small txt file. My TextFileReader reads the values from disk and stores them in a List of Maps. I need to get the values from the List of Maps and pass them to the setter method in my receipt class. I can see the that the values are stored when I use the Debugger. Any help would be appreciated.

    I can't figure out how to get the values out and assign them to a variable.

    Java Code:
    package GarageCalculator;
    
    import FileService.GarageFileFormat;
    import FileService.TextFileReader;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class Startup {
    
        public static void main(String[] args) {
    
            Recepit recepit = new Recepit("100");
            AutomatedTeller machine = new AutomatedTeller();
            TextFileReader r = new TextFileReader("src/new.txt", new GarageFileFormat());
    
            List<LinkedHashMap<String, String>> fromFile = r.getAllRecords();
    
            LinkedHashMap<String, String> hours = fromFile.get(0);
    
            System.out.println(hours);
    
            recepit.setHoursRunningTotal(hours);
    
    ==============================================================
    
    package FileService;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class TextFileReader {
    
        private String filepath;
        private GarageFileFormat formatter;
    
        public TextFileReader(String filepath, GarageFileFormat formatter) {
            this.filepath = filepath;
            this.formatter = formatter;
        }
    
        public String getFilepath() {
            return filepath;
        }
    
        public void setFilepath(String filepath) {
            this.filepath = filepath;
        }
    
        public GarageFileFormat getFormatter() {
            return formatter;
        }
    
        public void setFormatter(GarageFileFormat formatter) {
            this.formatter = formatter;
        }
    
        public List<LinkedHashMap<String, String>> getAllRecords() {
            File file = new File(filepath);
            String rawDataFromFile = "";
    
            BufferedReader in = null;
            try {
                in = new BufferedReader(new FileReader(file));
                String line = in.readLine();
                while (line != null) {
                    rawDataFromFile += (line + "\n");
                    line = in.readLine(); 
                }
    
            } catch (IOException ioe) {
                System.out.println("Houston, we have a problem! reading this file");
            } finally {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
            return formatter.decodeAll(rawDataFromFile);
    
        }
    ================================================== =============
    Last edited by Norm; 05-14-2012 at 06:52 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    To get to an object stored in a Map, use the key that the object was stored with.
    To get items stored in a list will require you to iterate over the list to find the desired object.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    Thanks for your help. I figured it out.

    Now I am using those values to do some calculations. When I pass the values(feeAmount, Hours) to my receipt class i can see the through the debugger that the variables are stored and correct. But for some reason i can't get it to work correctly and add the historical hours to the current hours. The same goes with the Fee amount. Am i doing something wrong?

    Java Code:
    package GarageCalculator;
    
    import FileService.GarageFileFormat;
    import FileService.TextFileReader;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class Startup {
    
        public static void main(String[] args) {
    
            Recepit recepit = new Recepit("100");
            AutomatedTeller machine = new AutomatedTeller();
            TextFileReader r = new TextFileReader("src/new.txt", new GarageFileFormat());
    
            List<LinkedHashMap<String, String>> fromFile = r.getAllRecords();
            LinkedHashMap<String, String> map = fromFile.get(0);
    
            String hours = map.get("TotalHours:");
            String feeAmount = map.get("TotalFees:");
            
            double hrs = Double.parseDouble(hours);
            double fee = Double.parseDouble(feeAmount);
    
            recepit.setFeesToDate(fee);
            recepit.setHoursToDate(hrs);
               
    
            machine.startNewTransaction("200");
            machine.addParkingRequirements("200", 1);
            machine.addParkingRequirements("200", 1);
            machine.addParkingRequirements("200", 1);
            machine.addParkingRequirements("200", 1);
            machine.displayRecepit();
    
    
    
    package GarageCalculator;
    
    public class Recepit {
        
        private GarageInfo garageInfo;
        private GarageInfo[] gDb;
        private Transaction[] transactions= new Transaction[0];
        private double hours;
        private double feeAmount;
        
        public Recepit(String garageId) {
            if (gDb == null) {
                gDb = new GarageInfo[2];
                GarageInfo one = new GarageInfo("100", "Milwaukee", "WI");
                GarageInfo two = new GarageInfo("200", "Madison", "WI");
                gDb[0] = one;
                gDb[1] = two;
            }
            garageInfo = this.findGarage(garageId);
        }
        
        public void addItem(String garageId, double hours) {
            Transaction item = new Transaction(garageId, hours);
            Transaction[] temp = new Transaction[transactions.length + 1];
            System.arraycopy(transactions, 0, temp, 0, transactions.length);
            temp[temp.length - 1] = item;
            transactions = temp;
        }
        
        public double getBillRunningTotal() {
            double total = 0;
            
            for (Transaction transaction : transactions) {
                total += transaction.getFeeAmount();
                total += feeAmount;
            }
            return total;
        }
        
        public void setFeesToDate(double fee){
            this.feeAmount = fee;
        }
        public double getHoursRunningTotal() {
            double total = 0;
            
            for (Transaction transaction : transactions) {
                total += transaction.getHoursParked();
                total += hours;
            }
            return total;
        }
        
        public void setHoursToDate(double hours){
            this.hours = hours;       
        }
        
        public String getRecepitDataAsString() {
            String output = "Garage ID: " + garageInfo.getGarageId() + "\n"
                    + "State: " + garageInfo.getCity() + "\n"
                    + "City: " + garageInfo.getState() + "\n\n";
            
            output += "GarageID    Hours    Cost\n";
            output += "============================\n";
            
            for (Transaction c1 : transactions) {
                output += c1.getLineItemAsString() + "\n";
            }
            
            output += "\n =========================\n";
            output += "Lifetime Totals For Garage\n";
            output += "Total Sales: " + (feeAmount + getBillRunningTotal()) + "\n";
            output += "Total Hours: " + (hours + getHoursRunningTotal()) + "\n";
            
            
            return output;
        }
        
        public GarageInfo getGarage() {
            return garageInfo;
        }
        
        public void setGarage(GarageInfo garageInfo) {
            this.garageInfo = garageInfo;
        }
        
        private GarageInfo findGarage(String garageId) {
            GarageInfo garage = null;
            
            for (GarageInfo g : gDb) {
                if (garageId.equals(g.getGarageId())) {
                    garage = g;
                    break;
                }
            }
            
            return garage;
        }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    i can't get it to work correctly and add the historical hours to the current hours
    Can you post the program's current output and explain what is wrong with it and show what you want it to be.

    Try debugging the code by adding println statements that print out the values of variables as they are changed and used.

    What are the names of the variables that are not getting the correct values?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    Output

    GarageID Hours Cost
    ============================
    200 1.0 2.0
    200 1.0 2.0
    200 1.0 2.0
    200 1.0 2.0

    =========================
    Lifetime Totals For Garage
    Total Sales: 8.0
    Total Hours: 4.0

    0.0

    The output format is correct, but the hours and fees are not adding correctly.
    =====================

    I am taking the Values from the list of maps and calling the setFeesToDate() and setHourstoDate(). Line 26 -27

    ======================

    Then i was storing those values in the Receipt Class. Line 46-47

    ======================

    Then i have the AutomatedTeller object call the displayRecepit() line 35
    Which in turns calls the getRecepitDataAsString()

    ======================


    Line 95-113 Is where you can see the getRecepitDataAsString() and at line109 and 110 is where is added the hours and feeAmount. However they don't seem to be adding together.

    ================================
    I also tried to add them inside of the getBillRunningTotal() line 68-75 and getHoursRunningTotal() line 81-88. No luck

    ================================

    I hope that helps explain things a little better.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    but the hours and fees are not adding correctly.
    You forgot to show what you want the output to look like.

    Did you Try debugging the code by adding println statements that print out the values of variables as they are changed and used.?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    The output is Fine. I just need the total to be Correct, I keep losing the values in the variables hours and feeAmount.

    I did put in printLn statements in but didn't have much luck. For some reason when I call the displayRecepit() the value in hours and fees goes to zero. I not quite sure I understand what is going on. Does it have something to do with my AutomatedTeller class and the displayRecepit()?

    Java Code:
    package GarageCalculator;
    
    public class AutomatedTeller {
    
        private Recepit invoice;
    
        public void startNewTransaction(String carId) {
            invoice = new Recepit(carId);
        }
        
        public void addParkingRequirements(String garageId, double hours){
            invoice.addItem(garageId, hours);
        }
        
        public void displayRecepit(){
            System.out.println(invoice.getRecepitDataAsString());
        }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    The output is Fine
    Please post what the correct output should look like.

    Did you add println statements to show the variables' values as they change and are used? The print out should show you where the code is going wrong.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    I was losing my Variables when I was calling the displayRecepit()

    So i fixed it up and now the code looks like this. it is now adding the totals correctly.

    Java Code:
    package GarageCalculator;
    
    public class AutomatedTeller {
    
        private Recepit invoice;
    
        public void startNewTransaction(String carId) {
            invoice = new Recepit(carId);
        }
        
        public void addParkingRequirements(String garageId, double hours){
            invoice.addItem(garageId, hours);
        }
        
        public void displayRecepit(double hours, double feeAmount){
            invoice.setHoursToDate(hours);
            invoice.setFeesToDate(feeAmount);
            System.out.println(invoice.getRecepitDataAsString());
        }
    }

  10. #10
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    Corrected Output

    GarageID Hours Cost
    ============================
    200 1.0 2.0
    200 1.0 2.0
    200 1.0 2.0
    200 1.0 2.0

    =========================
    Lifetime Totals For Garage
    Total Sales: 18.0
    Total Hours: 14.0

    //These are totals that i read in at the start of the program, they are read in from a txt file.
    10.0 hours
    10.0 FeeAmount

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    it is now adding the totals correctly.
    Is the problem solved now?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    Yes that problem is solved. Now i am on my last problem. I am using my TextFileWriter to write the feeAmount and hours to a txt file. I have the writer and it works, but there is nothing in my array when i call my getBllRunningTotal() or getHoursRunningTotal().

    Java Code:
    package GarageCalculator;
    
    import FileService.GarageFileFormat;
    import FileService.TextFileReader;
    import FileService.TextFileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class Startup {
    
        public static void main(String[] args) throws IOException {
            
    
            Recepit recepit = new Recepit("100");
            AutomatedTeller machine = new AutomatedTeller();
            TextFileReader r = new TextFileReader("src/new.txt", new GarageFileFormat());
            TextFileWriter w = new TextFileWriter("src/new.txt", new GarageFileFormat());
    
            List<LinkedHashMap<String, String>> fromFile = r.getAllRecords();
            LinkedHashMap<String, String> map = fromFile.get(0);
    
            String hours = map.get("TotalHours:");
            String feeAmount = map.get("TotalFees:");
            
            double hrs = Double.parseDouble(hours);
            double fee = Double.parseDouble(feeAmount);
     
            machine.startNewTransaction("200");
            machine.addParkingRequirements("200", 1);
            machine.addParkingRequirements("200", 1);
            machine.addParkingRequirements("200", 1);
            machine.addParkingRequirements("200", 1);
            machine.displayRecepit(hrs, fee);
            
            double allHours = recepit.getHoursRunningTotal();
            double allFees = recepit.getBillRunningTotal();
                
            System.out.println(allHours);
            System.out.println(allFees);
    Java Code:
    package FileService;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class TextFileWriter {
    
        private String filepath;
        private GarageFileFormat formatter;
    
        public TextFileWriter(String filepath, GarageFileFormat formatter) {
            this.filepath = filepath;
            this.formatter = formatter;
        }
    
        public void setFilepath(String filepath) {
            this.filepath = filepath;
        }
    
        public void setFormatter(GarageFileFormat formatter) {
            this.formatter = formatter;
        }
    
        public void writeAllRecords(List<LinkedHashMap<String, String>> rawData) throws IOException {
    
            File file = new File(filepath);
            String formattedData = formatter.encodeALL(rawData);
    
            PrintWriter txtWriter = new PrintWriter(
                    new BufferedWriter(
                    new FileWriter(file, false)));
    
    
            txtWriter.println(formattedData);
            txtWriter.close();
        }
    
        public static void main(String[] args) throws IOException {
    
            List<LinkedHashMap<String, String>> data =
                    new ArrayList<LinkedHashMap<String, String>>();
            LinkedHashMap<String, String> rec1 = new LinkedHashMap<String, String>();
            rec1.put("totFees", "34.50");
            data.add(rec1);
            rec1 = new LinkedHashMap<String, String>();
            rec1.put("totHours", "44.50");
            data.add(rec1);
    
            TextFileWriter w = new TextFileWriter("src/new.txt", new GarageFileFormat());
    
            w.writeAllRecords(data);
    
    //        System.out.println("Wrote file to: " + data.getAbsolutePath());
        }
    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    there is nothing in my array
    Make sure that you are trying to get data from the same array that you put the data into. One way to see what is happening is to add println statements every time you change the array's contents that print out the contents Use the Arrays class's toString() method to format the array for printing.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Getting Values out of a List of Maps & Assigning to a variable.

    I have the file writing now and getting information from disk, but now i need to find out where my math went crazy.

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2012, 08:46 AM
  2. Replies: 3
    Last Post: 11-23-2010, 03:19 PM
  3. Replies: 0
    Last Post: 11-23-2010, 01:32 PM
  4. Comparing List of maps
    By thorne_ in forum New To Java
    Replies: 1
    Last Post: 06-10-2009, 03:30 AM
  5. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 04:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •