elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Sigue las noticias más importantes de seguridad informática en el Twitter! de elhacker.NET


  Mostrar Temas
Páginas: [1]
1  Programación / Java / necesito ayuda en: 27 Junio 2010, 07:27 am
soy nueva en java y estoy haciendo una tarea q me esta dando un error a la hora de imprimir, la verdad es q no se ya q voy a hacer ,no puedo usar arreglos ni nada,  :-(
aqui les dejo mi codigo,

yo debo imprimir esto
Bread............  2.99
Chicken..........6.79
Egg..................3.07
______________
Total ……….$12.85

The number of items purchased is 3 items
The average price per item is $4.28

Amount tendered is $20.00
The change is $7.15

The change includes
7 dollars
0 quarters
1 dimes
1 nickels
0 cents

pero cuando compilo lo q hice pues me sale breadchickenEgg....12.85, y no en separado (esos datos los entra el usuario y son tres nada mas)algo anda mal y no he dado todavia con lo q es, aqui les dejo las tres clases q estoy haciendo..


la clase Cashier

/**
 *
 * @author Zuzi
 */
public class Cashier {

    private String name;
    private double price;
    private int numberOfItem;
    private double tendered;
    private double amount;
    private double change;
    private double average;
    private  int totalOfItem;
    private int num;
   
    private int dollars,quarters,dimes,nickles,cents;
   
   
    private static double totalSumOfAllItems; //sum of all prices of each item
   



    public Cashier()
    {
     name="";
     price=0.0;
     amount=0.0;
     change=0.0;
     average=0.0;
     numberOfItem=0;
     num=0;
    }

    public Cashier(String name,double price)
    {
    this();
    this.name=name;
    this.price=price;

    }

    public void add(String name,double price)
    {
       
       String a=name;
       this.name=a;
       

       double p=this.price+price;
       this.price=p;
       totalOfItem++;
       totalSumOfAllItems=totalSumOfAllItems+price;
    }

    public void  average()
    {
       average=(totalSumOfAllItems/numberOfItem);
     
    }

    public static double totalPrice()
    {
     return totalSumOfAllItems;
    }

    public int getTotalOfItem()
    {
    return totalOfItem;

    }

    public double tendered(double amount)
    {
       tendered=amount;
        return tendered;
    }

    public void makeChange()
    {
     
     //change = tendered - totalSumOfAllItems;
     //change = 100 * change;
     //change = Math.round(change);
     //change = change / 100;
        change=tendered-totalSumOfAllItems;
     dollars = (int)(tendered - totalSumOfAllItems) * 100 / 100;
     cents = (int)(change * 100) % 100;
     quarters = (int)cents / 25;
     cents  = (int)cents % 25;
     dimes = (int)cents / 10;
     cents = (int)cents % 10;
     nickles = (int)cents / 5;
     cents = (int)cents % 5;
     cents = (int)cents;
    }

    public double getChange()
    {
      return change;
    }

   public double getAmount()
   {
      return amount;
   }

   public double getPrice()
   {
      return price;
   }

   public double getTendered()
   {
       return tendered;

   }

   public String getName()
   {
       return name;
   }

   public double getAverage()
   {
       return average;
   }

   public int getDollars()
   {
      return dollars;
   }
   
   public int getQuarters()
   {
       return quarters;
   }

   public int getDimes()
   {
           return dimes;
   }
   public int getNickels()
   {
           return nickles;
   }
   public int getCents()
   {
           return cents;
   }


la clase testCashier para imprimir todo

public class TestCashier {

    public static void main(String[] arg)
   {
           Cashier c = new Cashier();
           
                   
    String name = GetData.getWord("Enter name of item");
    double price = GetData.getDouble("Enter price of item");
    c.add(name, price);

    name = GetData.getWord("Enter name of item");
    price = GetData.getDouble("Enter price of item");
    c.add(name, price);
   // Add a few more entries of your own

    name = GetData.getWord("Enter name of item");
    price = GetData.getDouble("Enter price of item");
    c.add(name, price);
         
         // Now average the price of the items
              c.average();

// Make payment
  double amount = GetData.getDouble("Enter amount of money for payment");

        c.tendered(amount); // Twenty dollars were tendered
   c.makeChange();
        generateReceipt(c);


   }
       static void generateReceipt(Cashier c)
       {
           // Write the necessary code that will generate a customer’s receipt.
           // The output must be displayed in a scrollable pane
           
            Date d = new Date();
            DateFormat df = DateFormat.getDateInstance();
            NumberFormat f = NumberFormat.getCurrencyInstance();
   
         String receipt= "Sears Inc"+"\n";
                       receipt=receipt+"Welcome – thanks for stopping, \n";
                       receipt = receipt + "Date :" + df.format(d) + "\n";

                             
                receipt=receipt+ c.getName()+"......."+ f.format(c.getPrice())+"\n"
                               + c.getName()+"......."+ f.format(c.getPrice())+"\n"
                               + c.getName()+"......."+ f.format(c.getPrice())+"\n";
                       receipt=receipt+"_____________________"+"\n";
                       receipt=receipt+"Total.........."+f.format(c.totalPrice())+"\n";

                       receipt = receipt + "The number of items purchased is: " + c.getTotalOfItem() + " items" + "\n";
                  receipt = receipt + "The average price per item is: " + f.format(c.getAverage()) +  "\n";
                  receipt = receipt + "Amount tendered is: " + c.getTendered() + "\n";
                  receipt = receipt + "The change is :"+c.getChange() + "\n";
                       receipt = receipt + "The change include :" + "\n";
                  receipt = receipt + c.getDollars() + " dollars" + "\n" + c.getQuarters()+ " quarters" + "\n" + c.getDimes()+ " dimes" + "\n" + c.getNickels()+ " nickels" + "\n" + c.getCents() + " cents";
         
         JTextArea text=new JTextArea(receipt,20,40);//rows and columns
         JScrollPane s=new JScrollPane(text);


       JOptionPane.showMessageDialog(null, s,"Sears Inc",JOptionPane.INFORMATION_MESSAGE);



    }


y la clase GetData

import javax.swing.JOptionPane;

/**
 *
 * @author zuzi
 */
public class GetData {


static int getInt(String s)
{

    return Integer.parseInt(getWord(s));
}

static double getDouble(String s)
{
return Double.parseDouble(getWord(s));
}

static String getWord (String s)
{
 return JOptionPane.showInputDialog(s);

}
//Write appropriate methods to return the remaining numeric types

       static short getShort(String s)
            {
                  return Short.parseShort(getWord(s));
            }

    static byte getByte(String s)
{
return Byte.parseByte(getWord(s));
}


     static float getFloat(String s)
{
return Float.parseFloat(getWord(s));
}

    static long getLong(String s)
{
 return Long.parseLong(getWord(s));
}

    static char getChar(String s)
{
    return getWord(s).charAt(0);
}




}


gracias a todos
   

Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines