I'm about to build a program written in Java. I've already done its pseudocode, but I'm stuck on the code and I don't know what to do exactly. Well this is my progress so far:
pseudocode:
class Customer
Print out “Enter costumer’s name” (Pop up to answer)
Random boolean
If True
print membership type
print current date and time
class Visit
Print out "Have you bought anything?" (Pop up to answer)
if the answer is “no”
print “Have a nice day!”
exit program
if else the answer is “yes”
continue
if else the answer is not “no” nor “yes”
ask again
class Discount
Print "Enter Price of Item:" (Pop up to price)
if customer's membership type is “Premium”
the discount to the price will be 20%
else if customer's membership type is “Gold”
the discount to the price will be 15%
else if customer's membership type is “Silver”
the discount to the price will be 10%
else if customer's membership type “simple”
the discount to the price will be 10%
class Main
Variables: customer, visit, discount
customer = new object
Customer visit = new object
Visit discount = new object Discount
do work with customer
do work with visit
do work with discount
Print customer.name, customer.surname, discount.price, discount.final_price
code:
import java.util.Scanner;
public class Discount
{
public static void main(String[] args)
{
String cust_name;
String cust_surname;
String answer;
String answer2;
float firstPrice, rate_1, D_rate, discount, final_price, prem_disc;
prem_disc = 0;
final_price = 0;
discount = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter Costumer's Name:");
cust_name = in.next();
cust_surname = in.next();
System.out.println("Have you bought anything?");
answer = in.next();
if (answer.equals("no"))
{
System.out.println("Have a good day!");
System.exit(0);
} else if (!answer.equals("no"))
System.out.println("Enter Price of Item:");
firstPrice = in.nextFloat();
System.out
.println("What type of membership do you have? Premium, Gold, Silver or simple?");
answer2 = in.next();
if (answer2.equals("Premium"))
{
prem_disc = 20;
discount = (firstPrice * 20 / 100);
final_price = (firstPrice - discount);
} else if (answer2.equals("Gold"))
{
prem_disc = 15;
discount = (firstPrice * 15 / 100);
final_price = (firstPrice - discount);
} else if (answer2.equals("Silver"))
{
prem_disc = 10;
discount = (firstPrice * 10 / 100);
final_price = (firstPrice - discount);
} else if (answer2.equals("simple"))
{
prem_disc = 10;
discount = (firstPrice * 10 / 100);
final_price = (firstPrice - discount);
}
System.out.println("Costumer Name:" + cust_name + " " + cust_surname
+ "\n" + "Discount Rate:" + prem_disc + "\n"
+ "Discounted Price:" + final_price + "\n");
}
}
It's working, but there are many things that are missing. :/
Just take one step at a time, and when that step completely works, proceed to the next step.
An example:
Here's your pseudo-code (which, by the way, is a great way to start):
class Visit
Print out "Have you bought anything?" (Pop up to answer)
if the answer is “no”
print “Have a nice day!”
exit program
if else the answer is “yes”
continue
if else the answer is not “no” nor “yes”
ask again
Let's just implement and test the "is the answer yes-or-no" functionality:
/**
<P>{@code java MyHomeworkMainClass}</P>
**/
public class MyHomeworkMainClass {
public static final void main(String[] ignored) {
System.out.println("Visit.isUserInputYesNo(null)=" + Visit.isUserInputYesNo(null));
System.out.println("Visit.isUserInputYesNo(\"gibberish\")=" + Visit.isUserInputYesNo("gibberish"));
System.out.println("Visit.isUserInputYesNo(\"yes\")=" + Visit.isUserInputYesNo("yes"));
System.out.println("Visit.isUserInputYesNo(\"no\")=" + Visit.isUserInputYesNo("no"));
}
}
class Visit {
public static final boolean isUserInputYesNo(String input) {
return (input != null &&
(input.equals("yes") || input.equals("no")));
}
}
Now run it and see if it does what you want:
[C:\java_code\]java MyHomeworkMainClass
Visit.isUserInputYesNo(null)=false
Visit.isUserInputYesNo("gibberish")=false
Visit.isUserInputYesNo("yes")=true
Visit.isUserInputYesNo("no")=true
It does. Now add something else small to this and just keep going until you're done. For particularly difficult parts, it may be beneficial to create a completely separate class, with its own testing class. Then at the end, merge it all together. And never get rid of your testing functions, which will always be there for diagnosing future problems.
Good luck!