Hi! Im all new to this forum!
I have an assignment where Im working with linking classes together.
After that I'm supposed to build a method that lower a cricle when price lowered and make it go up when the price is raised.
I wrote everything and it works fine when lowering the price, but when raising the price I can se my circle for a millisec in a new place, and then it appears in the same place as before when price is lowered.
Method that runs to create the problem is "setPris".
I can't figure out whats the problem Ive tried everything....Our techer gave us a hint with using a temporary variable to hold the oject itself, but I think ive done it the correct way..
Please help, would mean a lot
Picture Class
/** * This class represents a simple picture. You can draw the picture using * the draw method. But wait, there's more: being an electronic picture, it * can be changed. You can set it to black-and-white display and back to * colors (only after it's been drawn, of course). * * This class was written as an early example for teaching Java with BlueJ. * * @author Michael Kolling and David J. Barnes * @version 2008.03.30 */ public class Picture { private Circle mPriceChange; /** * Constructor for objects of class Picture */ public Picture() { // nothing to do... instance variables are automatically set to null } /** * Draw this picture. */ public void draw() { mPriceChange = new Circle(); mPriceChange.makeVisible(); } /** * Move Circle down */ public void nerPris() { mPriceChange.moveVertical(200); } /** * Moves Circle upp */ public void uppPris() { mPriceChange.moveVertical(-200); } }
Goods Class
/** * Beskrivning av klassen Goods * * @author Maritha Dahlin * @version 2010-08-01 */ public class Goods { private String mBeskrivning; private int mAntal; private int mPris; private int mLagervarde; private String mNyavaror; private int mKostnad; private Picture priceshift; /** * Konstruktor för att tilldela initialvärden vid skapande av en ny instans av klassen. * @param String beskrivning * @param int pris * @param int antal */ public Goods(String beskrivning, int pris, int antal) { mBeskrivning = beskrivning; mAntal = antal; mPris = pris; } /** * Returnerar lagrat värde i variabeln mBeskrivning * @return lagrat värde i mBeskrivning */ public String getBeskrivning() { return mBeskrivning; } /** * Returnerar lagrat värde i variabeln mPris * @return lagrat värde i mPris */ public int getPris() { return mPris; } /** * Returnerar lagrat värde i variabeln mAntal * @return lagrat värde i mAntal */ public int getAntal() { return mAntal; } /** * Tilldelar värdet för variabeln mBeskrivning med det inkommande värdet i inparametern beskrivning * @param String beskrivning - ny beskrivning */ public void setBeskrivning(String beskrivning) { mBeskrivning = beskrivning; } /** * Tilldelar värdet för variabeln mAntal med det inkommande värdet i inparametern antal * @param int antal - nytt antal */ public void setAntal(int antal) { mAntal = antal; } /** * Tilldelar värdet för variabeln mPris med det inkommande värdet i inparametern pris * @param int pris - nytt pris */ public void setPris(int pris) { priceshift = new Picture(); if(mPris > pris) { priceshift.draw(); priceshift.nerPris(); mPris = pris; } else { priceshift.draw(); priceshift.uppPris(); mPris = pris; } } /** * Kontrollerar om varan finns i lager, skriver ut text på skärmen beroende på värde i mAntal. */ public void isInStorage() { if(mAntal > 1){ System.out.println("Varan " +mBeskrivning + " finns i lager!"); } else { System.out.println("Varan " +mBeskrivning + " finns INTE i lager!"); } } /** * */ public void countaLagerVarde() { mLagervarde = (mAntal * mPris); System.out.println("Lagervärdet för " + mAntal + " st " + mBeskrivning + " á-pris " + mPris + " är " + mLagervarde + " kronor."); if(mAntal > 3){ } else{ orderGoods(); System.out.println(mNyavaror); } } public String orderGoods() { mNyavaror = "Varning! Dags att beställa nya varor!"; return mNyavaror; } public void kopGoods(int antal) { mKostnad = (antal * mPris); mAntal = (mAntal - antal); System.out.println("Du har köpt " + antal + " st " + mBeskrivning + " á-pris " + mPris +". Kostnaden blir " + mKostnad + ". Det är " + mAntal + " st kvar i lager."); if(mAntal > 3){ } else{ orderGoods(); System.out.println(mNyavaror); } } }