We assume that the cakes baked in Part 1 and other dishes are distributed to a group of people in return for voluntary donations. The programme should calculate the amount of donations received and, based on the production costs, output whether the donations generated a profit or a loss. The production costs for the cakes must be entered.
Firstly, the production costs (= Kosten) incurred for baking (as a decimal number) and the number of people donating (= Anzahl der Abnhemer) should be entered. Then enter the amount of the donation (=Spendenanzhal) per person (as a decimal).
As soon as the current donation amount is at least as high as the costs based on the last donation entered, a message should be displayed (see example). There may be further donations after this. If no profit is made (i.e. the costs exceed the total donations), the programme should display "Loss" at the end.
Make sure that the formatting corresponds exactly to the examples.
Example:
? Costs: 5.20
? Number of customers: 5
? Donation: 2.50
? Donation: 0.80
? Donation: 2.00
--- Profit zone ---
? Donation: 0.50
? Donation: 1.00
Total donations: 6.8
Example:
? Cost: 4.80
? Number of recipients: 3
? Donation: 0.5
? Donation: 1
? Donation: 0.20
Total donations: 1.7
Loss
My current code is:
package einfprog;
public class Bsp02 {
public static void main(String[] args) {
// TODO: Implement.
// Teil 2
SavitchIn in = new SavitchIn();//Teil 2
System.out.print("? Kosten: ");
double kosten = in.readDouble();
System.out.print("? Anzahl der Abnehmer: ");
int anzahlAbnehmer = in.readInt();
// Gesamtspenden und Anzahl der Spenden
double gesamtspenden = in.readInt();
int spendenAnzahl = in.readInt();
boolean gewinnZoneErreicht = false;
// Eingabe der Spenden pro Person
while (true) {
System.out.print("? Spende: ");
double spende = in.readDouble();
gesamtspenden += spende;
if (!gewinnZoneErreicht && gesamtspenden >= kosten) {
gewinnZoneErreicht = true;
System.out.println("--- Gewinnzone ---");
}
// Wenn alle Personen gespendet haben
if (spendenAnzahl == anzahlAbnehmer) {
break;
}
}
System.out.println("Gesamtspenden: " + gesamtspenden);
// Prüfen, ob Gewinn oder Verlust erzielt wurde
if (gesamtspenden >= kosten) {
} else {
System.out.println("Verlust");
}
}
}
The programme is tested by a test (I don't know exactly what it asks for). At the moment I get an output error, which I think is due to the while loop.
donateNumber is 0 and always remains 0. Therefore, the condition of the if statement donateNumber == numberofpurchasers will never result in true (if numberofpurchasers != 0) The variable number of donations is currently not being increased - but I could include a
spendenAnzahl++;
for this (in the while-loop)?
However, I can't get this to work without another output error or time out - could someone illustrate how I can solve this problem?
Many thanks for help in advance!
Make sure all your variables actually relate to something. I'm not a big fan of while (true) {
. Since you have already added a loop break mechanism within the while
loop block using
if (spendenAnzahl == anzahlAbnehmer) { break; }
why not use that concept as the condition for your while
loop instead of just true
except, use variables you have already in play and phrased a little
different, something similar to:
while (donationCount < numberOfCustomers) {
then within the while
loop block, at the very beginning, have donationCount++;
. Now you can get rid of that if (spendenAnzahl == anzahlAbnehmer) { break; }
.
Putting this into practice would look something like this (be sure to read the comments in code):
// Open a Keyboard input stream:
Scanner in = new Scanner(System.in);
// Get the Target Cost from User:
System.out.print("Required Target Cost? -> $");
double cost = in.nextDouble();
// Get the number of Customers that will be donating:
System.out.print("Number of Customers? -> ");
int numberOfCustomers = in.nextInt();
// Total donations and number of donations
double totalDonated = 0.0d; // Will hold the total sum of donations acquired.
int donationCount = 0; // Counter for number of donations processed.
boolean profitZoneReached = false; // Flag to indicate when in a donations profit state.
/* Enter donations per person. Iterate through
each Customer that will be donating. */
while (donationCount < numberOfCustomers) {
donationCount++;
System.out.print("Customer Donation #" + (donationCount) + "? -> $");
double donation = in.nextDouble();
totalDonated += donation; // Add the current donation to `totalDonated`:
/* If the profitZoneReached flag is false and the
total donated amount has surpassed or equals the
Cost then set the profitZoneReached flag to true
and display "--- Profit Zone ---". */
if (!profitZoneReached && totalDonated >= cost) {
profitZoneReached = true;
System.out.println("--- Profit Zone ---");
}
}
// Indicate the sum of donations formatted to two decimal places):
System.out.println("Total donations: -> $" + String.format("%.02f", totalDonated));
/* Check whether Target, Profit, or Loss was achieved:
All displayed values are formatted to two decimal
places. */
if (totalDonated == cost) {
System.out.println("On Cost Target of -> $" + String.format("%.02f", cost) + " :)");
}
else if (totalDonated > cost) {
System.out.println("At a Profit of -> $" +
String.format("%.02f", (totalDonated - cost)) + " :D");
}
else {
System.out.println("At a Loss of -> $" + String.format("%.02f", (cost - totalDonated)) + " :(");
}