#(Solved)

117 messages · Page 1 of 1 (latest)

azure orchid
#

I have this problem where trying to increase and decrease a value using a hashmap, I am almost certain I am doing it correctly; however, It is still the incorrect solution

yes, this is a homework question, but I've tried everything and I need some pointers in the right direction

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("src/main/java/data.dat"));
        HashMap<String, Integer> costs = new HashMap<>();

        while (input.hasNextLine()) {
            String line = input.nextLine();
            String[] split = line.split(": ");
            String company = split[0];
            String[] details = split[1].split(" ");
            String type = details[0];
            int amount = Integer.parseInt(details[1]);

            int cost = amount;

            if (costs.containsKey(company)) {
                cost += costs.get(company);
            }

            if (type.equals("Seat") || type.equals("Meal") || type.equals("Luggage") || type.equals("Fee") || type.equals("Tax")) {
                // Add to cost
                costs.put(company, cost);
            } else if (type.equals("Discount") || type.equals("Rebate")) {
                // Subtract from cost
                cost = costs.getOrDefault(company, 0) - amount;
                costs.put(company, cost);
            }
        }

        int minimumCost = Integer.MAX_VALUE;
        for (int cost : costs.values()) {
            if (cost < minimumCost) {
                minimumCost = cost;
            }
        }

        System.out.println(minimumCost);
    }
}
#

You recorded all the details of different options in a file as you found them (your input data). There are 20 different space travel companies you have researched the deals of. Determine the total cost for each company.

Anything recorded as a seat, meals, luggage, fee or tax must be added to the cost for that company
Anything that is recorded as a discount or rebate can be subtracted from the cost for that company
After determining the final cost each, what is the final cost of the cheapest option?

Example
Consider the following example,

AAA: Seat 9997
BBB: Discount 2886
DDD: Luggage 3500
AAA: Tax 156
CCC: Fee 9468
BBB: Fee 9378
AAA: Discount 3103
DDD: Rebate 967```
Spaceliner AAA has a seat cost of 9997, tax of 156 and a discount of 3103. That brings AAA to a total of 9997+156-3103 = 7050
Spaceliner BBB has a fee of 9378, and a discount of 2886. That brings BBB to a total cost of 9378-2886 = 6492
Spaceliner CCC has a fee of 9468 and that's all, so it's final cost is 9468
Spaceliner DDD has a luggage cost of 3500 and a rebate of 967, so the final cost is 3500-967 = 2533.
Based on the small example, space liner DDD would be the cheapest, so the answer would be 2533.

Your task
Of the options presented, what is the final cost of the cheapest option?
stray shard
#

costs.put(company, cost);

#

This doesn't add to the cost, this just sets it?

#

And you pull out the value first

#
  int cost = amount;

            if (costs.containsKey(company)) {
                cost += costs.get(company);
            }

            if (type.equals("Seat") || type.equals("Meal") || type.equals("Luggage") || type.equals("Fee") || type.equals("Tax")) {
                // Add to cost
                costs.put(company, cost);
            } 
#

Why not keep the costs += costs.get(company) inside of the if statement? Not sure why you do that even if the type is not Seat, Meal, etc.

azure orchid
#

I'm not sure, I don't use maps often. This is what I came up with

#

I tried using ArrayLists before, but it also failed

stray shard
#

That's not related to Maps

#

That's just related to order of doing things

#

the discount part is correct

#

the addition isn't correct

#

so you should do a similar thing there

azure orchid
#

so it's my if statement

#

the first one

#

like inside of it

stray shard
#

Yes

azure orchid
#

if i put it in the second if statement I get a nullpointer?

stray shard
#

did you just keep the get or did you look at your else-if as well?

#

to see what you're doing there

azure orchid
#

I kept the get

stray shard
#

There's your issue

#

crashes because the key isn't present

#

so maybe you should have another if before your if-elseif thing

#

that just checks if the company is in the map already

#

if its not you add it with a cost of 0

#

and then do the normal if else action

azure orchid
#

so nest my else if statement?

stray shard
#

no

azure orchid
#

im so confused on what you're saying

stray shard
#
// Read out stuff from the line

// Check if company doesn't exist
if (map does not contain company) {
    // add company to map with 0 cost
}

if (its a cost) {
   // update cost with old + current value
}
else if (its a discount) {
   // update cost with old - current value
}
azure orchid
#

I thought the put method either add's it or updates it if it's the same company though? or am I wrong

stray shard
#

Yeah

azure orchid
#
if (!costs.containsKey(company)) {
                costs.put(company, cost);
            }

            if (type.equals("Seat") || type.equals("Meal") || type.equals("Luggage") || type.equals("Fee") || type.equals("Tax")) {
                // Add to cost
              cost += costs.get(company);
              costs.put(company, cost);
            } else if (type.equals("Discount") || type.equals("Rebate")) {
                // Subtract from cost
                cost = costs.getOrDefault(company, 0) - amount;
                costs.put(company, cost);
            }

this is what I did, It is still wrong

stray shard
#

costs.put(company, cost);

#

did you read my message

azure orchid
#

I did, I dont know what other method to add

stray shard
#

// Check if company doesn't exist
if (map does not contain company) {
// add company to map with 0 cost
}

azure orchid
#

I did that

stray shard
#

if (!costs.containsKey(company)) {
costs.put(company, cost);
}

#

is cost always 0?

azure orchid
#

no, in fact it's never 0

stray shard
#

So then you didn't do what my message said

azure orchid
#

because I don't understand what you are trying to say

#

cost shouldn't be 0 with just sending in cost

#
int amount = Integer.parseInt(details[1]);

            int cost = amount;
stray shard
#

They should be 0 initially if you don't know anything else

#

At this point (before your if) you don't know if the costs are a discount or not

azure orchid
#

correct, but we know the value that will be added or subtracted

#

that is cost

stray shard
#

That's the amount

azure orchid
#

cost = amount

#

lol

stray shard
#

no

#

A discount is not a cost

#

costs.put(company, cost);

#

this should be costs.put(company, 0)

#

and then after that you're adding the cost in your if

#

it'll work with that change

azure orchid
#

so you're saying in the if statement checking if the map contains the company, if it doesn't, I should add it with the cost of 0? and then leave the else if statement alone

stray shard
#

Yup

azure orchid
#

and I add it with the put method?

stray shard
#

Yes

azure orchid
#
if (!costs.containsKey(company)) {
                costs.put(company, 0);
            }

            if (type.equals("Seat") || type.equals("Meal") || type.equals("Luggage") || type.equals("Fee") || type.equals("Tax")) {
                // Add to cost
              cost += costs.get(company);
              costs.put(company, cost);
            } else if (type.equals("Discount") || type.equals("Rebate")) {
                // Subtract from cost
                cost = costs.getOrDefault(company, 0) - amount;
                costs.put(company, cost);
            }

so this should work?

stray shard
#

I'd do

costs.put(company, costs.get(company) + amount); in the if branch and costs.put(company, costs.get(company) - amount); in the else-if branch

#

but yeah

azure orchid
#

I see

#

it still doesn't return the correct value unfortunatly

stray shard
#

is the rest of the calculation correct?

azure orchid
#

should be

//current code
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("src/main/java/data.dat"));
        HashMap<String, Integer> costs = new HashMap<String, Integer>();

        while (input.hasNextLine()) {
            String line = input.nextLine();
            String[] split = line.split(": ");
            String company = split[0];
            String[] details = split[1].split(" ");
            String type = details[0];
            int amount = Integer.parseInt(details[1]);

            int cost = amount;

            if (!costs.containsKey(company)) {
                costs.put(company, 0);
            }

            if (type.equals("Seat") || type.equals("Meal") || type.equals("Luggage") || type.equals("Fee") || type.equals("Tax")) {
                // Add to cost
              costs.put(company, costs.get(company) + amount);
            } else if (type.equals("Discount") || type.equals("Rebate")) {
                // Subtract from cost
              costs.put(company, costs.get(company) - amount);
            }
        }

        int minimumCost = Integer.MAX_VALUE;
        for (int cost : costs.values()) {
            if (cost < minimumCost) {
                minimumCost = cost;
            }
        }

        System.out.println(minimumCost);
    }
}
stray shard
#

So what does it print

azure orchid
#

the company which ends up the smallest number after reading the data file

stray shard
#

And what does this code print

azure orchid
#

"187874"

#

the data file is 1000 lines

#

wait no

#

5047 lines

stray shard
#

Try running through the test input

azure orchid
#

looks good

stray shard
#

So do you know the correct answer for the full file?

azure orchid
#

nope :)

#

I just know it's wrong

#

i can send data file?

#

would you like?

stray shard
#

Go ahead

tardy lakeBOT
#

@azure orchid

File Attachments Not Allowed

For safety reasons we do not allow files with certain file extensions.

Code Formatting

You can share your code using triple backticks like this:
```
YOUR CODE
```

Large Portions of Code

For longer scripts use Hastebin or GitHub Gists and share the link here

Ignored these files due to them having disallowed file extensions
  • datafile.txt
azure orchid
#

uh oh

stray shard
#

Ah, might have to DM the file

azure orchid
#

oik

#

I was confused at first but I get adding the 0 lol, still dont understand why just normally putting it wouldn't work though

stray shard
#

Let's see

#

Cheapest is astrowings for me

#

292139

azure orchid
#

that doesn't sound right but I can try it

#

yeah no

#

I'm going to try to do it with a class instead of maps, I'll let you know the results

ping me if you determine a flaw in the past code

stray shard
#

And I get that result

azure orchid
#

huh

#

made sure there were no spaces in my answer

stray shard
#

gives me out this as the cheapest and most expensive

azure orchid
#

I do believe it should be in the 100,000-200,000 range from what I've heard

tardy lakeBOT
#

@stray shard

File Attachments Not Allowed

For safety reasons we do not allow files with certain file extensions.

gangrob Said

So for just AstroWings I get the following lines

Code Formatting

You can share your code using triple backticks like this:
```
YOUR CODE
```

Large Portions of Code

For longer scripts use Hastebin or GitHub Gists and share the link here

Ignored these files due to them having disallowed file extensions
  • message.txt
stray shard
#

Jesus fucking christ this bot is so bad

#

292139 is my final answer

azure orchid
#

hmmm

#

I wonder why it doesn't work

#

im almost done with my new code

#

yeah I got it

#
import java.util.*;
import java.io.*;
public class Main {
  public static void main(String[] args) throws FileNotFoundException {
    ArrayList<Company> companies = new ArrayList<Company>();
    Scanner input = new Scanner(new File("src/main/java/data.dat"));

    while (input.hasNextLine()) {
        String line = input.nextLine();
        String[] split = line.split(": ");
        String company = split[0];
        String[] details = split[1].split(" ");
        String type = details[0];
        int amount = Integer.parseInt(details[1]);
      if (type.equals("Discount") || type.equals("Rebate"))
      {
        amount *= -1;
      }
        Company c = new Company(amount,company);
      boolean thing = false;
        for(Company company1 : companies)
          {
            if (company1.getName().equals(c.getName()))
                {
                  company1.addAmount(c.getAmount());
                  thing = true;
                  break;
                }
          }
      if(!thing)
      {
        companies.add(c);
      }
  }
    int mincost = Integer.MAX_VALUE;
    String minname = "";
    for (Company company : companies)
      {
        if (company.getAmount() < mincost)
        {
          mincost = company.getAmount();
          minname = company.getName();
        }
      }
    System.out.println(minname + ": " + mincost);
    
}
}
class Company
  {
    private int amount;
    private String name;
    public Company(int amount, String name)
    {
      this.amount = amount;
      this.name = name;
    }
    public void addAmount(int amount)
    {
      this.amount += amount;
      return;
    }
    public String getName()
    {
      return this.name;
    }
    public int getAmount()
    {
      return this.amount;
    }
  }