我目前正在处理一个分配给我的项目,我在尝试找出应该如何开始时遇到了一些麻烦。这个任务是使用类重写一个程序,而不是只是把所有东西都放到主程序中。这是我创建的必须重写的代码:
public class Lab14 {
public static void main(String[] args) {
int Numbook1=0;
int Numbook2=0;
int Numbook3=0;
double priceOfDesignPatterns=32.46;
double priceOfEffectiveJava=35.48;
double priceOfJavaPuzzlers=27.86;
String book1="Design Patterns";
String book2="Effective Java";
String book3="Java Puzzlers";
final double SALES_TAX=.065;
String choice;
int finished=0;
Scanner input=new Scanner(System.in);
System.out.println("Welcome to ECommerce!");
System.out.println("The books we have in stock are: ");
System.out.println(book1+", "+book2+", "+book3);
System.out.println("Please enter the name of the book you wish to purchase. Press 'q' to quit.");
while (finished==0)
{
choice=input.nextLine();
if (choice.equals(book1))
{
System.out.println("How many "+book1+"(s) do you wish to buy?");
Numbook1=(input.nextInt())+Numbook1;
System.out.println(Numbook1+" copies of "+book1+" are in your cart.");
System.out.println("Enter the name of the next book you would like to purchase, or press 'q' to quit.");
}
else if (choice.equals(book2))
{
System.out.println("How many "+book2+"(s) do you wish to buy?");
Numbook2=(input.nextInt())+Numbook2;
System.out.println(Numbook2+" copies of "+book2+" are in your cart.");
System.out.println("Enter the name of the next book you would like to purchase, or press 'q' to quit.");
}
if (choice.equals(book3))
{
System.out.println("How many "+book3+"(s) do you wish to buy?");
Numbook3=(input.nextInt())+Numbook3;
System.out.println(Numbook3+" copies of "+book3+" are in your cart.");
System.out.println("Enter the name of the next book you would like to purchase, or press 'q' to quit.");
}
if (choice.equals("q"))
{
double total=0;
double totalTax=0;
total=(Numbook1*priceOfDesignPatterns)+(Numbook2*priceOfEffectiveJava)+(Numbook3*priceOfJavaPuzzlers);
totalTax=total*SALES_TAX;
System.out.println("Your cart contains: ");
System.out.println(Numbook1+" "+book1+"(s), "+Numbook2+" "+book2+"(s), "+Numbook3+" "+book3+"(s).");
System.out.println("Sales Tax: $"+totalTax);
System.out.println("Your total cost is: $"+(totalTax+total));
finished=1;
}
}
}
}
现在我知道我必须编写几个类来构建book1,2,3
并设置价格并在以后访问它们,但我真的迷失了如何启动流程并将所有内容汇总在一起。如果有人能指导我朝着正确的方向前进,我们将不胜感激!非常感谢你。