import java.util.Scanner;

public class Bakery {
	private static String flavors;
	private static String weight;
	private static int quantity;

	public Bakery(){
		this("Chocolate Moist", "1(KG)", 1);
	}
	
    public Bakery(String flavors, String weight, int quantity) {
    	this.flavors = flavors;
    	this.weight = weight;
    	this.quantity = quantity;
    }
    
    public void setFlavors(String flavors){
    	this.flavors = flavors;
    }
    public String getFlavors(){
    	return flavors;
    }
    
    public void setWeight(String weight){
    	this.weight = weight;
    }
    public String getWeight(){
    	return weight;
    }
    
    public void setQuantity(int quantity){
    	this.quantity = quantity;
    }
    public int getQuantity(){
    	return quantity;
    }
    
    public double getPrice(){
    	double price = 0;
    	
    	if(weight == "1KG")
    		price = 25.5;
     	if(weight == "2KG")
    		price = 50;
    	if(weight == "3KG")
    		price = 70;
    	
    	return price;
   }
    
    public String toString(){
    	return "\t" + getFlavors() + "\t\t" + getWeight() + "\t\t\t" + getPrice() + "\t\t\t\t" + getQuantity() + "\t\t\t\t" + getPrice() * getQuantity();
    }
}

class TestBakery{
	static double grandTotal = 0;

	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		System.out.print("Enter how many types of cake you would like to order: ");
		int numberOrder = scan.nextInt();
		Bakery[] bakeryOrder = new Bakery[numberOrder];

		System.out.println();
		System.out.println("Flavour");
		System.out.println("\t1. Chocolate Moist");
		System.out.println("\t2. StrawBerry");
		System.out.println("\t3. BlueBerry");
		System.out.println("\t4. Cheesy Cake");
		System.out.println("\t5. American Chocolate");
		System.out.println();
		System.out.println("Price List");
		System.out.println("\t(1)KG = RM25.50");
		System.out.println("\t(2)KG = RM50.00");
		System.out.println("\t(3)KG = RM70.00");
		
		int i = 1;
		for(int x = 0; x < bakeryOrder.length; x++, i++){
			String flavour = "";
			String w = "";
			System.out.println("Bakery item " + i);
			System.out.println("-------------");
			
			System.out.print("Enter your choice of cake flavour (1 - 5): ");
			int choiceFlavour = scan.nextInt();
			if(choiceFlavour == 1)
				flavour = "Chocolate Moist";
			if(choiceFlavour == 2)
				flavour = "StrawBerryt";
			if(choiceFlavour == 3)
				flavour = "BlueBerry";
			if(choiceFlavour == 4)
				flavour = "Cheesy Cake";
			if(choiceFlavour == 5)
				flavour = "American Chocolate";
			
			System.out.print("Enter the Weight Of Cake (1 - 1KG, 2 - 2KG and 3 - 3KG): ");
			int choiceWeight = scan.nextInt();
			if(choiceWeight == 1)
				w = "1KG";
			if(choiceWeight == 2)
				w = "2KG";
			if(choiceWeight == 3)
				w = "3KG";
				
			System.out.print("Enter quantity ordered: ");
			int q = scan.nextInt();
			System.out.println();
			
			bakeryOrder[x] = new Bakery(flavour, w, q);
		}
		
		int j = 1;
		System.out.println("Order Details:");
		System.out.println("----------------");
		System.out.println("No	Cake Flavour	  Weight	Unit Price(RM)	Quantity	Total Price(RM)");
		System.out.println("--	--------------	  ------	--------------	--------	---------------");
		for(int x = 0; x < bakeryOrder.length; x++, j++){
			System.out.println(j + bakeryOrder[x].toString());
			grandTotal += (bakeryOrder[x].getPrice() * bakeryOrder[x].getQuantity());
		}
		System.out.println("---------------------------------------------------------------------------");
		System.out.println("\t\t\t\t\t\t\t\t\t\t\t\tGrand Total:\t\t" + grandTotal);
	}
}

take a look and test with it,
if i input 2 into numberOrder, the output should have two information out, but the information is the same as second input.

Nice program :)

The problem is the use of static. When you use static, the value of the variable is class dependent, and not object dependent.

Example:

class A {
A(int a) {
this.a = a;
}
public static int a; // Note: STATIC
}

class B {
void doSomething () {
A.a = 1;
// A.a = 1
A.a = 5;
// A.a = 5

// Note: I never created any object of A. Because of the static flag, I can just use the class.

new A(6);
// A.a  = 6
}
}
class A {
A(int a) {
this.a = a;
}
public int a; // Note: NOT STATIC
}

class B {
void doSomething () {
A a1 = new A(1);
// a1.a = 1
A a2 = new A(2);
// a1.a = 1 and a2.a = 2

// Note: Each object keeps is own values, because a is now object dependent.
}
}

Good luck!

oh it work correctly. thanks dude
by the way, i wanna ask, when do we know wanna to put static? because sometime i not put static, then got compile errors, this making me confusing.
and how to set the alignment instead of using \t to set alignments?

Using static:

I think you were confused by static methods. Mainly you will get compile errors on using variables in the main method (where any program starts). The main method is static, and you cannot use any variables you use in your class because of that. Make an instance of the class you want to use, and you can use your normal variables:

class A {
public static void main (String [] args) {
new A();
}
A () {
doWhatEverYouWantWithYourNonStaticVariables();
}
}

The '\t' character is the best for spacing out outputs. Use more then one to make large spaces, with some testing it should work out. You can also work with spaces and someString.length and while loops to fill up spaces, but I think that is quite ugly.


Please mark this thread as solved when it is solved.

is it something like must be linked?
let's say
if i define a static variable inside a class but outsite the main, so it will successful called from main right?
another words, if i define non-static variable inside a class but outsite the main, the compile will error, unless we declare inside the main.
am i right?

Oh ya, how to fixed the alignment instead of using \t to fixed, because using \t will run off the alignment because of the letter lengths.

This works for me: simple spacing with '\t'.

class Test {
	
	public static void main(String[] args) {
		System.out.println ("a\tbc\tdef\t.");
		System.out.println ("\t\t\t.");
	}
}

Hmm... Do you know the keyword 'volatile' in C++? In Java, static is very similar...

To align your printout, this is a part of your design. You could try to arrange by programmatically add a correct number of white spaces. Using \t is a simple way but could screw your display as you see. Still think that you should close this thread and create a new one to specifically ask for what you are looking for...

You would declare a variable static, if you want that variable to be common to ALL objects. For example,

class Foo
{
public static int counter = 0;
private int objectNumber;
  public Foo()
{ 
objectNumber = ++counter;

}
public int getObjectNumber()
{
return objectNumber;
}
}

So, ALL instances of this FOO class will have the same value of counter. And therefore will be able to create a unique objectNumber

is it something like must be linked?
let's say
if i define a static variable inside a class but outsite the main, so it will successful called from main right?
another words, if i define non-static variable inside a class but outsite the main, the compile will error, unless we declare inside the main.
am i right?

No. What we are now talking about are global and local variables:

You can declare a variable in two ways: global and local. Global means inside a class, but outside anyethod. This variable can be used anywhere in this class, and is object dependent.
You can also declare a variable locally, this means INside a method or block. This variable will ONLY exist as long as the method or block runs.

Example:

class A {
int a; // global variable
static int b; // global static variable
public static void main {
int c; // local variable for method main
static int d; // local static variable for method main
}

void doSomething() {
while (something) {
int e; // local variable in while block
static int f; // local static variable in block (makes no sense)
}
}
}

oh, i think i understand already. :) thanks Dude. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.