The Artima Developer Community
Sponsored Link

Java Answers Forum
How To Continue With The Code for Profit and Loss Program

3 replies on 1 page. Most recent reply: Mar 1, 2004 5:09 AM by Adam Duffy

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 3 replies on 1 page
leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

How To Continue With The Code for Profit and Loss Program Posted: Feb 26, 2004 7:47 PM
Reply to this message Reply
Advertisement
Hi Viswa and all,

This is the Profit and Loss that using the Product to display info.
Is there any way to improve it to be more OOP?
Please consult.
Thanks.

This is the code that i wrote:
import java.io.*;
 
class Product {
	String category;
	String favourite;
	int qtyPrepared;
	int qtySold;
	int costPrice;
	int sellingPrice;
	int totalSellingPrice;
	int totalCostPrice;
	int difference;
}
 
class Collection {
	public static void main(String[] args) {
		Product c1 = new Product();
		Product c2 = new Product();
		Product c3 = new Product();
		Product p1 = new Product();
		Product p2 = new Product();
		Product s1 = new Product();
		Product s2 = new Product();
 
		c1.category = "Computer";
		c1.favourite = "[c1]DELL";
		c1.qtyPrepared = 0;
		c1.qtySold = 0;
		c1.costPrice = 1000;
		c1.sellingPrice = 1700;
		c1.totalCostPrice = c1.qtyPrepared * c1.costPrice;
		c1.totalSellingPrice = c1.qtySold * c1.sellingPrice;
		c1.difference = c1.totalSellingPrice - c1.totalCostPrice;
 
		c2.category = "Computer";
		c2.favourite = "[c2]Compaq";
		c2.qtyPrepared = 0;
		c2.qtySold = 0;
		c2.costPrice = 900;
		c2.sellingPrice = 1200;
		c2.totalCostPrice = c2.qtyPrepared * c2.costPrice;
		c2.totalSellingPrice = c2.qtySold * c2.sellingPrice;
		c2.difference = c2.totalSellingPrice - c2.totalCostPrice;
 
		c3.category = "Computer";
		c3.favourite = "[c3]HP";
		c3.qtyPrepared = 0;
		c3.qtySold = 0;
		c3.costPrice = 1200;
		c3.sellingPrice = 1500;
		c3.totalCostPrice = c3.qtyPrepared * c3.costPrice;
		c3.totalSellingPrice = c3.qtySold * c3.sellingPrice;
		c3.difference = c3.totalSellingPrice - c3.totalCostPrice;
 
		p1.category = "Printer";
		p1.favourite = "[p1]Digitech";
		p1.qtyPrepared = 0;
		p1.qtySold = 0;
		p1.costPrice = 150;
		p1.sellingPrice = 200;
		p1.totalCostPrice = p1.qtyPrepared * p1.costPrice;
		p1.totalSellingPrice = p1.qtySold * p1.sellingPrice;
		p1.difference = p1.totalSellingPrice - p1.totalCostPrice;
 
		p2.category = "Printer";
		p2.favourite = "[p2]HP";
		p2.qtyPrepared = 0;
		p2.qtySold = 0;
		p2.costPrice = 180;
		p2.sellingPrice = 300;
		p2.totalCostPrice = p2.qtyPrepared * p2.costPrice;
		p2.totalSellingPrice = p2.qtySold * p2.sellingPrice;
		p2.difference = p2.totalSellingPrice - p2.totalCostPrice;
 
		s1.category = "Scanner";
		s1.favourite = "[s1]HP";
		s1.qtyPrepared = 0;
		s1.qtySold = 0;
		s1.costPrice = 100;
		s1.sellingPrice = 150;
		s1.totalCostPrice = s1.qtyPrepared * s1.costPrice;
		s1.totalSellingPrice = s1.qtySold * s1.sellingPrice;
		s1.difference = s1.totalSellingPrice - s1.totalCostPrice;
 
		s2.category = "Scanner";
		s2.favourite = "[s2]Digitech";
		s2.qtyPrepared = 0;
		s2.qtySold = 0;
		s2.costPrice = 80;
		s2.sellingPrice = 120;
		s2.totalCostPrice = s2.qtyPrepared * s2.costPrice;
		s2.totalSellingPrice = s2.qtySold * s2.sellingPrice;
		s2.difference = s2.totalSellingPrice - s2.totalCostPrice;
		
		
		int sumTotalCostPrice = c1.totalCostPrice + c2.totalCostPrice + c3.totalCostPrice + p1.totalCostPrice + p2.totalCostPrice + s1.totalCostPrice + s2.totalCostPrice;		
		int sumTotalSellingPrice = c1.totalSellingPrice + c2.totalSellingPrice + c3.totalSellingPrice + p1.totalSellingPrice + p2.totalSellingPrice + s1.totalSellingPrice + s2.totalSellingPrice;
		int sumDifference = sumTotalSellingPrice - sumTotalCostPrice;
 
        	System.out.println("\n\n");
		System.out.println("\t\tComputerMart, Madanne Amanda Smith");
		System.out.println("\t\t\tDaily Sales Collection");
		System.out.println("\n");
		System.out.println("\t\t\t\tQuantity\tTotal Price($)");
		System.out.println("Category\tFavourite   Prepared   Sold\tCost  Selling\tDifference($)");
		System.out.println("--------------------------------------------------------------------------------");
		System.out.println(c1.category + "\t" + c1.favourite + "\t" + c1.qtyPrepared + "\t" + c1.qtySold + "\t" + c1.totalCostPrice + "\t" + c1.totalSellingPrice + "\t\t" + c1.difference);
		System.out.println(c2.category + "\t" + c2.favourite + "\t" + c2.qtyPrepared + "\t" + c2.qtySold + "\t" + c2.totalCostPrice + "\t" + c2.totalSellingPrice + "\t\t" + c2.difference);
		System.out.println(c3.category + "\t" + c3.favourite + "\t\t" + c3.qtyPrepared + "\t" + c3.qtySold + "\t" + c3.totalCostPrice + "\t" + c3.totalSellingPrice + "\t\t" + c3.difference);
		System.out.println(p1.category + "\t\t" + p1.favourite + "\t" + p1.qtyPrepared + "\t" + p1.qtySold + "\t" + p1.totalCostPrice + "\t" + p1.totalSellingPrice + "\t\t" + p1.difference);
		System.out.println(p2.category + "\t\t" + p2.favourite + "\t\t" + p2.qtyPrepared + "\t" + p2.qtySold + "\t" + p2.totalCostPrice + "\t" + p2.totalSellingPrice + "\t\t" + p2.difference);
		System.out.println(s1.category + "\t\t" + s1.favourite + "\t\t" + s1.qtyPrepared + "\t" + s1.qtySold + "\t" + s1.totalCostPrice + "\t" + s1.totalSellingPrice + "\t\t" + s1.difference);
		System.out.println(s2.category + "\t\t" + s2.favourite + "\t" + s2.qtyPrepared + "\t" + s2.qtySold + "\t" + s2.totalCostPrice + "\t" + s2.totalSellingPrice + "\t\t" + s2.difference);
		System.out.println("--------------------------------------------------------------------------------");
        	System.out.println("Total" + "\t\t\t\t\t\t" + sumTotalCostPrice + "\t" + sumTotalSellingPrice + "\t\t" + sumDifference);
        	System.out.println("\n");
 
 
	}
}


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: How To Continue With The Code for Profit and Loss Program Posted: Feb 27, 2004 5:09 AM
Reply to this message Reply
Suggest change to class Product.

class Product {
    String category;
    String favourite;
    int qtyPrepared;
    int qtySold;
    int costPrice;
    int sellingPrice;
 
    public int getTotalCostPrice() {
      return( qtyPrepared * costPrice );
    }
 
    public int getTotalSellingPrice() {
      return( qtySold * sellingPrice );
   }
 
   public int getDifference() {
      return( getTotalSellingPrice() -
        getTotalCostPrice() );
   }
}


Let your class do all the hard work...
Also suggest use of arrays in class named Collection.

Adam

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How To Continue With The Code for Profit and Loss Program Posted: Feb 27, 2004 5:31 AM
Reply to this message Reply
Hi Adam,

But I'm not familiar in using Array.
Any Suggestion to change the Class variable to Array?
I'm a Java beginner.
Thanks.

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: How To Continue With The Code for Profit and Loss Program Posted: Mar 1, 2004 5:09 AM
Reply to this message Reply
Have a look at this

http://java.sun.com/docs/books/tutorial/java/data/arrays.html

It will explain arrays to you better than I could.

Adam

Flat View: This topic has 3 replies on 1 page
Topic: Thanks (Help with counter) Previous Topic   Next Topic Topic: PLease Help with counter

Sponsored Links



Google
  Web Artima.com   

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