hi i need help with this program. the book gives me this program with no main cl
ID: 3628424 • Letter: H
Question
hi i need help with this program.the book gives me this program with no main class.
public class TShirt {
public TShirt(String size) {
switch ( size.charAt(0) ) {
case 'S': case 'M': case 'L':
myPrice = 10.00;
break;
case 'X':
switch ( size.charAt(1) ) {
case 'S':
myPrice = 9.00;
break;
case 'L':
myPrice = 11.00;
break;
default:
System.err.println("tShirtPrice(size): size ""
+ size + "" is invalid");
System.exit(1);
}
break;
default:
System.err.println("tShirtPrice(size): size ""
+ size + "" is invalid");
System.exit(1);
}
mySize = size;
}
public double getPrice() { return myPrice; }
public String getSize() { return mySize; }
public String toString() { return mySize + " ($" + myPrice + ")"; }
private String mySize;
private double myPrice;
}
then it asks the following:
Expand the TShirt class from figure 10-7 by adding a Color instance variable, modifying the constructor to accept a Color, writing an accessor method to retrieve a TShirt object's Color. Then write a TShirtsOrder program that reads one or more shirt values from the keyboard, and then prints an invoice, listing the number of shirts of each size, the subtotal, any sales tax(for example, 6%), and the total.
-well i modified the TShirt class like this :
public class TShirt {
private double myPrice;
private String mySize;
private String myColor;
private int myCounter;
public TShirt(String size,String color, int number){
switch (size.charAt(0)){
case 'S': case 's':case 'M': case 'm':case 'L':case'l':
myPrice = 10.00;
mySize = size;
myColor = color;
myCounter = number;
break;
case 'X': case'x':
switch(size.charAt(1)){
case 'S':case's':
myPrice = 9.00;
mySize = size;
myColor = color;
myCounter = number;
break;
case 'L':case'l':
myPrice = 11.00;
mySize = size;
myColor = color;
myCounter=number;
break;
default:
System.err.println("TShirt(size): size""+ size+"" is invalid");
}break;
default:
System.err.println("TShirt(size): size""+ size+"" is invalid");
}
}
public String getSize(){
return mySize;}
public double getPrice(){
myPrice = myPrice * myCounter;
return myPrice;}
public String getColor(){return myColor;}
public String toString(){return myCounter+" "+mySize +" "+myColor+" T-Shirt price is: " +" $"+getPrice() ;}
public void total(){
}
}
-and wrote the TShirtsOrder class :
import java.util.Scanner;
public class TShirtsOrder {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String Decision;
System.out.println("Please enter the size of the Tshirt : ");
String size = input.nextLine();
System.out.println("Please enter the color of the Tshirt : ");
String color = input.nextLine();
System.out.println("Please enter the quantity of Tshirt : ");
int number = input.nextInt();
TShirt tshirt = new TShirt(size,color,number);
System.out.println(tshirt.toString());
System.out.println("place other order? : 'yes' or 'no'");
Decision= input.next();
while(Decision=="yes"){
System.out.println("Please enter the size of the Tshirt : ");
size = input.next();
System.out.println("Please enter the color of the Tshirt : ");
color = input.next();
System.out.println("Please enter the quantity of Tshirt : ");
number = input.nextInt();
tshirt = new TShirt(size,color,number);
System.out.println(tshirt.toString());
System.out.println("place other order? : yes or no");
Decision = input.next();
}
System.exit(0);
}
}
im stuck here... i dont know how to accumulate the total price and
make the program at the end print a ticket like this:
3 medium red Tshirts = $30.0
4 large green Tshirts = $40.0
3 small red Tshirts = $30.0
5 xl white Tshirts = $55.0
(at the end of program when u type dont want place other order)
subtotal = $155.0
tax = $9.3
total = $164.3
thx a lot for ur help
Explanation / Answer
please rate - thanks
note the changes in the class
use an array--I changed most of the main-keeping it true to your code
public class TShirt {
private double myPrice;
private String mySize;
private String myColor;
private int myCounter;
public TShirt(String size,String color, int number){
switch (size.charAt(0)){
case 'S': case 's':case 'M': case 'm':case 'L':case'l':
myPrice = 10.00;
mySize = size;
myColor = color;
myCounter = number;
break;
case 'X': case'x':
switch(size.charAt(1)){
case 'S':case's':
myPrice = 9.00;
mySize = size;
myColor = color;
myCounter = number;
break;
case 'L':case'l':
myPrice = 11.00;
mySize = size;
myColor = color;
myCounter=number;
break;
default:
System.err.println("TShirt(size): size""+ size+"" is invalid");
}break;
default:
System.err.println("TShirt(size): size""+ size+"" is invalid");
}
}
public String getSize(){
return mySize;}
public double getPrice(){
return myPrice* myCounter;}
public String getColor(){return myColor;}
public String toString(){return myCounter+" "+mySize +" "+myColor+" T-Shirt = " +" $"+getPrice() ;}
public void total(){
}
}
------------------------------------
import java.util.Scanner;
public class TShirtsOrder {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String Decision="yes";
String size,color;
double total=0,tax,taxrate=6/100.;
int number,i=0,j;
TShirt[] tshirt=new TShirt[10];
while(Decision.compareToIgnoreCase("yes")==0){
System.out.println("Please enter the size of the Tshirt : ");
size = input.next();
System.out.println("Please enter the color of the Tshirt : ");
color = input.next();
System.out.println("Please enter the quantity of Tshirt : ");
number = input.nextInt();
input.nextLine(); //empty buffer
tshirt[i] = new TShirt(size,color,number);
System.out.println("place other order? : yes or no");
Decision = input.nextLine();
i++;
}
for(j=0;j<i;j++)
{ System.out.println(tshirt[j].toString());
total+=tshirt[j].getPrice();
}
System.out.println("subtotal=$"+total);
tax=total*taxrate;
System.out.println("tax=$"+tax);
total+=tax;
System.out.println("total=$"+total);
System.exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.