Hello -

I am a student who bis mid-semester in an OOP course, with zero programming experience. I am trying to understand the concepts but this stuff is tough....I have an increased level of respect for all of the developers out there!

Here is my question..in the code i am posting below. I am trying to set and then get some user entered info..but when i run the program the values for Gold and silver are not storing...because my default statment for two 0 entries is displaying.

When i comment out the code, or change the math so that 0 doesnt trigger the code..i can see that the "customer" stores and the unique ID prints fabulously.

Can anyone explain what I am missing?

import java.util.Scanner; 

public class CashForMetalsClient 
{
	static String more;
	static String c;
	static double g, s, goldtw, silvertw, valueg, values, tvsilver, tvgold, sum, wsum, fee, net;
	static final double GOLD = 400.50;
	static final double SILVER = 6.25;
	
	public static final Scanner input = new Scanner( System.in);

	public static void Conversion()
	{
		
		valueg = (Customer.getGoldWeight()) * (GOLD);
		values = (Customer.getSilverWeight()) * (SILVER);
		sum = valueg + values;
		fee = (sum)*.10; 
		net = (sum - fee);
		tvgold = (goldtw) * (GOLD);
		tvsilver = (silvertw) * (SILVER);
		
		System.out.printf("\nCurrent Cash offer for gold weight entered:$%.2f\n",valueg);
		System.out.printf("Current Cash offer for silver weight entered:$%.2f\n",values);
		System.out.printf("The total amount offered for all metals, before deduction of handling fee is:$%.2f\n\n",sum);
		System.out.printf("The handling fee for this transaction is:$%.2f\n",fee);
		System.out.printf("The Net amount of cash "+Customer.getCustomer()+" will recieve for this transaction is:$%.2f\n\n",net);
	}
			
	public static void main (String [] args)
	{
		
		System.out.println("***\t\t\t\t\t\t\t***");
		System.out.println("***\tWelcome to Cash for Metals Calculator!!!\t***");
		System.out.println("***\t\t\t\t\t\t\t***\n");
			
		do
		{
			System.out.print("Please enter Customer Name: ");
			c = input.next();
			Customer.setCustomer(c);
						
			System.out.print("Enter weight of GOLD (in ounces) you would like to sell: ");
			g =input.nextDouble(); 
			Customer.setGoldWeight(g);
				if (goldw < 0)
					{
						System.out.println("***You have entered an invalid weight.***");
						System.out.printf("\nPlease enter a 0(zero) if you have no gold to sell.");
						System.out.print("\nEnter weight of GOLD (in ounces) you would like to sell: ");
						g =input.nextDouble();
						Customer.setGoldWeight(g);
						goldtw += Customer.getGoldWeight(); 
					}
				else
					{
						goldtw += Customer.getGoldWeight(); 
					}
											
			System.out.print("Enter weight of SILVER (in ounces) you would like to sell: ");
			s =input.nextDouble();
			Customer.setSilverWeight(s);
				if (Customer.getSilverWeight() < 0)
					{
						System.out.println("***You have entered an invalid weight.***");
						System.out.printf("\nPlease enter a 0(zero) if you have no silver to sell.");
						System.out.print("\nEnter weight of SILVER (in ounces) you would like to sell: ");
						s =input.nextDouble(); 
						Customer.setSilverWeight(s);
						silvertw += Customer.getSilverWeight(); 
					}
				else 
					{
						silvertw += Customer.getSilverWeight(); 
					}
				
				wsum = (Customer.getSilverWeight())+ (Customer.getGoldWeight());
				
				if (wsum >= 1)
					{
						System.out.println();
						System.out.println("Unique Customer ID: "+Customer.getID());
						System.out.println("Customer Name: "+Customer.getCustomer());
						CashforMetals.Conversion();
					
						System.out.print("Would you like to enter another customer(Yes/No)?  "); 
						more = input.next();
						System.out.println();
					}
				else 
					{
						System.out.println("TRANSACTION CANCELLED---You have indicated you have no metal to sell.\n");
						System.out.print("Would you like to enter another customer(Yes/No)?  "); 
						more = input.next();
						System.out.println();
					}
							
		}	
		while (more.equalsIgnoreCase("yes"));
			System.out.println("*****************************************************");
			System.out.println("\tSession Totals - Transaction Summary\t"); 
			System.out.println("*****************************************************");
			System.out.println("Total Gold Weight this session: "+goldtw);
			System.out.println("Total Silver Weight Silver this session: "+silvertw);
			System.out.printf("Total Cash offered for Gold this session:$%.2f",tvgold);
			System.out.printf("\nToal Cash offered for Silver this session: $%.2f",tvsilver);
			System.out.printf("\nGrand Total Cash offered for metals this session: $%.2f",tvgold+tvsilver);
	}
}

Recommended Answers

All 3 Replies

There is also a second class...(not sure if it is helpful to see the code for that as well...but here it is)

public class Customer 
{
	static String customer;
	static double goldw, silverw;
	static long ID;
	
	public Customer (String c)
	{
		customer = c;
	}		
	public static void setCustomer (String c)
	{
		customer = c;
		ID = (long)(Math.random()*100000000);
	}
	
	public static void setGoldWeight (double g)
	{
		g = goldw; 
	}
	
	public static void setSilverWeight (double s)
	{
		s = silverw;
	}
	
	public static String getCustomer()
	{
		return customer;
	}
	public static double getGoldWeight()
	{
		return goldw;
	}
	public static double getSilverWeight()
	{
		return silverw;
	}
	public static long getID()
	{
		return ID;
	}

}

well .. first remark:
I think what you want is keeping information per instance of customer, so, I'd suggest you remove a lot of those static-keywords. static means you store the information or value for an entire class, so, if you create a second customer, you won't store a new one. well, no, actually you do, but from both customers, you'll change the value using the set methods.

I just quickly went over your code, and honestly, I doubt it 'll even compile.
for instance:
you use a variable named goldw in the line

if (goldw < 0)

, but you don't have a variable named goldw. you do have a goldtw variable though.

also, in the line

CashforMetals.Conversion();

, remove the reference to the class, since this call is made from within the CashForMetals class.
even if the call were made from within another class, it still wouldn't compile.
you must understand, Java is very case sensitive:
CashForMetals is not the same as CashforMetals :)

sometimes it's just the simplest typo's that keep you from getting the work done.

Thanks so much for taking the time to reply! This is part two of a project where we are modifying the original code. I did really well on the first part of the project (147/150) and I worked really hard, and was very proud it worked.

I have to store the weights from all the "customers" for reporting out later in a summary piece...which is where the goldtw (gold total weight--I am not fancy when naming my variables, i try to make them easy to remember so I type them out instead of copy/paste..i learn better by repitition)

The capitalization error is definitely user error :( this is my second attempt..and i started from scratch--I'll fix that thanks for pointing it out.

It's hard to post stuff on here because i dont want the answer, so I dont want to post the whole project assignment...I just want a nudge in the right direction--ultimately I want to learn.

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.