この記事の例は、参照のJavaショッピングシステムの設計と実装コードを共有しています。特定のコンテンツは次のとおりです
1。ショッピングシステムの要件分析と分類
ショッピングシステム自体は非常に複雑なシステムであり、多くの詳細と問題があり、それらを深く研究すればより複雑になります。さらに、一般的なショッピングシステムはWebページタイプであり、フレンドリーなインターフェイスが必要です。ただし、単純なプロジェクトとして、このプロジェクトは、Javaの初心者に開発の基本的なアイデアを紹介し、オブジェクト指向の場合にフレームワークと実装プロセスを設計する方法を紹介するためだけです。したがって、これは、GUIの参加なしに、日食に基づいて開発された単純なプロジェクトであり、多くの詳細と問題がフォローアップ研究として使用されます。全体的なデザインは比較的簡単ですが、多くのデザインのアイデアやデザインの概念を説明するには十分です。したがって、基本的なニーズを以下に分析します。
簡単なショッピングシステムとして、少なくとも次の機能が必要です(これらの機能はさまざまなレベルのメニューで分散されています):
(1)ユーザーログイン関数とユーザーアカウントのパスワードの変更機能、登録機能は当面は利用できません。
(2)ユーザーが正常にログインした後、彼は顧客情報管理機能、ショッピング決済機能、およびいくつかの宝くじ活動を持つ必要があります。
(3)以下など、顧客情報管理機能の下に分割できる多くの機能があります。クエリ、変更、追加など。
(4)次のようなショッピング決済機能の下に分割できる多くの機能があります。製品の選択、支払い、請求など。
(5)宝くじの下でさまざまな宝くじフォームを設計でき、さらに多くの新しい機能モジュールに分割できます。
(6)システムを終了する機能は、第1レベルのメニューで提供される必要があります。2番目のレベルのメニューからログアウトする機能は、第2レベルのメニューで提供され、他のメニューは前のメニューに戻ることができるはずです。
上記の機能はすべて比較的基本的な関数です。プロセス指向のアイデアに従って設計すると、多くの機能モジュールを分割してから、プロセスに段階的に従います。しかし、今、私たちはオブジェクト指向のアイデアを使用して設計しますが、デザインフレームワークをどのように検討する必要がありますか?オブジェクト指向の主なアイデアは、いくつかの要件を多くのクラスに抽象化し、これらのクラス間のつながりを確立することです。異なるクラス間の共同協力を通じて、すべての機能を達成できます。したがって、主なタスクは、これらのクラスを合理的に抽象化する方法、これらのクラスはどのような機能を実装するか、クラス間の接続は何ですか?以下は、この設計の構造に基づいたこのプロセスの分析です。
(1)StartSMSクラス:システム起動に使用。私たちのシステムには間違いなくスタートアップクラスが必要です。このクラスには、システムを開始する主な方法が含まれています。このクラスはトップレベルにあるため、根本的な詳細が多すぎることはありません。いくつかのトップレベルの基本プロセスを実装するだけです。主なことは、機能を実装するために他の基礎となるクラスのいくつかの方法を呼び出すことです。
(2)データクラス:すべてのデータ情報を保存するために使用されます。このデザインは、主に、購入および登録されたメンバー情報のために事前に保存された製品情報を保存します。なぜこのクラスが必要なのですか?それについて考えてみてください。オブジェクト指向のデザインでは、多くのデータがあり、どこでも散らばって定義および変更してはなりません。これにより、システムの集約が低くなり、多くのエラーが発生しやすくなり、後の関数拡張とエラーの変更を実行することは困難です。したがって、使用されるパブリックデータの一部を分類し、クラスに配置し、このクラスでこのデータを操作する方法を提供する必要があります。
(3)メニュークラス:すべてのレベルでメニューを表示および処理するために使用されます。ショッピングシステムを設計しているので、たとえそれがシンプルであっても、ユーザーと対話するために基本的なメニューが必要です。多くのメニューがあり、各レベルのメニューはレイヤーごとに接続されているため、メニューを均一に管理する必要があるため、メニューカテゴリが表示されます。ここのメニューは、いくつかのトップレベルのメニューディスプレイと基本的な関数呼び出しにすぎないことに注意してください。特定の基礎となるアルゴリズムは、より基礎となるクラスによってまだ実装する必要があります。
(4)マネージャークラス:ユーザーのアカウントとパスワードを保存するために使用されます。ユーザーにログインする必要があるため、システムがより独立しているように、ユーザーのアカウントとパスワードを管理するために別のクラスが必要になる必要があります。今回設計されたユーザーは、アカウントとパスワードのみが1つだけで、アカウントとパスワードに変更のみが許可されますが、登録は許可されていません。
(5)equalクラスの確認:ログイン情報の確認に使用されます。このクラスは、ログイン関数をクラスに抽象化することと同等です。この実装は実際にはそれほど必要ではありませんが、システム関数分割をより明確にするために、このクラスはログイン情報と既存のアカウントとパスワードを検証するように設計されており、それにより検証結果が得られます。
(6)顧客情報の管理に使用される顧客情報の管理。このクラスは、クエリ、変更、追加など、いくつかの根本的な機能を実装します。顧客情報管理メニューを入力するときは、顧客情報について多くの操作を行う必要があります。これらの操作の管理を促進し、その後の拡張を考慮に入れるために、顧客情報管理のすべての機能はここで抽象化され、このカテゴリに配置されます。前のメニューは、このクラスのメソッドを呼び出すことにより、顧客情報の管理を実現します。
(7)支払いクラス:ショッピングおよびチェックアウト操作の処理に使用します。このクラスの原則は、基本的に上記のクラスと同じです。顧客が買い物をすることを選択した場合、購入するもの、金額、支払い、変更などなど、多くの操作が必要です。これらの機能は比較的断片化されているため、このクラスを抽象化し、ショッピングと決済のメニューオプションの基礎となるアルゴリズムを実装します。前のメニューは、このクラスのメソッドを呼び出すことでショッピングと決済の機能を実現し、前のメニューに戻ることができます。
(8)ギフト管理クラス:宝くじの活動を処理するために使用される関連機能。このクラスの存在の理由は、基本的に(6)および(7)のクラスと同じです。このクラスは、宝くじの活動を均一に管理しています。前のメニューは、宝くじの機能を実現するために、このクラスの方法を呼び出すだけです。
(9)ギフトクラス:ギフトの管理に使用されます。宝くじセッションが設計されているため、贈り物は間違いなく必要です。それで、私たちはどんな贈り物を贈りますか?各ギフトを詳細にリストすることはできません。これは非常に冗長で面倒なので、ギフトクラスを抽象化し、ギフトの属性を保存するだけです。クラスのメンバー変数へのギフトの名前と価格です。その後、クラスを簡単に管理できます。ギフトが必要な場合は、新しいギフトオブジェクトを直接作成し、オブジェクトの属性を変更および管理します。この実装はインターフェイスに似ていますが、インターフェイスとはまったく異なり、同様の機能があります。
要するに、上記のクラスはいくつかの機能モジュールに分割された後に抽象化され、いくつかの場所は必ずしも合理的ではありません。主に要件を検討し、さまざまなニーズに応じてさまざまな計画を策定する必要があります。ここでは、「ギフトクラス」を再び強調したいと思います。このクラスの設計は、オブジェクト指向のアイデアと非常に一致しています。たとえば、携帯電話、コンピューター、モバイル電源などのショッピングシステムで多くのギフトが必要な場合、これらのギフトを1つずつ書くと、システムコードは非常に冗長になります。贈り物の属性は基本的に同じであるため、それらをクラスに抽象化することができます。そのため、ギフトが必要なときにのみ1つのオブジェクトを定義し、特定の属性を提供できます。たとえば、携帯電話やコンピューターが必要な場合は、ギフトクラスのオブジェクトを新しく新しい必要があるだけで、携帯電話が必要なときに携帯電話に属性を設定し、コンピューターが必要なときに属性をコンピューターに設定し、必要なものを設定します。これにより、コードが簡素化され、構造がより明確になります。より複雑なシステムでは、インターフェイスを使用してギフトを実装することが実際にはより合理的です。そのため、インターフェイスに従ってさまざまなギフトカテゴリを実装して、さまざまなニーズを満たすことができます。コンピューターのUSBインターフェイスに似ています。このインターフェイスを使用すると、さまざまな周辺機器の多くのデバイスを接続できます。真実は似ています。
2。ショッピングシステムのクラス間の関係とプロセス(グラフィック形式で示されています)
次の図は、Microsoft Office Visio 2003 Drawing Toolを使用したこれらの9つのクラスの関係を示しています。
上記の図から、さまざまなタイプ間の関係をはっきりと見ることができます。一般的な関係とプロセスは次のとおりです。
(1)StartSMSクラスはスタートアップクラスで、メインメソッドが含まれています。このクラスでは、データと検証情報を保存するために、equalクラスとデータクラスの検証クラスのオブジェクトを定義します。同時に、データクラスには、事前に保存されたユーザーアカウント情報を保存するマネージャークラスが含まれています。次に、メインメソッドの特定のロジックを介して、メニュークラスのshowloginmenu()メソッドが呼び出され、最初のレベルのメニューであるロジン変更プロセスを処理します。
(2)ログインが成功した場合は、メニュークラスのshowmainmenu()メソッドを呼び出して、セカンドレベルのメニューのメインプロセスであるショッピングシステムを処理します。ログインが3回失敗した場合、システムを直接終了します。
(3)メニュークラスのshowmainmenu()メソッドでは、さまざまなセカンダリメニューオプションを選択することにより、メニュークラスのshowcustmmenu()メソッドが顧客情報管理プロセスを処理するか、メニュークラスのshowsendmenu()メソッドを呼び出すために呼び出され、宝くじプロセスを処理するか、ショッピングクラスのcalcprice()メソッドを呼び出します。
(4)セカンダリメニューの顧客情報管理オプションが選択されている場合、メニュークラスのshowcustmmenu()メソッドが呼び出されます。この方法では、顧客情報管理のさまざまな操作を処理するために、クストマネージメントクラスのさまざまな方法を呼び出します。
(5)セカンダリメニューのショッピング決済オプションが選択されている場合、買い物の決済プロセスを処理するために、給与クラスのCalcprice()メソッドが呼び出されます。賃金クラスのgetDiscount()メソッドは、顧客メンバー情報に基づいて割引率を計算するために使用されることに注意してください。
(6)セカンダリメニューの誠実なフィードバックオプションが選択されている場合、つまり宝くじが選択されている場合、メニュークラスのshowsendmenu()メソッドが呼び出されます。この方法では、宝くじのさまざまな操作を処理するために、ギフト管理クラスのさまざまな方法を呼び出します。
CustrastMenu()メソッドが、前のメニューに戻るために使用されるCustrastMenu()メソッドがあります。
3。コード実装
注意すべきことの1つは、これらのコードをCN.Itcastパッケージに配置する必要があることです。
3.1 StartSMSクラス
パッケージcn.itcast; Java.util.scannerをインポートします。 / ** *このクラスはこのシステムのメインメソッドクラスであり、ショッピングシステムを開始するために使用されます * * @author * * */ public class startsms {/ ** * empty constructor * */ public startsms(){}/ ** * system method * * @param args */ public static void main(String args []){//既存のデータを作成します。 data.Initial(); //メニュークラスのオブジェクトを作成メニューメニュー= new Menu(); //ここで、初期化された既存のデータ情報はメニューオブジェクトメニューに送信されます。SetData(data.goodsname、data.goodsprice、data.custno、data.custbirth、data.custscore); //最初のレベルのメニュー、つまりログインインターフェイスメニューを表示します。showloginmenu(); //このフラグは、システム操作エラーが発生したかどうかを判断するために使用されます。操作が正しくない場合、システムを終了するためにフラグが誤っています。デフォルトはエラーのないブールフラグ= trueです。 //システム全体のプロセスを処理する{//操作エラーが発生し、(!flag)breakの場合はシステムを終了します。 //アカウントに記録されたユーザーとパスワードにログインしているかどうかを確認するオブジェクトを作成します。ここではオブジェクトのみが作成され、検証方法の検証は実行されません。 equal = new verifyequal(); //第1レベルのメニュースキャナースキャナー= new Scanner(System.in)に選択を入力します。 int i = scanner.nextint(); //第1レベルのメニューの選択に対して異なる応答を行います。これは、クラシックスイッチケースの使用スイッチ(i){ケース1://ユーザーが「ログインシステム」を選択します。//カウンターを定義します。 //システムにログインするためのプロセスの処理{if(filefifyequal.verify(data.manager.username、data.manager.password)){//ユーザーが正常にログインし、2レベルのショッピングメニューが表示されます! ! ! menu.showmainmenu();壊す; } if(j!= 1){//ユーザー入力が正しくなく、3回到達していません。 system.out.printlnを再入力します( "/nユーザー名とパスワードが一致しません。再入力してください:"); } else {// 3つの試行が終了し、exitフラグを設定し、do-while loop system.out.println( "/nyouはシステムに入る許可がありません!ありがとう!"); flag = false;壊す; } //入力するたびにカウンターを1倍にして、j--; } while(true);壊す;ケース2://ユーザーは「管理者情報の変更」を選択します(equal.verify(data.manager.username、data.manager.password)){//新しい情報を入力する前に、元の情報を確認する必要があります。これは、検証が成功したことを意味します。 System.out.print( "新しいユーザー名を入力してください:"); data.manager.username = scanner.next(); System.out.print( "新しいパスワードを入力してください:"); data.manager.password = scanner.next(); System.out.println( "ユーザー名とパスワードが変更されました!"); //情報は正常に変更されました。次の操作System.out.println( "/nplease Selectを選択し、番号を入力してください:"); } else {//情報の検証が失敗し、exitフラグシステム.out.printlnを設定します( "申し訳ありませんが、変更する許可がありません!"); flag = false; } 壊す;ケース3://ユーザーは "Exit" system.out.println( "ありがとうございます!"); System.Exit(0);壊す;デフォルト://第1レベルのメニュー入力が正しくないため、system.out.print( "/n入力が正しくありません!もう一度選択して番号を入力してください:");壊す; }} while(flag); }}3.2データクラス
パッケージcn.itcast; /***ショッピングシステムの初期化データを保存するデータクラス。このクラスは、既存の製品情報と顧客情報のみを保存します * * @author * * / public classデータ{ / ** *デフォルトのコンストラクター、初期化変数。それらはすべて配列オブジェクトまたはクラスオブジェクトであるため、それらはすべてnew * */ public data(){goodsname = new String [50]を使用する必要があります。 goodsprice = new double [50]; custno = new int [100]; custbirth = new String [100]; custscore = new int [100]; Manager = new Manager(); } / ***このクラスのデータを初期化** / public void initial(){ /*========================================================================= =====================================================/ goodsname [0] = "addidasスニーカー"; goodsprice [0] = 880d; GoodsName [1] = "Kappa Tennis Skirt"; goodsprice [1] = 200d; GoodsName [2] = "Top Racket"; goodsprice [2] = 780d; goodsname [3] = "addidas Tシャツ"; goodsprice [3] = 420.77999999999999d; goodsname [4] = "nikeスニーカー"; goodsprice [4] = 900d; GoodsName [5] = "Kappa Tennis"; goodsprice [5] = 45d; GoodsName [6] = "Kappat-Shirt"; goodsprice [6] = 245d; /*========================================================================================== =========================================================================================== =========================================================================================== =========================================================================================== Custno [1] = 1711; custbirth [1] = "07/13"; custscore [1] = 4000; Custno [2] = 1623; custbirth [2] = "06/26"; custscore [2] = 5000; Custno [3] = 1545; custbirth [3] = "04/08"; custscore [3] = 2200; Custno [4] = 1464; custbirth [4] = "08/16"; custscore [4] = 1000; Custno [5] = 1372; custbirth [5] = "12/23"; custscore [5] = 3000; Custno [6] = 1286; custbirth [6] = "12/21"; custscore [6] = 10080; } /*================================================================================================== ============================================================================================================================================== ============================================================================================================================================== ==============================================================================================================================================顧客の誕生日public int custscore []; //顧客のポイントパブリックマネージャーマネージャー。 //管理者クラス、管理者のユーザー名とパスワードのみ}3.3マネージャークラス
パッケージcn.itcast; / ** *管理者クラス、管理者のユーザー名とパスワードのみを保存します * * @author * */ public class Manager {/ ** *デフォルトのユーザー名とパスワードを設定 * * */ public Manager(){username = "itcast";パスワード= "itcast"; } /*==================================================================================== class ============================================================================================================================== ============================================================================================ ============================================================================================ ===========================================================================================3.4 equalクラスを確認します
パッケージcn.itcast; Java.util.scannerをインポートします。 / ****ログインしたユーザーが正しいかどうかを検証するクラス** @author***/ public class equal {/ *** empty constructor***/ public equal(){}/ *** method foremifilifince** @param s検証のための正しいユーザー名* @param s username system.out.print( "ユーザー名を入力してください:");スキャナースキャナー=新しいスキャナー(System.in);文字列s2 = scanner.next(); //ユーザーSystem.out.printからパスワードを入力します( "パスワードを入力してください:"); scanner = new scanner(system.in);文字列s3 = scanner.next(); //ユーザーが入力した情報が既存の情報を返すことと一致しているかどうかを判断しますs2.equals(s)&& s1.equals(s3); }}3.5メニュークラス
パッケージcn.itcast; Java.util.scannerをインポートします。 / ** *メニュークラス、ユーザー選択のためにすべてのレベルのメニューを表示するために使用 * * @author * */ public classメニュー{/ ** *空のコンストラクター * */パブリックメニュー(){}/ ** * * @param as * @param ad * @param ai * @param ai * @param ai * @param ai1 */ public ai1 */ param ai * @param ai * @param ai * ad []、int ai []、string as1 []、int ai1 []){goodsname = as; goodsprice = ad; custno = ai; custbirth = as1; custscore = ai1; }/***最初のレベルのメニュー、つまり、ログインインターフェイス**/public void showloginmenu(){system.out.println( "/n/n/n/t/t/t/t歓迎のショッピング管理システムバージョン1.0/n/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *。 System.out.println("/t/t/t/t/t 3. Exit/n/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」このメソッドには、このメニューを処理するためのすべてのプロセスが含まれています * */public void showmainmenu(){//システムシステムのメインメニューを表示します。 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *ユーザーは、サービスアイテムを選択しますboolean falsは、{customer情報管理( "1"))を選択します。ショッピングプロセスは、new.setdata(goodsprice、custscore)を処理します( "4"){//最初のレベルのメニューに戻ります。 void showcustmenu(){system.out.println( "ショッピング管理システム>顧客情報管理/n"); * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * System.out.println( "/t/t/t 2。顧客情報/n"); * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *ここでのデータは、顧客の顧客、custno、custscore、custscore.next(); ( "2"){custmanagemed()// custmanagemed.modify() ( "n"){showmainmenu()。 System> true feedback/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * lottery/n "); system.out.println("/t/t/t/t/t 3。誕生日挨拶/n ") * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *」 * * * system./ここでのデータを設定します( "1"){giftmanagemed.// "giftmanagement.sendluckycust() 「前のメニュー」( "n"){showmainmenu()} println( "); /*============================================================= []; // customer membership number public String custBirth []; // customer's birthday public int custScore []; // customer's points}3.6 CustManagement类
package cn.itcast; import java.util.Scanner; /** * Customer information management class* * @author * */ public class CustManagement { /** * Empty constructor* */ public CustManagement() { } /** * Set data information of customer information management class* * @param as * @param ad * @param ai * @param ai * @param as1 * @param ai1 */ public void setData(String as[], double ad[], int ai[], String as1[], int ai1[]) { goodsName = as; goodsPrice = ad; custNo = ai; custBirth = as1; custScore = ai1; } /** * Return to the previous menu, i.e. the second-level menu-customer information management menu* */ public void returnLastMenu() { System.out.print("/n/nPlease press 'n' to return to the previous menu:"); Scanner scanner = new Scanner(System.in); boolean flag = true; do if (scanner.next().equals("n")) { // Return to the previous menu, a new menu object was created here, but in the user's opinion, the same processing flow is actually the same, // However, for the program, a new secondary menu processing process has begun. Menu menu = new Menu(); menu.setData(goodsName, goodsPrice, custNo, custBirth, custScore); menu.showCustMMenu(); } else { System.out.print("Input error, please 'n' return to the previous menu: "); flag = false; } while (!flag); } /** * Add customer information* */ public void add() { System.out.println("Shopping Management System> Customer Information Management> Add customer information/n/n"); Scanner scanner = new Scanner(System.in); System.out.print("Please enter the member number (<4-digit integer>):"); int i = scanner.nextInt(); System.out.print("Please enter the member's birthday (month/day <denoted in two digits>):"); String s = scanner.next(); System.out.print("Please enter the integral:"); int j = scanner.nextInt(); int k = -1; int l = 0; do { if (l >= custNo.length) break; // Find the first empty location in the array to store new customer information if (custNo[l] == 0) { k = l;壊す; } l++; } while (true); custNo [k] = i; custBirth[k] = s; custScore[k] = j; System.out.println("New member added successfully!"); // Return to the previous menu returnLastMenu(); } /** * Modify customer information* */ public void modify() { System.out.println("Shopping Management System> Customer Information Management> Modify customer information/n/n"); System.out.print("Please enter the member number:"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); System.out.println(" member number birthday points"); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- .append(custScore[k]).toString()); j = k; break; } k++; } while (true); // The member exists, then modify the information process if (j != -1) { System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /n"); System.out.println("/t/t/t1. Modify the member's birthday./n"); System.out.println("/t/t/t/t2.Modify member points./n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /n"); System.out.print("Please select, enter the number: "); switch (scanner.nextInt()) { case 1: // "Modify member's birthday" System.out.print("Please enter the modified birthday: "); custBirth[j] = scanner.next(); System.out.println("Birthday information has been changed!"); break; case 2: // "Modify member points" System.out.print("Please enter the modified member points: "); custScore[j] = scanner.nextInt(); System.out.println("Member points have been changed!"); break; } } else { System.out.println("Sorry, there is no member you query."); } // Return to the previous menu returnLastMenu(); } /** * Query customer information* */ public void search() { System.out.println("Shopping Management System> Customer Information Management> Query Customer Information/n"); Scanner scanner = new Scanner(System.in); for (String s = "y"; s.equals("y"); s = scanner.next()) { System.out.print("Please enter the membership number: "); int i = scanner.nextInt(); System.out.println("Member number birthday points"); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0; do { if (j >= custNo.length) break; // Show the information of the member if (custNo[j] == i) { System.out.println((new StringBuilder()).append(custNo[j]) .append("/t/t").append(custBirth[j]).append("/t/t") .append(custScore[j]).toString()); flag = true; break; } j++; } while (true); if (!flag) System.out.println("Sorry, there is no member information you query."); System.out.print("/nDo you want to continue querying (y/n):"); } // Return to the previous menu returnLastMenu(); } /** * Show all customer information* */ public void show() { System.out.println("Shopping Management System> Customer Information Management> Show customer information/n/n"); System.out.println("Member number birthday points"); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println((new StringBuilder()).append(custNo[j]).append( "/t/t").append(custBirth[j]).append("/t/t").append(custScore[j]).toString()); // Return to the previous menu returnLastMenu(); } /*======================================================================================================= ========================================================================================================= ========================================================================================================= ========================================================================================================= custNo []; // Customer’s membership number public String custBirth []; // Customer’s birthday public int custScore []; // Customer’s points}3.7 Pay类
package cn.itcast; import java.util.Scanner; /** * Class that handles customers' purchases and settlements* * @author * */ public class Pay { /** * Empty constructor* */ public Pay() { } /** * Set data information of shopping settlement class* * @param as * @param ad * @param ai * @param ai * @param as1 * @param ai1 */ public void setData(String as[], double ad[], int ai[], String as1[], int ai1[]) { goodsName = as; goodsPrice = ad; custNo = ai; custBirth = as1; custScore = ai1; } /** * Determine the discount rate based on member information* * @param i * @param ai * @param ai * @param ai1 * @return */ public double getDiscount(int i, int ai[], int ai1[]) { int j = -1; int k = 0; do { if (k >= ai.length) break; if (i == ai[k]) { j = k;壊す; } k ++; } while (true); double d; if (ai1[j] < 1000) d = 0.94999999999999999999999996D; else if (1000 <= ai1[j] && ai1[j] < 2000) d = 0.900000000000000000000002D; else if (2000 <= ai1[j] && ai1[j] < 3000) d = 0.8499999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999999999996D; else if (1000 <= ai1[j] && ai1[j] < 4000) d = 0.8000000000000000000004D; else if (4000 <= ai1[j] && ai1[j] < 6000) d = 0.75D; else if (6000 <= ai1[j] && ai1[j] < 8000) d = 0.69999999999999999999996D; else d = 0.5999999999999999998D; d; } /** * Key methods of this class, used to handle shopping and checkout* */ public void calcPrice() { String s2 = ""; double d1 = 0.0D; double d2 = 0.0D; int l = 0; double d4 = 0; System.out.println("Shopping Management System> Shopping Checkout/n/n"); System.out.println("************************************************"); System.out.println("Please select the purchased product number: "); // Show all purchaseable product information, the information here is the initial initial product data for (l = 0; l < goodsName.length && goodsName[l] != null; l++) { d4++; System.out.println((new StringBuilder()).append(d4).append(": ") .append(goodsName[l]).append("/t").toString()); } System.out.println("*********************************************/n"); Scanner scanner = new Scanner(System.in); System.out.print("/tPlease enter the member number: "); int i = scanner.nextInt(); // Get discount information based on member information d4 = getDiscount(i, custNo, custScore); String s1; do { System.out.print("/tPlease enter the product number: "); int j = scanner.nextInt(); System.out.print("/tPlease enter the number: "); int k = scanner.nextInt(); double d = goodsPrice[j - 1]; String s = goodsName[j - 1]; d1 += d * (double) k; s2 = (new StringBuilder()).append(s2).append("/n").append(s) .append("/t").append("¥").append(d).append("/t/t") .append(k).append("/t/t").append("¥") .append(d * (double) k).append("/t").toString(); System.out.print("/t continue (y/n)"); s1 = scanner.next(); } while (s1.equals("y")); d2 = d1 * d4; // Total price after discount System.out.println("/n"); System.out.println("***************************************************************************************************************************** System.out.println("Item/t/t unit price/t/t number/t/t amount/t amount/t"); System.out.print(s2); System.out.println((new StringBuilder()).append("/n discount: /t").append(d4).toString()); System.out.println((new StringBuilder()).append("Total amount:/t¥").append(d2).toString()); System.out.print("Actual payment:/t¥"); double d3 = scanner.nextDouble(); System.out.println((new StringBuilder()).append("Find money:/t¥").append(d3 - d2).toString()); int i1 = ((int) d2 / 100) * 3; int j1 = 0; do { if (j1 >= custNo.length) break; if (custNo[j1] == i) { custScore[j1] = custScore[j1] + i1; System.out.println((new StringBuilder()).append("The points obtained in this shopping are: ") .append(i1).toString()); break; } j1++; } while (true); System.out.print("/nPlease 'n' to return to the previous menu:"); if (scanner.next().equals("n")) { // Return to the previous menu, a new menu object was created here, but in the user's opinion, the same processing flow was actually still the same, // However, for the program, a new secondary menu processing flow has started. Menu menu = new Menu(); menu.setData(goodsName, goodsPrice, custNo, custBirth, custScore); menu.showMainMenu(); } } /*=====================================================================================*/ public String goodsName []; // product name public double goodsPrice[]; // product price public int custNo []; // customer membership number public String custBirth []; // customer's birthday public int custScore []; // customer's points}3.8 GiftManagement类
package cn.itcast; import java.util.Scanner; /** * Gift management class* * @author * */ public class GiftManagement { /** * Empty constructor* */ public GiftManagement() { } /** * Set data information of gift management class* * @param as * @param ad * @param ai * @param ai * @param as1 * @param ai1 */ public void setData(String as[], double ad[], int ai[], String as1[], int ai1[]) { goodsName = as; goodsPrice = ad; custNo = ai; custBirth = as1; custScore = ai1; } /** * Return to the previous menu, that is, the second-level menu-true feedback menu* */ public void returnLastMenu() { System.out.print("/n/nPlease press 'n' to return to the previous menu:"); Scanner scanner = new Scanner(System.in); boolean flag = true; do if (scanner.next().equals("n")) { // Return to the previous menu, a new menu object was created here, but in the user's opinion, the same processing flow is actually the same, // However, for the program, a new secondary menu processing process has begun. Menu menu = new Menu(); menu.setData(goodsName, goodsPrice, custNo, custBirth, custScore); menu.showCustMMenu(); } else { System.out.print("Input error, please 'n' return to the previous menu: "); flag = false; } while (!flag); } /** * Birthday greeting* */ public void sendBirthCust() { System.out.println("Shopping Management System> Birthday greeting/n/n"); System.out.print("Please enter today's date (month/day <indicated in two digits>): "); Scanner scanner = new Scanner(System.in); String s = scanner.next(); System.out.println(s); String s1 = "";ブールフラグ= false; for (int i = 0; i < custBirth.length; i++) if (custBirth[i] != null && custBirth[i].equals(s)) { s1 = (new StringBuilder()).append(s1).append(custNo[i]).append( "/n").toString(); flag = true; } // The gift here is fixed, so there is no gift class if (flag) { System.out.println("Birthday members are:"); System.out.println(s1); System.out.println("Congratulations! I received one MP3!"); } else { System.out.println("Members with no birthday today!"); } // Return to the previous menu returnLastMenu(); } /** * Lucky lottery, note that this is a random lottery, so you need a random number. You only need to formulate a lottery rule by yourself* */ public void sendLuckyCust() { System.out.println("Shopping Management System> Lucky lottery/n/n"); System.out.print("Does it start (y/n):"); Scanner scanner = new Scanner(System.in); if (scanner.next().equals("y")) { int i = (int) (Math.random() * 10D); // Generate a random number String s = "";ブールフラグ= false; for (int k = 0; k < custNo.length && custNo[k] != 0; k++) { // Compare the random number with the corresponding results of the customer membership number to determine whether a customer wins int j = (custNo[k] / 100) % 10; if (j == i) { s = (new StringBuilder()).append(s).append(custNo[k]) .append("/t").toString(); flag = true; } } // Fixed prize, so there is no need for gifts if (flag) System.out.println((new StringBuilder()).append("Lucky customer gets MP3:") .append(s).toString()); else System.out.println("No lucky customers."); } // Return to the previous menu returnLastMenu(); } /** * Lucky big broadcast, the member with the highest points will be the lucky one, and the prize will be given * */ public void sendGoldenCust() { System.out.println("Shopping Management System> Lucky big broadcast/n/n"); int i = 0; int j = custScore[0]; for (int k = 0; k < custScore.length && custScore[k] != 0; k++) { // Find the member with the highest points if (custScore[k] > j) { j = custScore[k]; i = k; } } System.out.println((new StringBuilder()).append("The member with the highest points is: ").append( custNo[i]).append("/t").append(custBirth[i]).append("/t") .append(custScore[i]).toString()); // Create a gift class and set the gift information. The gift here is a fixed information Gift gift = new Gift(); gift.name = "Apple laptop"; gift.price = 12000D; System.out.print("Congratulations! Received a gift: "); System.out.println(gift); // Return to the previous menu returnLastMenu(); } /*================================================================================================================= ==================================================================================================================== =================================================================================================================== ==================================================================================================================== Customer's points}3.9 Gift类
package cn.itcast; /** * Class used to store gifts in true feedback*/ public class Gift { /** * Empty constructor* */ public Gift() { } /** * Return all the information of the gift based on the variables of the gift object* */ public String toString() { return (new StringBuilder()).append("a value ¥").append(price) .append("").append(name).toString(); } /*======================== Define the variables owned by this class=================================================================================== ================================================================================================================== ================================================================================================================== ================================================================================================================== // Gift name public double price; // Gift price}3.10 代码总结
从上面的9个类的代码来看,有一些需要注意的地方:
(1)在许多类中都定义了与Data中基本一样的成员变量,只是没有Manager对象而已,这是为了让数据一层层保存和传递,通过setData()方法实现,不过这种方法其实并不是很好,并且一般来说成员变量应该最好设置为私有的,这里这样的设计是为了操作方便,使系统更简单一些,安全性不好。
(2)注意到所有要进行字符串拼接的地方都使用JAVA中的StringBuilder类,这是为了高效处理字符串拼接,防止String类带来的拼接数据冗余。
(3)这些设计中的流程并不是非常合理,大家可以自己的需要进行修改。
(4)代码中基本上没有考虑异常时的处理,所以当输入时出现错误的时候,普通错误可以进行重新输入,但是如果出现不匹配等错误,直接会出现异常,从而退出系统,这些也是本设计的缺陷,可以通过正则表达式等来完善一些。
总之,给出的代码仅供参考,大家可以根据需要进行修改,这些代码都是经过验证的,可以直接运行。
4。概要
这个设计只是为了说明一些基本的设计思想和设计理念,以及设计过程中需要考虑的问题,主要还是为了说明怎样用面向对象的思想去解决现实生活中的问题,所以设计相对简单,不过希望大家可以通过这个设计理解这些基本的思想,从而帮助大家理想面向对象的基本思想。
In short, language is just a tool to solve problems. You can use other languages such as C++ and C# to implement this system, as long as you have good design concepts and design ideas. Again, this design is for reference only. Everyone is welcome to participate in the discussion. If there are any errors, please correct me.ありがとう。