Im having trouble getting the input from one file to another.
Main file: - package assigment3;
-
-
/**
-
*
-
* @author mguer017
-
*/
-
public class Main {
-
-
/** Creates a new instance of Main */
-
public Main() {
-
}
-
-
public static void main(String[] args) {
-
Cashier c = new Cashier();
-
c.add ("Bread", 2.99); //adds string and double
-
c.add ("Chicken", 6.79); // adds string and double
-
c.add ("Egg", 3.07); // adds string and double
-
c.average(); // calculate the average price
-
c.tendered(20); // Twenty dollars were tendered
-
-
System.out.println(c); // print Cashier()
-
-
}
-
-
}
-
Cashier file: (Calculations file) - package assigment3;
-
-
/**
-
*
-
* @author mguer017
-
*/
-
public class Cashier {
-
/* Working on it
-
private static final double DOLLAR = 1.00;
-
private static final double QUARTER = 0.25;
-
private static final double DIME = 0.10;
-
private static final double NICKEL = 0.05;
-
private static final double PENNY = 0.01;
-
*/
-
private String Item;
-
private int Total_Items, Tendered;
-
private double Price, average, Total;
-
-
/** Creates a new instance of Cashier */
-
-
-
public Cashier() {
-
System.out.println(getItem() + " ......... $" + getPrice() + "\n" + "----------" + "\n" + getTotal());
-
System.out.println("Amount tendered: $" + getTendered());
-
System.out.println("The change is: $" + getChange());
-
System.out.println("There were " + getTotal_Items() + " items");
-
System.out.println("Average price is: $" + average);
-
System.out.println("Your change: $" + getChange());
-
-
-
}
-
public void add(String It, double Prc) {
-
this.Item = It;
-
this.Price = Prc;
-
-
}
-
-
public void tendered(int Amount) {
-
Tendered = Amount;
-
-
}
-
-
public double average() {
-
return average = Total_Items / Total;
-
-
}
-
-
public String getItem() {
-
return Item;
-
}
-
-
public double getPrice() {
-
return Price;
-
}
-
-
public double getTotal() {
-
return Total = Total + Price;
-
}
-
-
public double getTendered() {
-
return Tendered;
-
}
-
-
public int getTotal_Items() {
-
return Total_Items;
-
}
-
-
public double getChange() {
-
return Tendered - Total;
-
}
-
-
}
-
The output comes with 0's and null on the strings. - null ......... $0.0
-
----------
-
0.0
-
Amount tendered: $0.0
-
The change is: $0.0
-
There were 0 items
-
Average price is: $0.0
-
Your change: $0.0
-
Any idea what I am doing wrong?
The assignment gave me the code of the main file, so I only need to mess with the Cashier file.
Thanks,
Miguel Guerin
12 2005 BigDaddyLH 1,216
Recognized Expert Top Contributor
Consider two lines of your code: - Cashier c = new Cashier();
-
c.add ("Bread", 2.99); //adds string and double
Before you can get to the second line, you must execute the first, which calls: - public Cashier() {
-
System.out.println(getItem() + " ......... $" + getPrice() + "\n" + "----------" + "\n" + getTotal());
-
System.out.println("Amount tendered: $" + getTendered());
-
System.out.println("The change is: $" + getChange());
-
System.out.println("There were " + getTotal_Items() + " items");
-
System.out.println("Average price is: $" + average);
-
System.out.println("Your change: $" + getChange());
-
}
Clearly, the constructor is the last place to do that!
Yes, I was thinking on calling each one on the main file, but the assignment gave me the code for the main file. - ... Here is a typical test class.
-
-
class TestCashier
-
{
-
public static void main(String[] arg)
-
{
-
Cashier c = new Cashier();
-
c.add("Bread", 2.99);
-
c.add ("Chicken", 6.79);
-
c.add("Egg", 3.07);
-
c.average();
-
c.tendered(20); // Tenty dollars were tendered
-
System.out.println(c);
-
}
-
} ...
Is there a way to do it this way, or the professor is messing with us =p
This is my first semester in java so I don't have a lot of experience with the language.
BigDaddyLH 1,216
Recognized Expert Top Contributor
What do you think the last line should accomplish:
Sorry I don't get what you mean with that. I believe that is calling the Cashier() from the Cashier.java file because I create a new Cashier() with the variable "c".
As I stated in my last post I do not have a lot of experience.
BigDaddyLH 1,216
Recognized Expert Top Contributor
Sorry I don't get what you mean with that. I believe that is calling the Cashier() from the Cashier.java file because I create a new Cashier() with the variable "c".
As I stated in my last post I do not have a lot of experience.
I appreciate you don't have much experience, but if you don't know what a line of code does, and you are just typing it in because your instructor told you to do so, you've crossed the line that separates logic from voodoo.
That statement causes Cashier's toString method to be called. Have you discussed toString methods?
When I add this line: - public String toString() {
-
return Item + "........ $" + Price;
-
}
It only shows the last input. Egg ..... $3.07
and on "toString() " it warns me about a "Add@overri de annotation" What does this means?
BigDaddyLH 1,216
Recognized Expert Top Contributor
When I add this line: - public String toString() {
-
return Item + "........ $" + Price;
-
}
It only shows the last input. Egg ..... $3.07
and on "toString() " it warns me about a "Add@overri de annotation" What does this means?
You compiler is suggesting you do this: - @Override
-
public String toString() {
-
...
-
}
That @Override is an example of an annotation. It indicates that you are attempting to override a method. A compiler warning is just that, a warning and not an error, but the compiler has your best interests at heart. When you learn about annotations you'll see this is a safety function.
More on annotations: http://java.sun.com/j2se/1.5.0/docs/...notations.html
Any idea on why is only showing the last input? I been reading about the toString() but I still have problems getting the concept.
BigDaddyLH 1,216
Recognized Expert Top Contributor
Any idea on why is only showing the last input? I been reading about the toString() but I still have problems getting the concept.
If your code hasn't changed much from the initial post, fields item and price can only record what was passed by the last call to method add. If you want Cashier to store multiple items and their prices, you are going to write more code: to define a class that represents an order item and store instances of it in an array or collection within Cashier.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Tek9_AK |
last post by:
I need to find a way to transfer all the values of an array inside a
function out of the fuction into another array. IE
function splice($filename){
if(file_exists($filename)){
$value=array("0"); // clear the array.
$rows=file($filename); // break text file into a rows array
for($num=0;$num < count($rows); ++$num){
$column=split(";",$rows);
$value=$column;}}
|
by: tjshadyluver |
last post by:
ok i have gotten this damn project almost done but i am frustrated on
this one step. getting 18-35 together and 36-50 and so on. here is my
code how can i get them combined in one line instead of writing it out a
million times. please help me. i have to write a program that you type in
the age and it stores it in a text file then i have to catagorize them and
when i hit the read button it shows how many times 18-35 was typed in, then
how...
|
by: melanieab |
last post by:
Hi,
Easy question. It seems to me that I'm following the examples correctly,
but apparently I'm not. I'm trying to retrieve the data from a row called
"row" and a column called "File". This is what I have: (xFile is the int
value in column File and tCat is the table)
First I try:
xFile = int.Parse((tCat.Rows).ToString());
Another example I found:
|
by: wandafoda |
last post by:
Sorry guys....another homework question and if you don't wanna help out..i understand.
just to say..im' a chemical engineer and we are required at will to do whatever...thast why i have to take C++ and would like to also understand what im' doing.....to further my career..
i'll include the program after the description
So my teacher gave the class an algorithm we have to manipulate......its a struct and i understand what is going on in...
|
by: JamesB |
last post by:
I am writing a service that monitors when a particular app is started.
Works, but I need to get the user who is currently logged in, and of course
Environment.UserName returns the service logon (NT_AUTHORITY\SYSTEM).
I understand that when the service starts, no user may be logged in, but
that's ok, as the app I am monitoring can only be run by a logged in user.
Do I need to use WMI to get the user context of Explorer.exe or is there a...
| |
by: jerry |
last post by:
Ok, I have a feed that I need to get the data from. The file is an
html file that contains items. I need to step through the file,
getting the first item, doing whatever, then the second item, and so
on. Any thoughts on the best way to to this?
<Feed>
<Item>
<ID>Data I need</ID>
<Title>Data I need</Title>
</Item>
|
by: joebenjamin |
last post by:
This is a problem I was trying to help a few friends figure out for fun. I
am not sure how to go about this, but there is something I am missing
here. Here is what we want the output to be:
Need to read in a time period from the keyboard (for example 1.2 seconds,
3.4 seconds, or 8.37 seconds). Once the time has been read in, we want to
print out the word “TICK” and then wait the designated time period and
print the word “TICK” again....
|
by: garyrowell |
last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received.
The program
This week you are going to write three classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below.
Card.Java
This is a simple class that represents a playing card.
Card has two attributes:
• rank which is a String that represents the value...
|
by: jacob navia |
last post by:
Hi
We are rewriting the libc for the 64 bit version of lcc-win
and we have added a new field in the FILE structure:
char *FileName;
fopen() will save the file name and an accessor function
will return the file name given a FILE *.
Questions:
What would be the best name for this function?
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |