The Artima Developer Community
Sponsored Link

Java Answers Forum
How to Convert Class Variable to Array

2 replies on 1 page. Most recent reply: Feb 28, 2004 9:46 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 2 replies on 1 page
leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

How to Convert Class Variable to Array Posted: Feb 27, 2004 7:45 AM
Reply to this message Reply
Advertisement
Hi guys, i'm trying below class into array but i am not successful to do so.
I cannot figure out what's wrong.
Pls rescue.
Thanks.

This is the class that i'm trying to convert into array.
class Product {
	String category;
	String favourite;
	int qtyPrepared;
	int qtySold;
	int costPrice;
	int sellingPrice;
	int difference;
}
 
class MainDisplay {
	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;
 
	}
}


This is the array that i tried to create but it does not work:
class Product {
	String[] category;
	String[] favourite;
	int[] qtyPrepared;
	int[] qtySold;
	int[] costPrice;
	int[] sellingPrice;
	int[] totalSellingPrice;
	int[] totalCostPrice;
	int[] difference;
 
}
 
class ComputerProduct {
	public static void main(String[] args) {
 
	String[] category = new String[3];
	String[] favourite = new String[7];
	int[] qtyPrepared = new int[7];
	int[] qtySold = new int[7];
	int[] costPrice = new int[7];
	int[] sellingPrice = new int[7];
	int[] totalSellingPrice = new int[7];
	int[] totalCostPrice = new int[7];
	int[] difference = new int[7];
 
	String[] category = { "Computer", "Printer", "Scanner" };
	String[] favourite = { "[c1]DELL", "[c2]Compaq", "[c3]HP", "[p1]Digitech", "[p2]HP", "[s1]HP", "[s2]Digitech" };
	int[] qtyPrepared = { 0, 0, 0, 0, 0, 0, 0 };
	int[] qtySold = { 0, 0, 0, 0, 0, 0, 0 };
	int[] costPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] sellingPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] totalSellingPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] totalCostPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] difference = { 0, 0, 0, 0, 0, 0, 0 };
 
	System.out.println("\n\n");
	System.out.println("\t\tComputerMart, Madanne Amanda Smith");
	System.out.println("\n");
	System.out.println("Category\tFavourite\tQty Prepared\tQty Sold\tSelling Price($)");
	System.out.println("--------------------------------------------------------------------------------");
	System.out.println( category[0] + "\t" + favourite[0] + "\t" + qtyPrepared[0] + "\t\t" + qtySold[0] + "\t\t" + sellingPrice[0]);
	System.out.println( category[0] + "\t" + favourite[1] + "\t" + qtyPrepared[1] + "\t\t" + qtySold[1] + "\t\t" + sellingPrice[1]);
	System.out.println( category[0] + "\t" + favourite[2] + "\t\t" + qtyPrepared[2] + "\t\t" + qtySold[2] + "\t\t" + sellingPrice[2]);
	System.out.println( category[1] + "\t\t" + favourite[3] + "\t" + qtyPrepared[3] + "\t\t" + qtySold[3] + "\t\t" + sellingPrice[3]);
	System.out.println( category[1] + "\t\t" + favourite[4] + "\t\t" + qtyPrepared[4] + "\t\t" + qtySold[4] + "\t\t" + sellingPrice[4]);
	System.out.println( category[2] + "\t\t" + favourite[5] + "\t\t" + qtyPrepared[5] + "\t\t" + qtySold[5] + "\t\t" + sellingPrice[5]);
	System.out.println( category[2] + "\t\t" + favourite[6] + "\t" + qtyPrepared[6] + "\t\t" + qtySold[6] + "\t\t" + sellingPrice[6]);
	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");
 
	}
}


leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How to Convert Class Variable to Array Posted: Feb 27, 2004 8:55 AM
Reply to this message Reply
I managed to convert the class variable to array.
But i'm not sure whether it's correct or not.
Pls help to verify.
Thanks.

This is the code:
import java.io.*;
 
class ComputerMart1 {
	public static void main(String[] args) throws IOException{
 
 
	// Declare computer product array and allocate storage
	String[] category = { "Computer", "Printer", "Scanner" };
	String[] favourite = { "[c1]DELL", "[c2]Compaq", "[c3]HP", "[p1]Digitech", "[p2]HP", "[s1]HP", "[s2]Digitech" };
	int[] qtyPrepared = { 0, 0, 0, 0, 0, 0, 0 };
	int[] qtySold = { 0, 0, 0, 0, 0, 0, 0 };
	int[] costPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] sellingPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] totalSellingPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] totalCostPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] difference = { 0, 0, 0, 0, 0, 0, 0 };
 
	// Display the main screen
	System.out.println("\n\n");
	System.out.println("\t\tComputerMart, Madanne Amanda Smith");
	System.out.println("\n");
	System.out.println("Category\tFavourite\tQty Prepared\tQty Sold\tSelling Price($)");
	System.out.println("--------------------------------------------------------------------------------");
	System.out.println( category[0] + "\t" + favourite[0] + "\t" + qtyPrepared[0] + "\t\t" + qtySold[0] + "\t\t" + sellingPrice[0]);
	System.out.println( category[0] + "\t" + favourite[1] + "\t" + qtyPrepared[1] + "\t\t" + qtySold[1] + "\t\t" + sellingPrice[1]);
	System.out.println( category[0] + "\t" + favourite[2] + "\t\t" + qtyPrepared[2] + "\t\t" + qtySold[2] + "\t\t" + sellingPrice[2]);
	System.out.println( category[1] + "\t\t" + favourite[3] + "\t" + qtyPrepared[3] + "\t\t" + qtySold[3] + "\t\t" + sellingPrice[3]);
	System.out.println( category[1] + "\t\t" + favourite[4] + "\t\t" + qtyPrepared[4] + "\t\t" + qtySold[4] + "\t\t" + sellingPrice[4]);
	System.out.println( category[2] + "\t\t" + favourite[5] + "\t\t" + qtyPrepared[5] + "\t\t" + qtySold[5] + "\t\t" + sellingPrice[5]);
	System.out.println( category[2] + "\t\t" + favourite[6] + "\t" + qtyPrepared[6] + "\t\t" + qtySold[6] + "\t\t" + sellingPrice[6]);
	System.out.println("--------------------------------------------------------------------------------");

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How to Convert Class Variable to Array Posted: Feb 28, 2004 9:46 AM
Reply to this message Reply
leonglkw,

I am not trying to be mean, but there are so many things wrong in your code that I don't really know where to begin. Are you taking a class in Java? If so, then you really should sit down with your instructor. If not, then you need to get a good book and work through it.

Again, I want to stress that I'm not trying to be mean. Most of the mistakes that you are making are made by all of us when we first learn. You just seem to have made them all at once.

Let me start by posting some code that does the same thing that yours does. It is NOT a finished product, and will NOT produce any output. But neither did yours. I hope it will give you something to compare your code to, and perhaps I can answer questions from there.

I hope this helps.
tom


public class Product {
//instance variables are private
 private String category;
 private String favourite;
 private int qtyPrepared;
 private int qtySold;
 private int costPrice;
 private int sellingPrice;
 private int totalCostPrice;
 private int totalSellingPrice;
 private int difference;
 
 //Constructor to constuct an object of the class the proper way.
 public Product(String cat, String fav, int prepared, int sold, int price)
 {
     category = cat;
     favourite = fav;
     qtyPrepared = prepared;
     qtySold = sold;
     sellingPrice = price;
     totalCostPrice = c1.qtyPrepared * c1.costPrice;
     totalSellingPrice = c1.qtySold * c1.sellingPrice;
     difference = c1.totalSellingPrice - c1.totalCostPrice;
 }
 
//Still need accessor methods, and maybe some mutator methods.
}
 
//should be a separate file
public class MainDisplay {
 public static void main(String[] args) {
  //I created 3 arrays, but you could get by with just one.
  //These lines create the arrays
  Product[] computers = new Product[3];
  Product[] printers = new Product[2];
  Product[] scanners = new Product[2];
  
  //these lines create the Product objects in the arrays.
  computers[0] = new Product("Computer", "DELL", 0, 0, 1000, 1700);
  computers[1] = new Product("Computer", "Compaq", 0, 0, 900, 1200);
  computers[2] = new Product("Computer", "HP", 0, 0, 1200, 1500);
 
  printers[0] = new Product ("Printer", "Digitech", 0, 0, 150, 200);
  printers[1] = new Product ("Printer", "HP", 0, 0, 180, 300);
 
  scanners[0] = new Product("Scanner", "HP", 0, 0, 100, 150);
  scanners[0] = new Product("Scanner", "Digitech", 0, 0, 80, 120); 
 }
}
 

Flat View: This topic has 2 replies on 1 page
Topic: Tomcat server with IIS Previous Topic   Next Topic Topic: Help with random numbers

Sponsored Links



Google
  Web Artima.com   

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