السلام عليكم و رحمة لله و بركاته
أرجو منكم المساعدة في business cards project
قمت بكتابة 2 classes كالتالي:
هذا هو ال main class
public class MainCard
{
public static void main(String[] args)
{
CardOrder a = new CardOrder("Asma Khalid mohamed", 200);
System.out.println("Here is your card: ");
a.getTopLine();
a.getBlankLine();
a.getLineWithName();
a.getBlankLine();
a.getTopLine();
a.getFinalCost();
a.hasDiscount();
}
}الكلاس الثاني cardOrder:
public class CardOrder
{
private String name;
private int numCards;
private String initials;
private char border ='*';
private char space =' ';
private static final int CARD_LEN = 32;
public CardOrder(String fullName, int numberOfCards)
{
name = fullName;
numCards = numberOfCards;
if(!nameIsValid())
{
System.out.println("Please enter a valid name. A valid name is at least one word and not more than three.");
System.exit(1);
}
}
public String getInitials() {
String initials2="";
String nameToUpper = name.toUpperCase();
String[] splitName = nameToUpper.split(" ");
for (int i = 0; i < splitName.length; i++) {
initials = splitName.substring(0, 1);
initials2+=initials;
}
return initials2;
}
public String getFormattedName()
{ String name2="";
String nameToUpper = name.toUpperCase();
String nameToLower = name.toLowerCase();
String[] splitName = name.split(" ");
String[] splitNameToUpper = nameToUpper.split(" ");
String[] splitNameToLower = nameToLower.split(" ");
if (splitName.length <=2)
{
for(int i =0; i <splitName.length; i++)
{
String init = splitNameToUpper.substring(0,1);
String remainder = splitNameToLower.substring(1);
name = init + remainder + space;
}
name2+=name;
}
if (splitName.length == 3)
{
String init1 = splitNameToUpper[0].substring(0,1);
String remainder1 = splitNameToLower[0].substring(1);
String init2 = splitNameToUpper[1].substring(0,1);
String init3 = splitNameToUpper[2].substring(0,1);
String remainder3 = splitNameToLower[2].substring(1);
name = init1 + remainder1 + space + init2 + space + init3 + remainder3;
name2+=name;
}
return name2;
}
public boolean nameIsValid()
{
String[] splitName = name.split(" ");
int noOfWords = splitName.length;
if ((noOfWords >= 1) && (noOfWords <= 3))
{
return true;
}
else {
return false;
}
}
public void setNumCards(int n)
{
numCards=n;
}
public int getNumCards()
{
return numCards;
}
public String getTopLine()
{
String dotstr="";
for(int i =0; i <CARD_LEN-2*getInitials().length(); i++)
{
dotstr=dotstr + border;
}
String line = getInitials() + dotstr + getInitials();
System.out.println(line);
return line;
}
public String getBlankLine()
{
String spacestr="";
for(int i =0; i <CARD_LEN-2 ; i++)
{
spacestr+= space;
}
String line = border + spacestr + border;
System.out.println(line);
return line;
}
public String getLineWithName()
{
String line2 = "";
double whiteSpaceAvailable = (30 - getFormattedName().length())/2;
String spacestr="";
if(getFormattedName().length() % 2 == 0)
{
for(int i =0; i < whiteSpaceAvailable; i++)
{
spacestr+=space;
}
String line = border + spacestr + getFormattedName() +spacestr+ border;
line2+= line;
}
if(getFormattedName().length() % 2 != 0)
{
for(int i =0; i < whiteSpaceAvailable; i++)
{
spacestr+=space;
}
String line = border + spacestr + space + getFormattedName() +spacestr+ border;
line2+= line;
}
System.out.println(line2);
return line2;
}
public double getCardPrice()
{
double cost;
if (name.length() <= 12)
{
cost = 0.20;
}
else
{
cost = 0.25;
}
return cost;
}
public boolean hasDiscount()
{
if (numCards >= 100)
{
return true;
}
else
{
return false;
}
}
public double getFinalCost()
{
double finalCost;
if(!hasDiscount())
{
finalCost = (getNumCards() * getCardPrice());
System.out.println("The price of " + getNumCards() + " cards is AED " + finalCost);
System.out.println("No discount given");
}
else
{
finalCost = (getNumCards() * getCardPrice()) - (getNumCards() * getCardPrice() * 0.1);
System.out.println("The price of " + getNumCards() + " cards is AED " + finalCost);
System.out.println("10% discount applied");
}
return finalCost;
}
}الحمدلله البرنامح يعمل بشكل ممتاز و لكن لدي مشكلتين فقط ..الأولى انه اذا تم ادخل اسم اليوزر ك كلمتين يظهر في البطاقة الاسم الثاني فقط
يعني اذا ادخلت:
asma khalide
احصل على :
Here is your card:
AK****************************AK
* *
* Khalid *
* *
K******************************K
The price of 200 cards is AED 36.0
10% discount applied
و المشكلة الثانية ان سعر البطاقة finalCost يجب ان يكون with 2 decimal places
يعني في المثال السابق يجب ان يكون السعر 36.00
بانتظار مساعدتكم