I am doing a program that will convert euros and yens to US dollars and then give the total amount of money upon exiting the program. I need a gift class to hold the total. Basically I did two file a ConvertCurrency file and a gift file. It is has been pretty much down hill from there. Any suggestion how to link the files correctly.

import java.util.*;

public class ConvertCurrency

{
    public static void main (String [] args) //start main method

    // define constants
    final double USdollars_Rate = 1.0
    final double Euro_Rate = 1.24
    final double Yen_Rate = .0091

    // define variables
    int choice = 0;
    double value = 0;
    double answer = 0;

    // description of Program to user
    System.out.print ("This program will convert USdollars, Euros, ad Yen");
    System.out.println ("to Usdollars based on the selction of the user.");
    System.out.println ("Please make you selection.");

    // set up input scanner
    scanner stdin1 = new scanner (System.in);

    // get choice from user
    System.out.print ("To convert USdollars to US dollars, press 1");
    System.out.println ("To convert Euros to USdollars, press 2");
    System.out.println ("To convert Yen to USdollars, press 3");
    System.out.println ("To exit the program, press 4");
    System.out.println ( );
    choice = stdin1.nextInt();

    Gift choice = new Gift ();

    while (choice != 4) // set up the while statement

    {
        switch ( choice ) // use a switch statement to switch choice

        {
            case 1: // USdollars conversion
            choice.enterUSdollars;
            break;

            case 2: // Euros conversion
            choice.enterEuros;
            break;

            case 3: // Yen conversion
            choice.enterYen;
            break;

            default: // Exit to accumulate total
            System.out.println ("Exit the program and give total.");
        }
    }

}

gift file

import java.util.*;

public class Gift () {

{

// method to enter USdollars
System.out.println ("Enter amount of US dollars received: "); 
value = stdin1.nextDouble() ; // read in the user input
answer = value * USdollars_RATE ; // do the math
totalCollected += USD; 
System.out.println(USD + "US dollars equals " + USD + "US"); 

//method to enter Euros
System.out.println ("Enter amount of Euros received: "); 
value = stdin1.nextDouble() ; // read in the user input 
answer = value * Euros_RATE; 
totalCollected += USD; 
System.out.println(USD + "US dollars equals " + USD + "US"); 

// method to enter Yen
System.out.println ("Enter amount of Yen received: "); 
value = stdin1.nextDouble() ; // read in the user input
answer = value * Yen_RATE; 
totalCollected += USD; 
System.out.println(USD + "US dollars equals " + USD + "US"); 

// default method
System.out.println ("totalCollected" = USD ") ;

}
}
}
}

Dani AI

Generated

A focused, testable design fixes the confusion in the original snippets and also satisfies the instructor’s requirement for a Gift class. Keep responsibilities separate: main handles the menu and Scanner I/O; ConvertCurrency exposes pure conversion methods (no I/O); Gift is a simple accumulator that holds the running total. This follows ’s aggregation idea, echoes ’s OO advice, and acknowledges ’s point that the Gift class can be minimal if that’s all the assignment needs.

A compact, safe approach uses BigDecimal for money and small, well-named APIs. Example method layout (implementation omitted for brevity):

public class ConvertCurrency {
    private final BigDecimal EURO_RATE = new BigDecimal("1.24");
    private final BigDecimal YEN_RATE  = new BigDecimal("0.0091");

    public BigDecimal euroToUsd(BigDecimal euros) { ... }
    public BigDecimal yenToUsd(BigDecimal yen)   { ... }
    public BigDecimal usdToUsd(BigDecimal usd)   { return usd; }
}
public class Gift {
    private BigDecimal total = BigDecimal.ZERO;
    public void add(BigDecimal amount) { total = total.add(amount); }
    public BigDecimal getTotal() { return total; }
}

Wiring in the main loop is straightforward: instantiate both classes, read amounts into BigDecimal (use the String constructor or BigDecimal.valueOf), call the appropriate converter, then call gift.add(convertedAmount). On exit, format the result with NumberFormat.getCurrencyInstance().

Troubleshooting and cautions: the original code shows common Java errors (missing semicolons, wrong Scanner capitalization, invalid class syntax, calling methods without parentheses, and redeclaring a variable named choice as both an int and a Gift). Avoid putting I/O inside ConvertCurrency/Gift, prefer private fields with accessor methods, and construct BigDecimal from String to prevent floating-point surprises. This pattern meets the assignment intent while producing code that’s easy to read, test, and extend.

Recommended Answers

All 5 Replies

I don't think it would make sense to 'link' the classes, or at least the way you have written them it doesn't make sense. From what I can see, you need to make the 'CurrencyConvert' class nothing but methods and possibly aggregate that class into the Gift class. With what you have now, there's really nothing you can do. Not being rude or anything, but it'd probably be best to start over.

I'll give a small example of what it should look like:

public class ConvertCurrency
{
    private final double USdollars_Rate = 1.0
    private final double Euro_Rate = 1.24
    private final double Yen_Rate = .0091

   public ConvertCurrency()
   {
       super();
   }
    
   public void convertToUS(double amount)
   {
         //conversion
   }
}

Second class

class Gift
{
   private ConvertCurrency  cc;
  
   public Gift()
   {
      super();
      cc = new ConvertCurrency();
   }
   public void getAmount()
   {
      System.out.print("Enter an amount --> ");
     //get the input
    cc.convertToUS(input);
   }
}

There may be better options of combining the classes, but I'm not sure the exact functionality you expect out of this.

I understand completely where you are coming from. I have started over. I had what I thought was a really good program, it work, met the requirements. I submitted and was told I needed a gift class. I'm having problems not only comprehending how to get the gift class linked and why it is necessary.

I don't think it would make sense to 'link' the classes, or at least the way you have written them it doesn't make sense. From what I can see, you need to make the 'CurrencyConvert' class nothing but methods and possibly aggregate that class into the Gift class. With what you have now, there's really nothing you can do. Not being rude or anything, but it'd probably be best to start over.

I'll give a small example of what it should look like:

public class ConvertCurrency
{
    private final double USdollars_Rate = 1.0
    private final double Euro_Rate = 1.24
    private final double Yen_Rate = .0091

   public ConvertCurrency()
   {
       super();
   }
    
   public void convertToUS(double amount)
   {
         //conversion
   }
}

Second class

class Gift
{
   private ConvertCurrency  cc;
  
   public Gift()
   {
      super();
      cc = new ConvertCurrency();
   }
   public void getAmount()
   {
      System.out.print("Enter an amount --> ");
     //get the input
    cc.convertToUS(input);
   }
}

There may be better options of combining the classes, but I'm not sure the exact functionality you expect out of this.

Something working doesn't mean it's good (especially in an academic environment) ;)
Proper OO design is more important when you're a student than just producing working code, it should become so automatic that you don't even consider writing something that's not properly designed and start to gag on seeing poorly written code.

And yes, I've had teachers who rejected working solutions that weren't created according to the laid out specifications while accepting faulty solutions that did meet the specs.
This is not quite what happens in business (in business both would be rejected), but it's a start.

Thanks for the comments. What I meant to day (and didn't due to frustration) is that he is asking for something he has not taught. It is a introductory java class and none of us have ever program before. I have no problem meeting the requirements and turning in quality work but some guidance would be helpful. Again, thanks for your comments

Did the professor tell you what the Gift class is supposed to represent?

If this program is about converting currency, what does a gift have to do with converting currency?

You mentioned that the Gift class must hold the total. You could use the Gift class below and create a Gift object in the main method (as you did earlier) and call the updateTotal after converting the currency.

class Gift
{
    private int total;

    public void updateTotal(int amount)
    {
        total += amount;
    }

     public int getTotal()
     {
        return total;
     }
}

This class isn't necessary. You could just maintain an variable in the ConvertCurrency class.

Please give more information about the project so a more helpful response can be given

:?: For more help,

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.