Assignment

Ma and Pa need a program that will project the profit of their sporting goods department. the items in the department are with a 1,2 or 3 depending on the amounf of profit for the item. An item with a profit code of 1 produces a 10 percent profit, a code of 2 produces a 12 percent profit and a code of 3 generates a 15 percent profit. write a program that will project the profit of the following inventory.

ItemquantitypriceProfit code

Fishing_Line 1323.95 1

Fish_hooks97 0.89 2

Snikers1230.49 2

Fish_nets12 8.75 1

Spinner_Baits2562.49 3

Jigs 49 0.29 3

The program should generate a report of the item, quanity, expected profit in dollars per item, and total expected profit for all items.

You are required to divide the whole program into several functions. for example readDataFile, computation and write results..

How can i calculate the profit? and please can someone tell me how the output should look like?. i dont understand the problem properly except for the profit code part that if the code is 1 then it produces a profit of 10 percent. iknow i need to use the if/else statements but what my output is going to be ?

ThankYou

Message was edited by:

lrngjava

[1256 byte] By [lrngjavaa] at [2007-11-26 19:22:37]
# 1
You have x number of items at y price and a z profit margin. Simply plug them into a formula.If you want neatly lined up output look at printf method and/or Formatter class.
floundera at 2007-7-9 21:43:23 > top of Java-index,Java Essentials,New To Java...
# 2

> You have x number of items at y price and a z profit

> margin. Simply plug them into a formula.

>

> If you want neatly lined up output look at printf

> method and/or Formatter class.

Thanks. so what you are trying to say for example for an item fishing_line quantity(132), price(3.95), profitcode(1).

i need to calculate the total price for this tiem which would be quantity * price which is (521.40) and since the profit code is 1 so i made 10% profit which is(( 521.40/10) * 100). is this formula correct am i on the riter track? Say yes..

null

lrngjavaa at 2007-7-9 21:43:23 > top of Java-index,Java Essentials,New To Java...
# 3

hmm y is it not printing the item name?

import java.io.*;

import java.util.Scanner;

import java.io.BufferedReader;

class ProfitTester

{

public double calculateTotal()

{

int total, quantity=0, price=0;

total = quantity * price;

return total;

}

}

class ProfitCalculator

{

public static void main(String[] args) throws IOException

{

Scanner s = null;

String name;

double quantity;

try

{

s = new Scanner(

new BufferedReader (new FileReader("saloo.txt")));

while(s.hasNext())

{

name=s.next();

//name = s.readLine();

System.out.println(name);

quantity = s.nextInt();

}

}

finally

{

s.close();

}

ProfitTester test = new ProfitTester();

System.out.println(test.calculateTotal());

}

}

giving me exception in thread main.

lrngjavaa at 2007-7-9 21:43:23 > top of Java-index,Java Essentials,New To Java...
# 4
What does the error say. Are you sure the text file exists and is the correct directory?
floundera at 2007-7-9 21:43:23 > top of Java-index,Java Essentials,New To Java...
# 5

yes flounder the txt file exist and here is my updated code. i was trying to read the names only for the item and i thought array of type String would be a good option for me but instead of printing the item name it prints the whole file and after printing gives me an error.

Item QuantityPriceProfit_code

Fishing_Line132 3.951

Fish_hooks970.892

Snkers123 0.492

Fish_nets128.751

Spinner_Baits256 2.493

Jigs 490.293

Exception in thread "main" java.util.NoSuchElementException: No line found

at java.util.Scanner.nextLine(Scanner.java:1471)

at ProfitCalculator.main(ProfitCalculator.java:34)

and here is my code.

import java.io.*;

import java.util.Scanner;

import java.io.BufferedReader;

class ProfitTester

{

public double calculateTotal()

{

int total, quantity=0, price=0;

total = quantity * price;

return total;

}

}

class ProfitCalculator

{

public static void main(String[] args) throws IOException

{

Scanner s = null;

ProfitTester test = new ProfitTester();

String[] name = new String[100];

int[] number = new int[100];

double quantity;

try

{

s = new Scanner(

new BufferedReader (new FileReader("saloo.txt")));

while(s.hasNext())

{

if(s.hasNext())

{

for (int i=0; i<name.length; i++)

{

name[i] = s.nextLine();

System.out.println(" " + name[i].trim());

}

}

}

}

finally

{

s.close();

}

System.out.println(test.calculateTotal());

}

}

My method is not complete for the calculateTotal().>

lrngjavaa at 2007-7-9 21:43:23 > top of Java-index,Java Essentials,New To Java...
# 6

It is because you have a for loop inside your while loop. The for loop executes 100 times because that is how big your name array is. Each time around the for loop it reads a line from the text file. So if the text file is anything less than 100 lines long you will get that error.

On another note, how does your data get to the ProfitTester class?

floundera at 2007-7-9 21:43:24 > top of Java-index,Java Essentials,New To Java...
# 7

Gotcha.

for (int i=0; i<name.length; i++)

{

while(s.hasNext())

{

if(s.hasNext())

{

name[i] = s.nextLine();

System.out.println(" " + name[i].trim());

}

}

i guess i dont need to calculate the total in my assignment. it ask specifically to print the item, quantity, expected profit in dollars per item and total expected profit for all items. Thankyou flounder i will give you 3 dukes for rite now :). i am following your instructions which is really very helpful but what i am thinking is how can i print the item name, quantity which belongs to that item and the profit per item and total expected profit. grrr i guess i need a beer.>

lrngjavaa at 2007-7-9 21:43:24 > top of Java-index,Java Essentials,New To Java...
# 8
3 dukes awarded.
lrngjavaa at 2007-7-9 21:43:24 > top of Java-index,Java Essentials,New To Java...
# 9

This might work but still isn't optimal.

Your for loop still executes 100 times. The first time it enters the while loop, it then reads the entire text file until it is exhausted. Then for the other 99 times

around the for loop it will do nothing as the while loop will return false and not execute.

Also the if statement is useless. You have already tested s.hasNext in the while loop statement. If this is true then the if will be true. If the while is false it won't

reach the if.

floundera at 2007-7-9 21:43:24 > top of Java-index,Java Essentials,New To Java...
# 10
allrite i will try to work this thing out tomorrow please check my post. i will see if i can work this thing out tomorrow my head is banging rite now. anyways thanks
lrngjavaa at 2007-7-9 21:43:24 > top of Java-index,Java Essentials,New To Java...