1 Replies - 720 Views - Last Post: 28 October 2007 - 06:48 PM Rate Topic: ***** 1 Votes

#1 fdcchief  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-October 07

Client Program Question

Posted 28 October 2007 - 06:02 PM

I have a question about my homework. The assignment is here:

http://faculty.unlv....assignment6.doc

Now, I have completed it, but I am not sure if I did it the right way, and I know that it could probably be simplified. Any help would be great. Thanks

Here is my code:

import java.text.DecimalFormat;

public class Client
{
	public static void main(String[] args) 
	{

		//Set Product Information
		//Product Number, Name, Price
		Product com =	new Product(1, "Computer", 350.00);
		Product pri =	new Product(2, "Printer", 40.00);
		Product sca =	new Product(3, "Scanner", 45.50);
		Product dig =	new Product(4, "Digital Camera", 150.99);
		
		//Set Order Information
		//Order Number, Date
		Order one = new Order(1, "October 10, 2007"); 
		Order two = new Order(2, "October 23, 2007");
		
		//Set Lineitem Information
		//Lineitem Number, Quantity
		Lineitem first = new Lineitem(1, 1);	//1 Computer
		Lineitem second = new Lineitem(2, 2);	//2 Printers
		Lineitem third = new Lineitem(3, 1);	//1 Computer
		Lineitem fourth = new Lineitem(4, 3);	//3 Scanners
		Lineitem fifth = new Lineitem(5, 2);	//2 Cameras
		
		//decimal format
		DecimalFormat bal = new DecimalFormat ("$#,##0.00");
		
		//Extra Credit Table Structure
		String order1 = "Order # " + one.getOrderNumber();
		String order2 = "Order # " + two.getOrderNumber();
	
		String header = "Lineitem #	Product		Price	   Quantity	Total";
	
		String table1 = "	" + first.getItemNum() + "	" + com.getProdname() + "	" + 
		bal.format(com.getProdprice()) + "		" + first.getQuantity() + "	" +
		bal.format(com.getProdprice() * first.getQuantity());
		
		String table2 =  "	" + second.getItemNum() + "	" + pri.getProdname() + "		" + 
		bal.format(pri.getProdprice()) + "		" + second.getQuantity() + "	" +
		bal.format(pri.getProdprice() * second.getQuantity());

		String table3 =  "	" + third.getItemNum() + "	" + com.getProdname() + "	" + 
		bal.format(com.getProdprice()) + "		" + third.getQuantity() + "	" +
		bal.format(com.getProdprice() * third.getQuantity());
		
		String table4 =  "	" + fourth.getItemNum() + "	" + sca.getProdname() + "		" + 
		bal.format(sca.getProdprice()) + "		" + fourth.getQuantity() + "	" +
		bal.format(sca.getProdprice() * fourth.getQuantity());
		
		String table5 =  "	" + fifth.getItemNum() + "	" + dig.getProdname() + "	" + 
		bal.format(dig.getProdprice()) + "		" + fifth.getQuantity() + "	" +
		bal.format(dig.getProdprice() * fifth.getQuantity());
		
		String total1 = "Grand total: " + bal.format((com.getProdprice() * first.getQuantity()) +
		(pri.getProdprice() * second.getQuantity()));
		
		String total2 = "Grand total: " + bal.format((com.getProdprice() * third.getQuantity()) +
		(sca.getProdprice() * fourth.getQuantity()) + (dig.getProdprice() * fifth.getQuantity()));
		
		System.out.println(order1);
		System.out.println(header);
		System.out.println(table1);
		System.out.println(table2);
		System.out.println(total1);
		System.out.println("\n" + order2);
		System.out.println(header);
		System.out.println(table3);
		System.out.println(table4);
		System.out.println(table5);
		System.out.println(total2);
		}
}

public class Product
{	
	private int prodnum;				//product number
	private String prodname;		//product name
	private double prodprice;		//unit price
	
	//Product variables
	Product (int x, String y, double z)
	{
		prodnum = x;
		prodname = y;
		prodprice = z;
	}
			
	public int getProdnum()
	{
		return prodnum;
	}
	
	public String getProdname()
	{
		return prodname;
	}
	
	public double getProdprice()
	{
		return prodprice;
	}
	
	public void setProdnum(int x)
	{
		prodnum = x;
	}

	public void setProdname(String y)
	{
		prodname = y;
	}
	
	public void setProdprice(double z)
	{
		prodprice = z;
	}
	
}

public class Order
{
	private int orderNumber;	//Order Number
	private String orderDate;	//Order Date stored as a String variable

	//Order variables
	Order(int x, String y)
	{
		orderNumber = x;
		orderDate = y;
	}
			
	public int getOrderNumber()
	{
		return orderNumber;
	}
	
	public String getOrderDate()
	{
		return orderDate;
	}
	
	public void setOrderNumber(int x)
	{
		orderNumber = x;
	}
	
	public void setOrderDate(String y)
	{
		orderDate = y;
	}

}

public class Lineitem
{
	private int itemNum;		//Item number
	private int quantity;	//Item quantity
	private Product lineProduct;
	
	//Lineitem variables
	Lineitem(int x, int y)
	{
		itemNum = x;
		quantity = y;
	}
			
	public int getItemNum()
	{
		return itemNum;
	}
	
	public int getQuantity()
	{
		return quantity;
	}
	
	public void setItemNum(int x)
	{
		itemNum = x;
	}

	public void setQuantity(int y)
	{
		quantity = y;
	}

	
}




Is This A Good Question/Topic? 0
  • +

Replies To: Client Program Question

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Client Program Question

Posted 28 October 2007 - 06:48 PM

Yeah you got some problems here and you didn't do it exactly right. The idea behind the assignment is having your objects consume other objects. Your order class is suppose to consume line item objects and your line item objects are to consume your products.

On your assignment sheet, look back at the order class section, part B where it shows an array of line items. You don't have that. Your class also does not take in line item objects and put them in the array. So you should see something like this when you are using your order class...

// Create our order
Order one = new Order(1, "October 10, 2007"); 

// Create our line item (ordering a computer)
Lineitem first = new Lineitem(1, 1);

// Lets also create a line item for 2 printers
Lineitem second = new Lineitem(2, 2);

// Pass the line items to the order
// This is where the order is consuming line items.
one.addLineItem(first);
one.addLineItem(second);

// Ask order to total its line items
one.getTotalOrder();



Then in our getTotalOrder() you would loop through the array of line items, asking each item to give it a value for its line, you then just add them up and spit back the total. This fulfills part D of your Order class specs.

See where I am coming from here? So to make this work you order has to have the array like outlined in part B of your Order class definition in the assignment, you will also need a method which will take in a LineItem as a parameter. Then you will need another method in your order which will loop through the items, asking each line item for its total, add them up and give it back to the customer (aka main()).

I hope this is making sense. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1