The Artima Developer Community
Sponsored Link

Java Answers Forum
How to put it into Class Method

8 replies on 1 page. Most recent reply: Mar 2, 2004 5:34 AM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

How to put it into Class Method Posted: Feb 29, 2004 1:02 PM
Reply to this message Reply
Advertisement
Hi TWC,

This is the part of the code for the Sales option, i modified based on the array that u created for me.
As you can see in the Main Display part of the code,
i am unable to make use of the Forloop because this affected the alignment of the Main Display.

i try to put some of almost repeated task in Class Method but unsuccessfully.
Can u tell me how to implement it?

This is part of the code:
if(prdCode.equalsIgnoreCase("c1") && inventory[0].prdQtyPrepared >= prdQty){
	inventory[0].prdQtyPrepared -= prdQty;
	inventory[0].prdQtySold += prdQty;
	System.out.println("Quantity prepared =" + inventory[0].prdQtyPrepared);
	System.out.print("Quantity Sold =" + inventory[0].prdQtySold);
}
else if(prdCode.equalsIgnoreCase("c2") && inventory[1].prdQtyPrepared >= prdQty){
	inventory[1].prdQtyPrepared -= prdQty;
	inventory[1].prdQtySold += prdQty;
	System.out.println("Quantity prepared =" + inventory[1].prdQtyPrepared);
	System.out.print("Quantity Sold =" + inventory[1].prdQtySold);	
}


This is workable code that i write:
import java.io.*;
 
public class Product {
	//instance variables are private
	private String prdCategory;
	private String prdFavourite;
	private int prdQtyPrepared;
	private int prdQtySold;
	private int prdCostPrice;
	private int prdSellingPrice;
	private int prdTotalCostPrice;
	private int prdTotalSellingPrice;
	private int prdDifference;
 
	//Constructor to constuct an object of the class the proper way.
	public Product(String category, String favourite, int qtyPrepared, int qtySold, int costPrice, int sellingPrice,int totalCostPrice, int totalSellingPrice, int difference)
		{
     
		prdCategory = category;
		prdFavourite = favourite;	
		prdQtyPrepared = qtyPrepared;
		prdQtySold = qtySold;
		prdCostPrice = costPrice;
		prdSellingPrice = sellingPrice;
		prdTotalCostPrice = totalCostPrice;
		prdTotalSellingPrice = totalSellingPrice;
		prdDifference = difference;	
 
		}
 
	
	public String getCategory(){
  		return prdCategory;
	}
 
	public String getFavourite(){
  		return prdFavourite;
	}
 
	public int getQtyPrepared(){
 		return prdQtyPrepared;
	}
 
	public int getQtySold(){
 		return prdQtySold;
	}
 
	public int getCostPrice(){
 		return prdCostPrice;
	}
 
	public int getSellingPrice(){
 		return prdSellingPrice;
	}
 
	public int getTotalCostPrice(){
 		return prdTotalCostPrice;
	}
	
 
	public int getTotalSellingPrice(){
 		return prdTotalSellingPrice;
	}
 
	public int getDifference(){
 		return prdDifference;
	}
 
		
	
 
 
 
	public static void main(String[] args) throws IOException {
	
	// Array for 7computer product created
	Product[] inventory = new Product[7];
	
	// Create the Product objects in the arrays.
	inventory[0] = new Product("Computer", "DELL", 2, 0, 1000, 1700, 0, 0, 0);
	inventory[1] = new Product("Computer", "Compaq", 4, 0, 900, 1200, 0, 0, 0);
	inventory[2] = new Product("Computer", "HP", 2, 0, 1200, 1500, 0, 0, 0);
 	inventory[3] = new Product ("Printer", "Digitech", 20, 0, 150, 200, 0, 0, 0);
	inventory[4] = new Product ("Printer", "HP", 20, 0, 180, 300, 0, 0, 0);
	inventory[5] = new Product("Scanner", "HP", 100, 0, 100, 150, 0, 0, 0);
	inventory[6] = new Product("Scanner", "Digitech", 100, 0, 80, 120, 0, 0, 0);
 
	// Display the main screen 	
	System.out.println("\n");
	System.out.println("\n");
	System.out.println("\t\t" + "ComputerMart, Madame Amanda Smith");
	System.out.println("\n");
	System.out.println("Category" + "\t" + "Favourite" + "\t" + "Qty Prepared" + "\t" + "Qty Sold" + "\t" + "Selling Price($)");
	System.out.println("--------------------------------------------------------------------------------");
	System.out.println(inventory[0].getCategory() + "\t" + inventory[0].getFavourite() + "\t\t" + inventory[0].getQtyPrepared() + "\t\t" + inventory[0].getQtySold() + "\t\t" + inventory[0].getSellingPrice());
	System.out.println(inventory[1].getCategory() + "\t" + inventory[1].getFavourite() + "\t\t" + inventory[1].getQtyPrepared() + "\t\t" + inventory[1].getQtySold() + "\t\t" + inventory[1].getSellingPrice());
	System.out.println(inventory[2].getCategory() + "\t" + inventory[2].getFavourite() + "\t\t" + inventory[2].getQtyPrepared() + "\t\t" + inventory[2].getQtySold() + "\t\t" + inventory[2].getSellingPrice());
	System.out.println(inventory[3].getCategory() + "\t\t" + inventory[3].getFavourite() + "\t" + inventory[3].getQtyPrepared() + "\t\t" + inventory[3].getQtySold() + "\t\t" + inventory[3].getSellingPrice());
	System.out.println(inventory[4].getCategory() + "\t\t" + inventory[4].getFavourite() + "\t\t" + inventory[4].getQtyPrepared() + "\t\t" + inventory[4].getQtySold() + "\t\t" + inventory[4].getSellingPrice());
	System.out.println(inventory[5].getCategory() + "\t\t" + inventory[5].getFavourite() + "\t\t" + inventory[5].getQtyPrepared() + "\t\t" + inventory[5].getQtySold() + "\t\t" + inventory[5].getSellingPrice());
	System.out.println(inventory[6].getCategory() + "\t\t" + inventory[6].getFavourite() + "\t" + inventory[6].getQtyPrepared() + "\t\t" + inventory[6].getQtySold() + "\t\t" + inventory[6].getSellingPrice());	
	System.out.println("--------------------------------------------------------------------------------");	
	System.out.println("[S]ales" + "\t\t" + "[D]aily Order" + "\t\t" + "[C]ollection" + "\t\t" + "[E]xit");		
	System.out.println("\n");
 
	// Prompt user to input the options for Sales, Daily Order, Collection or Exit
	InputStreamReader reader = new InputStreamReader(System.in);
	BufferedReader input = new BufferedReader(reader);
	System.out.print(">Option: ");
	String text = input.readLine();
	System.out.println("Your choice: " + text);
 
	if(text.equalsIgnoreCase("S")){
		System.out.println("Sales");
 
		try
		{
	
		// Prompt user to input Product Code and to input Quantity Sold
			while(true)
			{
				String prdCode ="";
				String prdQuantity = "0";
				String morePrd ="";
		
				InputStreamReader inReader = new InputStreamReader(System.in);
				BufferedReader BuffReader = new BufferedReader(inReader);
				System.out.println("Items sold:-");
				System.out.print("Enter product code: ");
				prdCode = BuffReader.readLine();
				System.out.print("Enter quantity: ");
				prdQuantity = BuffReader.readLine();
				int prdQty = new Integer(prdQuantity).intValue();
		
				if(prdCode.equalsIgnoreCase("c1") && inventory[0].prdQtyPrepared >= prdQty){
					inventory[0].prdQtyPrepared -= prdQty;
					inventory[0].prdQtySold += prdQty;
					System.out.println("Quantity prepared =" + inventory[0].prdQtyPrepared);
					System.out.print("Quantity Sold =" + inventory[0].prdQtySold);
				}
				else if(prdCode.equalsIgnoreCase("c2") && inventory[1].prdQtyPrepared >= prdQty){
					inventory[1].prdQtyPrepared -= prdQty;
					inventory[1].prdQtySold += prdQty;
					System.out.println("Quantity prepared =" + inventory[1].prdQtyPrepared);
					System.out.print("Quantity Sold =" + inventory[1].prdQtySold);	
				}
				else if(prdCode.equalsIgnoreCase("c3") && inventory[2].prdQtyPrepared >= prdQty){
					inventory[2].prdQtyPrepared -= prdQty;
					inventory[2].prdQtySold += prdQty;
					System.out.println("Quantity prepared =" + inventory[2].prdQtyPrepared);
					System.out.print("Quantity Sold =" + inventory[2].prdQtySold);
				}
				else if(prdCode.equalsIgnoreCase("p1") && inventory[3].prdQtyPrepared >= prdQty){
					inventory[3].prdQtyPrepared -= prdQty;
					inventory[3].prdQtySold += prdQty;
					System.out.println("Quantity prepared =" + inventory[3].prdQtyPrepared);
					System.out.print("Quantity Sold =" + inventory[3].prdQtySold);
				}
				else if(prdCode.equalsIgnoreCase("p2") && inventory[4].prdQtyPrepared >= prdQty){
					inventory[4].prdQtyPrepared -= prdQty;
					inventory[4].prdQtySold += prdQty;
					System.out.println("Quantity prepared =" + inventory[4].prdQtyPrepared);
					System.out.print("Quantity Sold =" + inventory[4].prdQtySold);
				}
				else if(prdCode.equalsIgnoreCase("s1") && inventory[5].prdQtyPrepared >= prdQty){
					inventory[5].prdQtyPrepared -= prdQty;
					inventory[5].prdQtySold += prdQty;
					System.out.println("Quantity prepared =" + inventory[5].prdQtyPrepared);
					System.out.print("Quantity Sold =" + inventory[5].prdQtySold);
				}
				else if(prdCode.equalsIgnoreCase("s2") && inventory[6].prdQtyPrepared >= prdQty){
					inventory[6].prdQtyPrepared -= prdQty;
					inventory[6].prdQtySold += prdQty;
					System.out.println("Quantity prepared =" + inventory[6].prdQtyPrepared);
					System.out.print("Quantity Sold =" + inventory[6].prdQtySold);
				else
					System.out.println("Insufficient Qty.-Please come again");
		
				System.out.print("\n");
 
				System.out.print("More items[Y/N]?:");
				morePrd = BuffReader.readLine();
				if(morePrd.equalsIgnoreCase("N")){
					System.out.println("\n");
			 		System.out.println(">Press any key to continue");
					break;
				}
				else
			 		continue;
				
				
			
			}
		}
		catch(Exception e )
		{
			System.out.println("ERROR " + e);
		}
 
	}
	else if (text.equalsIgnoreCase("D")){
		System.out.println("Daily Order");
	else if (text.equalsIgnoreCase("C")){
		System.out.println("Collection");
	else if (text.equalsIgnoreCase("E")){
		System.out.println("Exit");
		System.exit(0);
	}
	else
		System.out.println("Invalid Option!");
 
	
 }
}

Pls advise.


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How to put it into Class Method Posted: Feb 29, 2004 2:32 PM
Reply to this message Reply
This looks much better. Let me start with a couple of quick questions. Why do you need parameters for totalCostPrice, totalSellingPrice and difference? Can't you calculate these from the other parameters like below?
prdTotalCostPrice = prdQtyPrepared * prdCostPrice;
prdTotalSellingPrice = prdQtyPrepared * prdSellingPrice;
prdDifference = prdTotalSellingPrice - prdTotalCostPrice;


Another option would be to do like Adam Duffy suggested and calculate those values when you return them. For example:
public int getTotalSellingPrice(){
  return prdQtyPrepared * prdSellingPrice;
}

Based on your other question, it looks like that might be the way to go. You could eliminate three parameters and three instance variables.

I'm still unclear on what you are trying to accomplish. Do you need to be able to simulate selling some of the product so the inventory decreases and the number sold increases?

If so, then you need mutator methods in your Product class. I'm making a guess that this might be close to what you need.

public void sellProduct(int quantitySold){
  prdQtySold -= quantitySold;
  prdQtyPrepared += quantitySold;
  //If you do NOT follow Adam Duffy's approach (second block of code above), then you'll probably need to put a copy the first block here.  
  //Since I'm not sure what your goal is, it is hard for me to advise you.
}  

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How to put it into Class Method Posted: Feb 29, 2004 2:42 PM
Reply to this message Reply
Aaargh! I hit post too soon!!!

If you put in the suggestions that I made in the previous post, then the code that you are having problems with can be modified as follows:
if(prdCode.equalsIgnoreCase("c1") && inventory[0].prdQtyPrepared >= prdQty){
	inventory[0].sellProduct(prdQty);//let class modify instance variables
	System.out.println("Quantity prepared =" + inventory[0].getQtyPrepared());//never use instance variables directly, use accessor methods
	System.out.print("Quantity Sold =" + inventory[0].getQtySold());//same as above
}
else if(prdCode.equalsIgnoreCase("c2") && inventory[1].prdQtyPrepared >= prdQty)
//more like above
}


In OOP you almost NEVER access the fields directly. They are made private to prevent you from doing that. Instead, you use accessor methods (sometimes called 'get' methods). Also, notice that I let the object take care of adding and subtracting inventory. That should almost NEVER be in the main method.

I know you are getting close to your deadline. But if I was your teacher, this would be a much better grade that what you had.

I hope this helps, and I hope that you can see a little of the OOP design going on.

twc

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How to put it into Class Method Posted: Feb 29, 2004 4:22 PM
Reply to this message Reply
Thanks a lot.
I hope that i can meet the deadline.
By the way, do u have any ideas using I/O file to the class variable instead of just exist during program run-time.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How to put it into Class Method Posted: Feb 29, 2004 7:31 PM
Reply to this message Reply
This is probably past your deadline, but here are some suggestions just in case. It has been awhile since I've done this, so I hope I haven't left anything critical out. You probably have to throw IOException or use a try-catch block. (No offense, but this seems WAY beyond a typical assignment for someone who needed help creating an array.)

This is actually a two-stage process. You have to write a small program to create the file as well as read the file in where you are currently creating the array elements.

Here is the guts of the class that will create the file.
//put in main method of a class to create data file
FileOutputStream ostream = new FileOutputStream("filename");
ObjectOutputStream p = new ObjectOutputStream(ostream);
Product inventory = new Product[7];
 
// Create the Product objects in the arrays.
inventory[0] = new Product("Computer", "DELL", 2, 0, 1000, 1700, 0, 0, 0); 
//rest of data
p.writeObject(inventory);//write array to file
p.flush();//flush the buffer
ostream.close();//close the file


This would go in the MainDisplay class.
//put in main method of a class to create data file
FileInputStream instream = new FileInputStream("filename");
ObjectInputStream n = new ObjectInputStream(instream);
 
Product inventory = (Product[])p.readObject();
instream.close();//close the file

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How to put it into Class Method Posted: Mar 1, 2004 3:07 AM
Reply to this message Reply
Hi TWC,

It doesn't work I/O method that u show me.
Actually, I have one main program file and another one text file will be created for the array storage and retriaval.

Maybe time zone difference, i still have 3 hour bfore deadline.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How to put it into Class Method Posted: Mar 1, 2004 6:06 AM
Reply to this message Reply
> Hi TWC,
>
> It doesn't work I/O method that u show me.

What problems are you encountering?
> Actually, I have one main program file and another one
> text file will be created for the array storage and
> retriaval.

I did not know that you already had a text file. The way that I showed you does not work with text files. Without knowing how the text file is set up, there is no way for me to know how to help you use it. Sorry.

> Maybe time zone difference, i still have 3 hour bfore
> deadline.

I'm sure it is long past now. I hope you have learned a little about OOP in this.

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How to put it into Class Method Posted: Mar 1, 2004 4:54 PM
Reply to this message Reply
Hi TWC,

I have submitted my coursework.
Thanks for the advice to help get my program up and running.
I have very limited knowledge in OOP and Java.
Do u have any good introductory book for Java to recommend so that i can have a better understanding of it.

Thanks......A Million..........And Have Nice Day.......

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How to put it into Class Method Posted: Mar 2, 2004 5:34 AM
Reply to this message Reply
> Hi TWC,
>
> I have submitted my coursework.
> Thanks for the advice to help get my program up and
> running.
> I have very limited knowledge in OOP and Java.
> Do u have any good introductory book for Java to recommend
> so that i can have a better understanding of it.
>
> Thanks......A Million..........And Have Nice Day.......

Here are two suggestions. Go to www.javabook.org and you will find a collegiate textbook called "Java Au Natural" that is available for download. Also "Thinking In Java" by Bruce Eckel is available in electronic form on the net. It is aimed more at experienced programmers that are switching to Java, and my not be ideal for a beginner. But it is free, so why not give it a look.

Certainly, you must have a textbook for the course that you are taking. Read it too. It should explain the fundamentals of OOP while it is teaching java.

Cheers,
twc

Flat View: This topic has 8 replies on 1 page
Topic: What is wrong?  Help please Previous Topic   Next Topic Topic: Video in Java Applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use