私はまだ非常に学習者でもあるので、何を尋ねているのか完全にわからないときに質問するのは簡単ではないことを理解しています.
あなたの挑戦的な説明に基づいて、あなたが構築できるサンプルプログラムを作成しようとしました。理解/学習に役立つように非常に説明的にしようとしたので、私のコメントを読んでください。
public class Order {
//Constructor 2
public Order(String productName, int quantity) {
this.productName = productName;//Get the name
this.quantity = testQuantity(quantity); //Get the total quantity
this.orderPrice =getPrice(productName, quantity); //Get the price of the order
this.discountPrice = 0; //Should be set to a value, even though it won't be used in this constructor
orderNum++; //Another order in, up the count :)
displayOrder(productName, quantity, this.orderPrice, this.discountPrice); //Show order details
}
//Constructor 3
public Order(String productName, int quantity, int discount) {
this.productName = productName; //Get the name
this.quantity = testQuantity(quantity); //Get the total quantity
this.orderPrice = getPrice(productName, quantity); //Get the price of the order
this.discountPrice = testDiscount(discount, this.orderPrice); //Get the price if there is a discount
orderNum++; //Another order in, up the count :)
displayOrder(productName, quantity, this.orderPrice, this.discountPrice); //Show order details
}
クラスの最後にフィールドをいくつか追加して、例を簡単に表示できるようにしました。これは、いくつかのメソッドをどのように機能させたいかについて非常に確信が持てなかったためです。これらをコンストラクターに配置して初期化し、その理由についてコメントしました。
private void displayOrder(String productName, int quantity, int orderPrice, int discountPrice){
if(discountPrice == 0){ //If the discount is 0 then there is no discount so the discount price is the same as the order price
discountPrice = orderPrice;
}
// \n is called an escape character and when in a string creates a new line.
System.out.println("You have ordered: " + quantity + " " + productName + ("(s)") //Number and name of item ordered
+ "\nTotal cost: £" + orderPrice + "\nPrice with Discount (If applicable)= £" + discountPrice //Order price and discount price displayed
+ "\nOrder number: " + orderNum +"\n"); //Order Number
}
上記のメソッドは、いずれかのコンストラクターによって作成された注文のすべての詳細を表示し、その詳細を表示します。
private int testQuantity(int q){
//What you want to do in this method,
//For example
//System.out.println("You have ordered " + q + "Items");
return q;
}
このメソッドで何をしたいのかわからないので、空白のままにしました。利用可能なアイテムの総数を保存して数量を確認する必要がある場合、最適な方法はデータベースであり、これにはまったく異なる一連の学習が必要です。
private int testDiscount(int discount, int orderPrice){ //Will return a discounted price and store it in 'discountPrice' field
int reducedPrice = (orderPrice - discount);
return reducedPrice;
}
ユーザーが合計注文価格に加えて割引を持っている場合、割引価格を返します。どちらも、コンストラクターで作成した Order オブジェクトによって保持されます
private int getPrice(String productname, int quantity){
int price = 0;
switch(productName){ //Switch so you can find what item ordered and allocate the correct price
//Add cases for different items?
case "Sweater":
price = 10;
break;
default:
price = 0;
break;
}
int totalPrice = price * quantity; //Work out price by multiplying quantity of item by price determined by switch
return totalPrice;
}
上記のスイッチは、アイテムの価格を確認するための最良の方法である可能性があります。各ケースには、アイテムの名前と価格が保持されます。その価格に数量を掛けて、注文価格を作成します。
private String productName; //Name of product
private int quantity; //Quantity ordered
private int orderPrice; // The total order of the price
private int discountPrice; //Somewhere to store the price of an order with a discount
private static int orderNum; //This is static so that you it can be referenced even if no orders have been made
public static void main(String[] args){ //Test the ordering
Order order1 = new Order("Sweater", 2);
Order order2 = new Order("Sweater", 2, 5);
}
「メイン」メソッドから 2 つの注文を作成して、アプリケーションをテストします。
いじくり回して遊んでください。これがあなたの質問からあなたが望んでいたものかどうかはわかりませんが、うまくいけば、これがいずれかの方法で役立つことを願っています.