Click to See Complete Forum and Search --> : Java Pizza Program Help


mojoopop
November 6th, 2009, 05:16 PM
so here is the big problem... the point of this assignment was to make a program that accepts 2 arguments of which only the first letter of each is read so i understand i would have to use the .charAt method to read Them

after it reads the strings and accepts the char values it is supposed to switch them out for strings after it has done that it must check the validity of the switch and output a message saying either ("your size is: +checksize") or ("invalid size") i understand that part

so here is the point of my problem i dont know how to get my argument that i inputed into my checkorder class from my orderform class? both the orderform and the check order use my pizza class.

here are my three classes so far...

p.s. i have also included the slides where i was given instructions on what to do in case you can not understand what i said...

/**
* Write a description of class Pizza here.
*
* @author Michael Wojtysiak
* @version 1.1
*/
public class Pizza
{
private String size;
private String toppings;
private static int PizzasSold;


public Pizza()
{
System.out.println("Pizza One - Default Constructor");
setSize("Small");
setToppings("Regular");
System.out.println("Size: " + size);
System.out.println("Toppings: " + toppings);
System.out.println("");
}

public Pizza(String setSize, String setToppings)
{
this.size = setSize;
this.toppings = setToppings;
System.out.println("Pizza Three - Two Parameter Constructor");
System.out.println("Size: " + size);
System.out.println("Toppings: " + toppings);
System.out.println("");
}

public Pizza(String setSize)
{
setSize("Medium");
setToppings("Regular");
System.out.println("Pizza Two - One Parameter Constructor");
System.out.println("Size: " + size);
System.out.println("Toppings: " + toppings);
System.out.println("");
}

public void setSize(String size)
{
this.size = size;
}

public void setToppings(String toppings)
{
this.toppings = toppings;
}

public String getSize()
{
return this.size;
}

public String getToppings()
{
return this.toppings;
}

public static int getPizzasSold()
{
return PizzasSold;
}

// public boolean equal(Pizza CompPizza)
// {
// if(this.getSize()==(CompPizza.getSize()))
// {
// if(this.getToppings()==(CompPizza.getToppings()))
// {
// return true;
//}
// else
// {
// return false;
// }
// }
// return false;
// }

public boolean equal(Pizza CompPizza)
{
if(this.getSize()==(CompPizza.getSize()))
{
if(this.getToppings()==(CompPizza.getToppings()))
{
return true;
}
else
{
return false;
}
}
return false;
}
private void calculatePrice()
{

}


public void display(String size, String toppings, double price)
{
System.out.println("Size: " + size);
System.out.println("Toppings: " + toppings);
System.out.println("Price: " + price);

}




}

import java.util.Scanner;

/**
*Has Pizzas
*
* @author Michael Wojtysiak
* @version 1.1
*/
public class OrderForm
{
public static void main(String args[])
{


String mySize = args[0];
String myToppings = args[1];



CheckOrder.checkSize(args[0]);
// CheckOrder.checkTopping(args[1]);

Pizza One = new Pizza();
Pizza Two = new Pizza(mySize);
Pizza Three = new Pizza(mySize,myToppings);




boolean Res = Three .equal(One);
if(Res == true)
{
System.out.println("They are the same!");
}
else
{
System.out.println("They are different!");
}





}
}
/**
* Write a description of class CheckOrder here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CheckOrder
{
public static boolean checkSize(String size)
{
char num = size;
String checksize;

switch(num)
{
case 's' : checksize = "Small";
break;
case 'm' : checksize = "Medium";
break;
case 'l' : checksize = "Large";
break;
default : checksize = "Invalid";

}

if ((checksize == "Small")||(checksize == "Medium")||(checksize == "Large"))
{
System.out.println("Your Size is: " + checksize);
}


else
{
System.out.println("Invalid Size");
}





}

//if(boolean checkSize = true)
//{
// size = "Small";
// }
//else
//{
// System.out.println("Invalid");
// }




}

Xeel
November 6th, 2009, 06:13 PM
The code itself makes little sense, but anyways...

The things I saw in the first 5 seconds:

error#1: char num = size;
You can't initialize char with a string directly. One of many ways to do it would be char num = size.charAt(0); considering you are sure there is at least one char in the string.

error#2: if ((checksize == "Small")||(checksize == "Medium")||(checksize == "Large")), this.getSize()==CompPizza.getSize() and more...
That's a nice one =). This is no JavaScript, you can't compare objects' contents the way you do with primitives. Use equals() method for this (i.e. str1.equals(str2)).

And I am not even talking about algorithm corrections/optimizations... The program will throw basic exceptions almost every line if something goes wrong. Check status of one step before starting the next one.

mojoopop
November 6th, 2009, 06:58 PM
ok number 1 was delt with what do you suggest about error 2... and ye the codes probably incorrect in many ways but please note i am a first year college student with no background in any programming and i am only a month in my object oriented java 1 program

Deliverance
November 8th, 2009, 01:32 PM
The code itself makes little sense, but anyways...

The things I saw in the first 5 seconds:

error#1: char num = size;
You can't initialize char with a string directly. One of many ways to do it would be char num = size.charAt(0); considering you are sure there is at least one char in the string.

error#2: if ((checksize == "Small")||(checksize == "Medium")||(checksize == "Large")), this.getSize()==CompPizza.getSize() and more...
That's a nice one =). This is no JavaScript, you can't compare objects' contents the way you do with primitives. Use equals() method for this (i.e. str1.equals(str2)).

And I am not even talking about algorithm corrections/optimizations... The program will throw basic exceptions almost every line if something goes wrong. Check status of one step before starting the next one.

I want to add a suggestion to the OP and make it more explicit what Xeel is saying. I very seldom see people doing this, and maybe they're just used to it or just followed one bad programmer's example, but consider this when doing string comparisons:



String s = null;
if(s.equals("Something")) {
// NullPointerException
}
if("Something".equals(s)) {
// Will never NPE
}



Always use the latter approach of comparing a string literal against a reference object when you can.