Results 1 to 4 of 4
Thread: Updating Arrays
- 12-05-2009, 10:19 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
Updating Arrays
I have a program for a real estate company that displays a menu from which the user selects and can search unsold listings, change prices, etc...
Everything is working except for making a sale, and updating selling price I assume problems are related.
Java Code:public class PropertyListing { int listingNumber; char listingType; String location; String description; float askingPrice; float sellingPrice; boolean sold; // Default constructor public PropertyListing() { listingNumber = 0; listingType = 'H'; location = null; description = null; askingPrice = 0.00f; sellingPrice = 0.00f; sold = false; }// end default constructor // 5-parameter constructor public PropertyListing(int dListingNumber, char dListingType, String dLocation,String dDescription,float dAskingPrice) { listingNumber = dListingNumber; listingType = dListingType; location = dLocation; description = dDescription; askingPrice = dAskingPrice; sellingPrice = 0; sold = false; }// end 5-parameter constructor // get listingNuber public int getListingNumber() { return listingNumber; } //end get listingNumber // get listingType public char getListingType() { return listingType; }//end get listingType // get location public String getLocation() { return location; }//end get location // get rentalFee public String getDescription() { return description; }//end get description // get askingPrice public float getAskingPrice() { return askingPrice; }//end get askingPrice // get sellingPrice public float getSellingPrice() { return sellingPrice; }//end get sellingPrice // isSold method returns a boolean for sold public boolean isSold() { return sold; }// end isSold // set askingPrice public void setAskingPrice(float dAskingPrice) { askingPrice = dAskingPrice; }// end set askingPrice // sale sets sellingPrice and sets sold to true public void sale(float dSellingPrice) { sellingPrice = dSellingPrice; sold = true; }// end sale // getCommission calculates commission for sales public float getCommission() { float getCommission = 0; if(listingType == 'H') { getCommission = sellingPrice * CommissionRate.HOUSE_RATE; } else if(listingType == 'L') { getCommission = sellingPrice * CommissionRate.LOT_RATE; } return getCommission; }// end getCommission }// end class PropertyListing
My main
And one of the menu selectionsJava Code:import java.util.Scanner; public class OakridgeListingSystem { public PropertyListing currentListings [] = {new PropertyListing(1001, 'H', "Barrhaven", "Bungalow", 230000), new PropertyListing(1445, 'L', "Orleans", "50 x 90 service lot", 50000), new PropertyListing(1888, 'L', "Carp", "1/4 acre", 90000), new PropertyListing(2933, 'H', "Kanata", "2 storey", 380000), new PropertyListing(2948, 'L', "Kemptville", "1/2 acre", 49000), new PropertyListing(2966, 'H', "Barrhaven", "Townhouse", 260000), new PropertyListing(2998, 'H', "Orleans", "Bungalow", 310000), new PropertyListing(3010, 'H', "Glebe", "2 storey", 980000), new PropertyListing(3011, 'H', "Riverside South", "Townhouse", 255000), new PropertyListing(3014, 'L', "Renfrew", "1 1/2 acre", 89000)}; public static void main(String[] args) { int userChoice; userChoice = getUserChoice(); while(userChoice != 8) { switch(userChoice) { case 1: InquiryByListingNumber inquiryByListingNumber = new InquiryByListingNumber(); inquiryByListingNumber.execute(); break; case 2: DisplayListings displayListings = new DisplayListings(); displayListings.execute(); break; case 3: ListingByLocation listingByLocation = new ListingByLocation(); listingByLocation.execute(); break; case 4: ChangeAskingPrice changeAskingPrice = new ChangeAskingPrice(); changeAskingPrice.execute(); break; case 5: PropertySales propertySales = new PropertySales(); propertySales.execute(); break; case 6: GetCommission getCommission = new GetCommission(); getCommission.execute(); break; case 7: SalesReport salesReport = new SalesReport(); salesReport.execute(); break; default: System.out.println("\nInvalid entry; re-enter"); } userChoice = getUserChoice(); } } private static int getUserChoice() { int userChoice; Scanner keyboard = new Scanner(System.in); System.out.println("\n\nSelect: "); System.out.println("1 - Display listing details"); System.out.println("2 - Display all unsold listings"); System.out.println("3 - Display by location"); System.out.println("4 - Change the asking price"); System.out.println("5 - Process a sale"); System.out.println("6 - Get Commission"); System.out.println("7 - Generate sales report"); System.out.println("8 - Exit"); System.out.print("\nEnter your choice: "); userChoice = keyboard.nextInt(); return userChoice; } }
Java Code:import java.util.Scanner; public class ChangeAskingPrice { public void execute() { OakridgeListingSystem curList = new OakridgeListingSystem(); Scanner input = new Scanner(System.in); int inListingNumber, i; float newPrice; boolean changed; changed = false; i = 0; newPrice = 0; do { System.out.println("Enter listing number: "); inListingNumber = input.nextInt(); System.out.println("\nEnter new asking price: "); newPrice = input.nextFloat(); if(inListingNumber == curList.currentListings[i].getListingNumber()) { curList.currentListings[i].askingPrice = newPrice; changed = true; } else { System.out.println("Invalid listing"); } }while(!changed); System.out.printf("Listing %d price changed to $ %f.2", curList.currentListings[i].listingNumber, curList.currentListings[i].askingPrice ); } }
The printf shows that curList.currentListings[i].askingPrice is a new price but when I go back to display listings the asking price is the original. Anyone know the problem?
- 12-05-2009, 11:44 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Java Code:public class ChangeAskingPrice { public void execute() { OakridgeListingSystem curList = new OakridgeListingSystem();
The critical word here is "new". Your ChangeAskingPrice class creates a whole new listing system - a listing whose data (the array of listings) is completely unrelated any other listing system. I presume the class that displays listings does much the same thing. Consequently the listings that you change are not the listings that you display.
You could make the listings array currentListings static.
But, in general, static==wrong. The code comments suggest you are following steps in an assignment. But if you have any choice in the matter, start again and make nothing static - save a few line main() method somewhere.
- 12-06-2009, 02:02 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
It certianly is for an assignment :)
Thank you for the help. I'll try this out when I get home.
- 12-06-2009, 02:34 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Updating my GUI
By Catkill in forum AWT / SwingReplies: 6Last Post: 09-01-2009, 05:09 PM -
Help Updating Tree via Listeners
By Seiki in forum SWT / JFaceReplies: 1Last Post: 03-27-2009, 10:46 AM -
Updating a progress bar from the UI thread
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:52 PM -
Updating Graphics
By Greedful in forum Java 2DReplies: 2Last Post: 07-20-2007, 07:12 PM -
Updating into 2 tables in the DB
By yuchuang in forum New To JavaReplies: 2Last Post: 05-12-2007, 06:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks