I'm unsure if I've encapsulated properly, and also, How can i use my methods and constructors in the main?? I get errors as I can't figure out how to create a new object using the main method, nor am I able to access the methods I've written.. Netbeans tells me i can't access non-static methods from a static context...so I'm at a loss of how to properly set up the permissions.
Here are the Project "requirements"
This project needs to be created as ONE Class file that will have the main method. When you create a new project in Netbeans you will come to a window shown below. Set the name of the main class as mentioned in the project description!
Create a MAIN class Ledger (The class should be encapsulated) that will record the sales for a store. It will have the attributes
• sale—an array of double values that are the amounts of all sales
• salesMade—the number of sales so far
• maxSales—the maximum number of sales that can be recorded
Following methods should be added to the class
• Ledger(max)—a constructor that sets the maximum number of sales to max
• addSale(d)—adds a sale, to the array, whose value is d
• getNumberOfSales—returns the number of sales made
• getTotalSales—returns the total value of the sales
• getAverageSale()—returns the average value of all the sales
• getCountAbove(v)—returns the number of sales that exceeded v in value
• A main method that will test this class. The main methods should be included in this class and not as a separate file. The main method should ask the user to enter a particular sale (call the methods to enter the sale after performing important illegal checks and checking if the array has an empty space) and then ask the user if the sales have finished through a loop. After the sales have been finished, show the following outputs: number of sales made, total sales, average sales and number of sales greater than a user entered sale price!
Rules:
1) Make sure that your program checks for illegal entries.
2) The program should be commented appropriately for easy understanding.
3) You class name should be “Ledger”. After you are finished with your programs (building and running it), please rename the file using the following format:
The class file should be named:
YourFullName_Project_5_Ledger.java
An example of the file name, if I were to write the program, would be RohitDua_Project_5_Ledger.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package project5.Main;
import javax.swing.*;
/**
*
* @author Azeem
*/
public class Ledger
{
private int maxSales;
private int salesMade;
private double[] sales;
Ledger(int x)
{
maxSales = x;
sales = new double[maxSales];
}//constructor sets the max sales to the max value as defined by user.
double[] addSale(double d)
{
double[] ans = new double[getSales().length+1];
System.arraycopy(getSales(), 0, ans, 0, getSales().length);
ans[ans.length - 1] = d;
return ans;
}//copies existing sales and adds new element
public boolean trufal(char yesno)
{
boolean yes = true;
if(yesno == 'y'||yesno=='Y'){yes = true;}
if(yesno == 'n'||yesno=='N'){yes = false;}
return yes;
}//currently this method is not used
/**
* @return the maxSales
*/
public int getMaxSales() {
return maxSales;
}
/**
* @param maxSales the maxSales to set
*/
public void setMaxSales(int maxSales) {
this.maxSales = maxSales;
}
/**
* @return the salesMade
*/
public int getSalesMade() {
return salesMade;
}
/**
* @param salesMade the salesMade to set
*/
public void setSalesMade(int salesMade) {
this.salesMade = salesMade;
}
/**
* @return the sales
*/
public double[] getSales() {
return sales;
}
/**
* @param sales the sales to set
*/
public void setSales(double[] sales) {
this.sales = sales;
}
/**
* Program asks if user wants to enter sales.
* if yes, continuously loop until the user sets sales as finished, i.e. do while continue = true.
* program loops through the sales to be recorded
* during loop, user enters double value for array object for each sale which is error checked.
* method addsale returns new array with old values plus new value
* for each new sale input, copy src = previous new array from index 0 to end, and place in new array of +1length at index 0.
*
* during loop, new array at last index = user input double
* when loop ends, the array is saved.
*
* getnumberofSales returns the amount of sales made (# times looped through)
* gettotal sales returns the sum of all the values in the final array.
* getAverageSale returns the mean of all the values in the finaly array.
* getCountabove returns all values in final array that are greater than userinput V
*
*/
public static void main(String[] args)
{
int Continue = JOptionPane.showConfirmDialog(null, "Would you like to begin entering sales?", "Welcome to Ledger!", JOptionPane.YES_NO_OPTION);
do
{
double input = Double.parseDouble(JOptionPane.showInputDialog("Please enter the sales amount:"));
Ledger.setMaxSales();
Continue = JOptionPane.showConfirmDialog(null, "Would you like to continue entering sales?", "Continue?", JOptionPane.YES_NO_OPTION);
//System.out.println("Continue = " + Continue);
}while(Continue == JOptionPane.YES_OPTION);
// TODO code application logic here
}
}
This post has been edited by greatestone4eva: 10 May 2009 - 12:01 PM

New Topic/Question
Reply




MultiQuote




|