5 Replies - 7709 Views - Last Post: 29 December 2009 - 01:41 PM Rate Topic: -----

#1 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Store user input in an array inside a loop. HELP!

Post icon  Posted 29 December 2009 - 10:43 AM

Hi everyone! I have a school assignment pending and I don't seem to get the last part right.
The task is to write a program that would enable the user to enter items into a database and
then, when the user has declared no need of further entries, print out a summary of ALL items
ordered/entered.

A typical run of the program would look like this:

ENTER ITEM CODE: K330/2
ENTER ITEM DESCRIPTION: Petrol Stove
ENTER ITEM WEIGHT IN GRAMS: 1200
ENTER ITEM UNIT PRICE ($): 43.50
ENTER QUANTITY REQUIRED: 4
---------------------------------------------------------
SUMMARY DATA FOR ITEM K330/2, Petrol Stove
TOTAL WEIGHT:4800 grams
TOTAL PRICE: $174.00
----------------------------------------------------------
ANOTHER? (Y/N): Y
ENTER ITEM CODE: P556
ENTER ITEM DESCRIPTION: Water purifier
ENTER ITEM WEIGHT IN GRAMS: 450
ENTER ITEM UNIT PRICE ($): 85.00
ENTER QUANTITY REQUIRED: 6
-----------------------------------------------------------
SUMMARY DATA FOR ITEM P556, Water purifier
TOTAL WEIGHT:2700 grams
TOTAL PRICE: $510.00
------------------------------------------------------------
ANOTHER? (Y/N): Y
ENTER ITEM CODE: F32/7
ENTER ITEM DESCRIPTION: Energy Biscuits
ENTER ITEM WEIGHT IN GRAMS: 60
ENTER ITEM UNIT PRICE ($): 2.25
ENTER QUANTITY REQUIRED: 300
-----------------------------------------------------------
SUMMARY DATA FOR ITEM F32/7, Energy Biscuits
TOTAL WEIGHT: 18000 grams
TOTAL PRICE: $675.00
----------------------------------------------------------
ANOTHER? (Y/N): N
-------------------------------------------------------------
SUMMARY:
YOU HAVE ORDERED
4 Petrol Stove (K330/2) @ $43.50 TOTAL: $174.00
6 Water Purifier (P556) @ $85.00 TOTAL: 510.00
60 Energy Biscuits (P32/7) @ $2.25 TOTAL:$675.00
GRAND TOTAL WEIGHT:25.50 Kg
GRAND TOTAL COST: $1359.00

In order to get the summary at the end I thought about entering item's code and description
into two separate arrays (there is just one in the code, as I was just trying out different solutions,
but nothing seemed to work really).

The problem is : the data in the array is constantly replaced by the user's next input.

I thought i++ would help, but I have no clue why it does not, and what should I do next.

PLEASE HELP! :)

my code:

import java.util.Scanner;

public class assign{

	public static void main(String args[]){

		Scanner input= new Scanner(System.in);

		String answer, desc;
		int weight, quant, k = 100, i = 0;
		double gweight=0, price, cost=0, kgweight=gweight/1000;;


		System.out.print("Hello! Would you like you to place an order?(Y/N): ");
		  answer = input.nextLine();


	while(answer.equalsIgnoreCase("Y")){


			System.out.print("Please enter item's code: ");
				String[] code;
				code =new String[8];


			  code[i] = input.nextLine();
			  i++;


			System.out.print("Please enter item's description: ");
				desc = input.nextLine();


	System.out.print("Please enter item's weight: ");
			weight = input.nextInt();

			System.out.print("Please enter item's price: ");
			price = input.nextDouble();

			System.out.print("Please enter desired quantity: ");
			quant = input.nextInt();
					input.nextLine();

			double totalprice=(price * quant);
			weight = weight*quant;


			System.out.print("Summary of the item ordered: "+ code +", "+ desc+"\n Total Weight:"+weight +" grams \n Total Price:$"+ totalprice+"\n");

			System.out.println("Another item?(Y/N): ");
			answer = input.nextLine();



			if(answer.equalsIgnoreCase("N")){

			System.out.println("Your Order: ");
			System.out.println(quant+" "+desc+"(" + code + ")"+" @ " + price + " Total: " + totalprice);


		}
		}
		}



Thank you in advance for even trying to help me!
Have a great day.

This post has been edited by IncredibleDisappointment: 29 December 2009 - 10:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Store user input in an array inside a loop. HELP!

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,271
  • Joined: 27-December 08

Re: Store user input in an array inside a loop. HELP!

Posted 29 December 2009 - 10:52 AM

You might consider designing an Item class with variables representing the information you are getting from user input for each item. For the Item toString() method, have it return a String in the format described for summary. Then, you just have to store an array of Items.
Was This Post Helpful? 0
  • +
  • -

#3 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: Store user input in an array inside a loop. HELP!

Posted 29 December 2009 - 12:16 PM

Thanks for your quick reply! Since I am a TOTAL beginner, could you give me an example of how this would look?
If not - I do understand :) I will try to get it myself.

Thanks again!



View Postmacosxnerd101, on 29 Dec, 2009 - 09:52 AM, said:

You might consider designing an Item class with variables representing the information you are getting from user input for each item. For the Item toString() method, have it return a String in the format described for summary. Then, you just have to store an array of Items.

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,271
  • Joined: 27-December 08

Re: Store user input in an array inside a loop. HELP!

Posted 29 December 2009 - 01:28 PM

I'll meet you half-way; I'll provide a skeleton-version of the Item class and leave you to fill in the blanks. From here, it should be fairly straightforward to use an array of Items

public class Item{
   //define attributes weight, quantity, name and price which 
   //any Item object will have
   private int weight, quantity;
   private String name;
   private double price;

/*this is the constructor method. when you create an object, it 
  is called. It has only an access modifier (public, private, 
  protected) and no return type. It otherwise works like a 
  normal method in terms of parameters.
*/
   public Item(){
	 //initialize variables with default values
   }

/*Now you try to create a constructor that will accept 
  parameters for each of the attributes of weight, quantity,
  name, and price. The params provided should be the values
  for the attributes
*/

/*
  Since the variables are private, they can only be accessed
  within this class. So you need to write getter methods, which 
  return the specified variable; and setter methods, which 
  accept a param and overwrite the existing value of the  
  attribute with the param.  I will demonstrate with a getter 
  and setter for weight, you fill in the rest
*/
  //notice that this method only performs a task, but doesn't
  //return any values
  public void setWeight(int w){weight = w;}

 //notice that the return-type matches the type of the variable
  public int getWeight(){return weight;}

  public String toString(){
	 /*
	  this method needs to return a String representation of 
	  the Item. Go ahead and write the code for this method so 
	  that it returns a String value in the format you want to 
	  display for the Summary.  I think the format looks like:
	  Quantity Name (Weight) @ $Price TOTAL: $(Quantity * Price) 

	  Also, Just FYI: Any math calculations should be enclosed within parentheses if part of a String.  For example: String x = "Price: " + (1+2); Here, x = "Price: " + 3; 

	 */ 
  }
}


This post has been edited by macosxnerd101: 29 December 2009 - 01:28 PM

Was This Post Helpful? 1
  • +
  • -

#5 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: Store user input in an array inside a loop. HELP!

Posted 29 December 2009 - 01:38 PM

Thank you so much, I will get it from here. You have just taught a man how to fish :)


View Postmacosxnerd101, on 29 Dec, 2009 - 12:28 PM, said:

I'll meet you half-way; I'll provide a skeleton-version of the Item class and leave you to fill in the blanks. From here, it should be fairly straightforward to use an array of Items

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,271
  • Joined: 27-December 08

Re: Store user input in an array inside a loop. HELP!

Posted 29 December 2009 - 01:41 PM

View PostIncredibleDisappointment, on 29 Dec, 2009 - 04:38 PM, said:

Thank you so much, I will get it from here. You have just taught a man how to fish :)


You mean you're going to down a 12 cans of beer each day? :blink:

Anyways, glad I could help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1