In this program you will sort the contents of a text file and provide the abilit
ID: 3836416 • Letter: I
Question
In this program you will sort the contents of a text file and provide the ability for a user to search within the sorted result. The input file is an unsorted listing of 1,000 fictional bank account numbers and their corresponding balances. The first field in the file represents the account number followed by a space and then the balance. The input file Final Random Accounts Unsorted.txt is located as an attachment to this assignment. Steps required to complete the program: 1) The first step is to read the values in from the input file and store them in appropriate data structures within your program. Iwould recommend using arrays or vectors. 2) Next you should sort the values from the input file in ascending order by account number keeping the corresponding balance paired with the account number. You may use a selection sort or bubble sort algorithm. You cannot use the sorting algorithms within the standard template library 3) Write the sorted contents to another file with the name Final Random Accounts sorted Last First.txt where Last and First is your last and first name. This will be sent to me later along with your source code 4) Prompt the user for an account number to search. You must implement the search using a binary search algorithm. If the account number is found then display the corresponding account number and balance, otherwise display an appropriate error message. 5) It is not necessary to provide any formatting with your output. The floating point values from the file are in two-digit decimal precision already which is sufficient for this lab In addition: You must segment your program into multiple functions. Do not implement your entire program embedded within a single main() method. Complex problems are better solved in smaller parts. Use your own judgment regarding the functional decomposition of the problem. While there is no correct amount of functions, somewhere in the four to eight range should work to solve this problem e comments where appropriate. Do not use global variables. However, you may use global constants. Indent your code where applicable. Unlike your compiler, proper use of whitespace greatly helps me in the comprehension of your program. If I cant understand it, then l cant properly grade it! Use meaningful identifier names. (i.e. dont use variable names such as x, x1, etc.) Be careful when sorting the accounts to make certain the corresponding balance is maintained for each account 100640 8909.06 100743 2479.37 100362 8652.25 100044 6638.90 100631 2612.81 100844 8058.44 100700 1268.93 100882 1258.15 100947 7381.69 100391 9691.02 100513 3090.72 100651 3577.06 100040 784.40 100647 4113.57 100369 2461.34 100975 6763.19 100663 6847.65 100336 5606.53 100435 7180.63 100853 7108.63 100627 3132.66 100548 8589.80 100099 4974.16 100358 8516.25 100224 4599.82 100161 4217.44 100872 4829.54 100266 1324.79 100325 3907.21 100384 7531.68 100561 9853.12 100171 2229.61 100119 1170.68 100377 8663.60 100613 8154.46 100991 5898.29 100101 4782.19 100191 4631.14 100185 3261.16 100564 2884.03 100923 5445.56 100103 6112.96 100515 5887.13 100692 5776.56 100450 7847.18 100624 8393.48 100815 5463.09 100404 2903.22 100192 978.55 100255 9522.76 100740 8099.16 100981 4515.51 100365 5689.42 100501 8936.41 100307 3460.31 100575 7288.58 100385 4150.23 100474 4603.54 100861 7071.76 100778 2807.71 100428 8384.69 100900 8754.32 100354 7231.30 100604 5285.33 100958 9193.94 100585 4105.58 100505 947.87 100810 3154.65 100996 5211.16 100637 4929.54 100582 2926.02 100139 1603.60 100423 8999.98 100956 6349.25 100053 9175.01 100449 8379.21 100128 1031.91 100962 1284.25 100782 2360.36 100857 1177.62 100638 8780.74 100877 8482.27 100238 4387.71 100310 7578.16 100186 1929.83 100356 5662.87 100863 8047.71 100279 4447.01 100241 7782.64 100037 3832.72 100348 2322.46 100661 8727.39 100341 9242.76 100160 8970.33 100125 4588.14 100881 8067.85 100789 3120.92 100416 934.09 100896 6119.31 100864 4682.35 100218 8160.25 100723 8523.55 100020 2846.43 100281 8372.51 100475 5271.06 100458 3578.40 100796 9174.35 100110 9318.79 100054 3705.30 100737 6449.92 100242 1127.79 100024 7313.37 100211 7785.09 100868 7833.71 100614 6676.98 100017 4905.37 100319 6565.18 100231 8273.65 100107 9160.54 100388 3790.12 100538 1408.49 100138 5815.67 100883 9220.16 100406 5521.30 100706 9838.56 100403 2302.74 100363 1597.34 100726 5464.10 100599 3495.71 100096 4915.07 100446 5326.88 100011 8102.58 100804 2244.52 100143 606.07 100903 3344.23 100654 2600.31 100659 8089.98 100436 8972.03 100259 806.19 100764 2179.69 100087 5716.31 100507 1703.67 100583 9961.45 100418 2339.73 100818 5802.56 100540 1541.39 100300 4900.37 100755 8349.25 100725 6561.97 100398 9906.11 100351 1344.18 100516 2588.17 100080 3791.27 100182 6437.55 100587 5362.83 100528 8832.80 100308 7266.76 100891 3052.39 100305 1987.73 100298 9384.12 100617 1169.16 100464 7500.89 100535 1162.74 100253 2720.55 100884 9140.43 100865 5686.79 100136 6422.67 100913 6011.38 100855 5715.46 100906 704.39 100089 1971.57 100457 4084.17 100816 8351.32 100013 5127.52 100511 8590.44 100628 8287.01 100546 8952.20 100055 8570.43 100626 3578.16 100780 4970.85 100705 5918.66 100258 9165.52 100820 9950.61 100704 3766.19 100529 7687.46 100215 9973.16 100323 595.69 100135 5739.68 100819 5872.57 100222 3901.10 100081 1818.17 100247 3909.85 100473 6386.17 100843 3772.86 100944 1112.30 100387 1669.09 100586 5007.34 100765 3914.91 100237 1453.34 100851 6307.27 100664 4638.73 100299 906.80 100297 9701.29 100793 7326.30 100524 4642.45 100776 5126.91 100183 559.49 100570 8809.10 100330 9112.16 100213 1570.06 100767 4501.70 100126 4092.39 100518 4373.60 100948 5975.08 100071 6420.06 100543 5070.59 100124 3468.46 100714 2395.89 100156 1152.07 100347 9013.15 100320 6522.54 100016 8190.00 100886 7563.69 100566 2844.42 100005 2516.89 100694 5127.26 100251 4648.31 100735 9269.63 100467 1950.80 100656 1386.28 100514 583.11 100571 4526.60 100102 924.78 100296 5333.07 100066 9965.80 100684 4590.29 100662 6013.91 100502 7767.56 100783 6524.75 100787 5700.39 100146 4228.60 100930 3448.39 100268 7464.68 100260 1359.72 100977 3796.70 100086 8550.84 100982 8833.96 100972 3239.92 100748 6439.46 100741 9889.46 100625 5961.90 100454 6126.19 100047 5749.03 100940 991.94 100995 2142.53 100352 9881.08 100685 9702.25 100907 4983.62 100031 9070.73 100784 5876.89 100179 4393.26 100917 1859.52 100525 7696.46 100567 1016.36 100619 6437.90 100873 1982.19 100060 7449.62 100530 4265.89 100618 7223.73 100510 9502.79 100699 5813.80 100777 755.34 100846 3261.00 100249 7667.57 100920 4102.37 100489 667.48 100560 5192.93 100775 7657.29 100159 1442.93 100850 9060.19 100355 8853.02 100338 5871.98 100248 5834.20 100267 9124.34 100061 5563.19 100318 674.54 100602 3675.34 100038 8270.69 100774 1686.29 100839 3082.79 100189 533.31 100643 7864.59 100533 1817.65 100026 9030.46 100596 7879.71 100374 1293.17 100681 6085.17 100729 6568.44 100226 6946.90 100717 1321.85 100371 9221.55 100828 1919.03 100425 3946.05 100653 5086.28 100809 6494.70 100340 8321.06 100029 2051.78 100542 4846.50 100184 8166.46 100331 7877.53 100015 2678.97 100105 8597.48 100430 877.48 100198 2659.03 100675 6239.96 100201 3720.91 100082 1463.05 100788 7290.94 100589 1674.51 100806 4931.33 100019 8390.28 100093 6701.39 100576 2253.94 100683 3841.97 100553 3310.37 100859 9500.71 100344 705.93 100451 603.38 100109 8213.62 100379 5106.88 100257 9659.44 100951 4598.44 100650 5615.41 100092 2537.49 100805 9356.27 100899 7807.98 100648 7138.50 100478 3241.67 100503 1361.19 100079 8547.41 100480 8455.25 100481 2278.90 100181 2793.10 100014 9266.83 100879 2847.30 100176 3162.92 100108 2242.39 100957 7028.66 100484 7978.20 100033 1793.94 100006 2494.30 100225 7782.85 100414 7863.82 100686 2444.93 100670 7330.61 100552 7005.22 100887 8912.49 100830 4779.58 100332 9807.20 100479 6816.30 100487 1406.41 100677 6222.07 100208 2063.18 100287 3612.89 100979 589.86 100954 9943.14 100032 8130.70 100295 3564.84 100493 1597.86 100461 5505.71 100122 7557.02 100825 3669.95 100823 6429.07 100554 2985.08 100942 5791.94 100931 8563.19 100206 1292.96 100438 2009.10 100434 4469.06 100573 8926.12 100965 7196.30 100719 2826.23 100410 4348.13 100210 1773.18 100841 2110.68 100333 7028.71 100592 5484.80 100522 6695.94 100050 1259.69 100169 9852.60 100422 3559.63 100794 8627.51 100447 9677.60 100939 3166.91 100838 2837.83 100265 2327.10 100878 804.59 100641 5578.00 100286 6809.64 100807 5175.95 100453 2995.13 100131 8577.23 100547 1190.01 100199 7999.43 100901 1626.73 100933 6948.56 100271 7423.19 100786 9153.67 100912 7793.52 100027 3767.03 100492 916.15 100770 8226.84 100591 4509.56 100393 3492.75 100322 4489.96 100178 9833.92 100252 3667.29 100927 9645.07 100909 2585.93 100207 1593.31 100202 1981.65 100541 1424.20 100112 3768.39 100950 7945.68 100772 569.22 100919 5035.09 100690 4598.47 100460 6890.90 100036 3788.24 100084 1086.79 100228 3945.89 100978 4692.07 100064 9756.45 100392 6858.38 100097 4518.09 100565 7509.72 100023 1638.52 100236 7291.13 100630 1130.67 100572 6194.73 100426 1402.29 100004 8821.44 100620 1231.25 100964 8518.88 100262 7772.05 100652 3380.83 100999 3746.36 100282 4465.14 100521 9122.54 100115 2400.14 100963 7051.30 100062 4824.41 100118 9111.64 100665 4793.53 100366 1539.93 100095 4208.58 100129 8359.94 100378 8131.57 100563 3134.31 100193 5635.56 100517 1989.62 100424 2463.58 100420 3459.67 100925 5742.90 100155 930.20 100811 3542.57 100294 2350.90 100581 6988.52 100520 7087.11 100214 4123.26 100063 4563.86 100165 6221.24 100556 2567.88 100072 2381.42 100100 824.91 100691 7031.16 100359 664.18 100427 2909.35 100000 7182.72 100216 1033.70 100943 8942.39 100606 1239.82 100145 1175.89 100394 7471.53 100959 9696.25 100751 4103.73 100288 6604.91 100747 8820.15 100470 814.00 100360 2751.11 100588 4935.62 100509 2418.06 100483 6550.64 100666 7929.09 100544 3325.90 100022 858.59 100758 2420.39 100065 5122.74 100888 6413.85 100790 8646.91 100091 9914.98 100278 9984.52 100090 7009.41 100616 6420.58 100496 8989.67 100832 1852.76 100970 4155.69 100130 4435.27 100836 6856.88 100885 2562.87 100111 580.53 100168 5800.87 100137 3119.84 100085 5383.61 100350 2454.53 100752 8104.61 100173 8875.70 100041 3567.10 100200 7448.73 100988 4047.09 100395 2367.07 100974 3803.96 100039 4195.99 100234 7093.82 100632 826.03 100689 7422.40 100088 4933.40 100264 1239.65 100045 8571.90 100106 1333.43 100854 6566.00 100945 3798.70 100491 2266.36 100539 5729.56 100314 7485.85 100862 6273.82 100056 7866.41 100245 4823.38 100512 2948.51 100649 8722.25 100368 9486.78 100076 2988.63 100696 568.64 100357 4219.24 100992 8800.32 100736 5881.36 100953 6006.11 100170 5915.50 100785 6479.58 100555 4810.74 100440 9126.65 100902 9565.86 100826 4890.13 100966 1415.24 100133 7198.02 100584 2753.07 100984 5894.94 100094 9607.60 100598 8274.55 100898 7000.80 100827 524.19 100668 9780.86 100157 5376.90 100302 4285.64 100679 1715.14 100326 967.42 100390 8626.14 100431 2772.94 100536 2280.67 100132 6986.90 100937 7283.19 100482 2695.55 100469 5435.25 100164 1455.62 100644 9099.61 100172 8597.52 100068 4228.03 100349 2235.13 100710 7383.02 100069 1612.66 100448 8002.85 100275 5534.17 100911 3800.28 100154 2558.92 100367 8644.37 100188 5263.59 100928 7043.38 100702 3576.18 100466 1190.73 100724 5506.58 100835 1869.76 100284 5354.62 100771 2208.12 100534 1993.03 100667 5901.71 100834 4540.37 100738 962.83 100952 7513.63 100849 6941.67 100934 9289.93 100381 3145.70 100212 8979.04 100042 2249.98 100814 8477.73 100098 8884.35 100417 8720.28 100240 9253.76 100669 6593.88 100220 3079.61 100739 2758.98 100676 7852.18 100642 2812.38 100049 3105.37 100455 4359.77 100158 956.63 100386 8005.73 100766 9415.35 100148 7977.64 100456 7820.93 100594 6649.44 100339 1939.08 100223 6136.91 100419 9542.69 100382 5099.18 100639 3534.35 100941 3134.26 100893 9921.70 100985 4936.28 100313 1382.11 100914 8586.75 100001 4417.52 100672 3116.97 100795 3578.76 100866 5966.80 100922 7037.94 100803 6737.67 100798 568.63 100733 8697.28 100324 8742.47 100718 2416.16 100030 9631.63 100337 2954.67 100892 6032.60 100721 8785.59 100808 4204.07 100600 7812.07 100476 4936.14 100730 3394.17 100720 5152.10 100494 2299.63 100490 6770.00 100227 7940.55 100760 9207.51 100559 4585.89 100938 672.69 100217 5049.54 100127 4886.09 100495 8407.95 100232 4319.88 100605 9811.10 100280 5933.04 100734 3003.29 100971 4776.75 100703 3801.14 100399 1671.13 100372 1250.67 100908 6003.82 100742 8768.21 100870 7589.38 100615 9714.25 100002 6523.37 100051 7520.42 100744 824.72 100035 3568.75 100380 3448.86 100994 3286.20 100848 2669.57 100120 4377.40 100221 1934.45 100052 6541.47 100781 9301.15 100731 8449.61 100601 6873.69 100432 2947.17 100871 4958.81 100342 7894.89 100230 4401.52 100196 3305.18 100549 2650.91 100028 3278.08 100635 7067.68 100852 3425.34 100059 7482.14 100010 2963.02 100328 2069.82 100472 7508.09 100745 9897.61 100246 7374.91 100407 3461.75 100629 9821.34 100597 7940.11 100269 9716.21 100346 4011.16 100904 8582.08 100678 9506.42 100162 3445.58 100329 9477.13 100697 7311.57 100916 1854.17 100673 7246.07 100895 5265.78 100291 3156.34 100141 5671.40 100289 1660.06 100488 1560.92 100961 4087.55 100009 7498.72 100149 8102.71 100750 4737.83 100413 2306.46 100334 1320.43 100986 8298.64 100396 7960.55 100824 8423.79 100918 5700.18 100400 4063.02 100437 7430.51 100550 3373.40 100452 9170.74 100057 1823.93 100256 7154.22 100445 8324.25 100459 2947.69 100759 3500.42 100301 2088.97 100709 5988.37 100389 7472.90 100860 5116.40 100048 3690.14 100303 2219.65 100968 4814.80 100711 6127.41 100802 9049.43 100197 9021.57 100204 9914.68 100429 6787.37 100153 8254.62 100608 7820.60 100990 4331.75 100471 9382.88 100007 5727.00 100402 8213.35 100545 2934.05 100935 3835.78 100270 8211.94 100762 3597.23 100603 9820.65 100175 2339.56 100046 1869.28 100949 4015.02 100712 7767.57 100074 3215.20 100905 5302.60 100987 8017.52 100444 2336.72 100580 1045.45 100983 8900.41 100969 8152.00 100441 6370.19 100345 6458.61 100847 8857.52 100276 3115.36 100327 5138.97 100989 7925.85 100073 6682.68 100831 7987.83 100229 6678.70 100757 7655.80 100695 5609.81 100558 4854.13 100773 3261.95 100504 758.25 100465 5500.52 100315 8652.15 100768 5090.25 100274 5000.06 100519 8514.16 100645 7397.59 100195 5094.88 100753 2669.12 100797 8864.89 100361 9007.37 100142 7231.84 100756 8914.04 100674 5941.08 100837 6179.67 100442 8172.21 100936 8561.53 100194 1399.11 100867 6474.53 100997 5908.70 100383 6386.32 100732 7445.43 100998 2417.83 100910 2293.87 100375 5529.61 100263 3716.49 100754 4286.78 100612 3146.91 100177 1043.48 100636 2455.98 100486 7323.10 100190 6685.73 100568 3805.20 100646 3615.71 100660 9980.56 100121 1858.86 100658 653.53 100373 4207.62 100025 8725.55 100290 1734.50 100497 4213.54 100078 2011.69 100817 4146.00 100769 8804.17 100579 7690.80 100433 2087.54 100932 3184.86 100799 1951.78 100890 4396.81 100443 6844.24 100317 5965.06 100174 786.34 100408 8547.44 100875 6986.28 100569 6733.98 100609 8311.84 100671 9509.89 100728 3899.11 100134 5854.16 100845 8042.03 100187 6955.27 100117 5621.37 100244 3734.87 100219 9232.61 100976 5005.43 100687 7357.14 100312 6830.51 100634 2002.30 100622 1919.25 100144 6432.51 100067 6224.14 100439 8557.69 100273 1935.27 100749 3685.07 100715 9026.70 100150 3468.56 100116 880.40 100874 3367.81 100921 5736.51 100147 9843.95 100532 3553.94 100623 3060.04 100401 8362.61 100018 4336.97 100858 9055.12 100792 6034.66 100698 3164.56 100316 2679.98 100163 5340.83 100727 8867.45 100123 8593.42 100537 9883.31 100412 5488.70 100655 7391.32 100311 2936.13 100499 2179.37 100716 8317.99 100376 3266.85 100869 1589.42 100008 4613.23 100405 9903.33 100462 1521.82 100526 5487.29 100167 7193.79 100924 875.47 100595 3434.95 100926 5822.07 100104 5841.34 100713 2411.31 100205 5671.09 100993 2280.13 100166 4154.57 100353 8597.44 100114 5819.46 100551 5613.42 100578 8311.07 100309 2090.80 100343 5203.58 100498 2247.32 100421 5374.42 100034 1293.98 100003 796.57 100610 7434.72 100321 9120.18 100140 7826.60 100152 3753.25 100840 8371.80 100292 7503.91 100876 8796.78 100967 9353.92 100746 7924.38 100397 4733.47 100243 3902.17 100856 1993.73 100842 5085.55 100415 8357.59 100688 2139.13 100272 8044.50 100894 3712.18 100083 1993.72 100633 929.27 100889 2442.53 100813 9847.18 100293 1212.10 100897 2275.67 100075 9202.21 100722 4624.44 100070 8113.07 100693 4153.28 100335 882.75 100531 1433.78 100364 7325.90 100021 1469.22 100829 5624.91 100463 2929.45 100761 588.84 100680 2456.78 100574 5932.29 100682 3033.03 100557 2972.97 100980 8554.00 100822 9505.55 100043 1335.70 100235 2621.66 100701 5493.78 100233 2253.33 100506 3949.49 100707 6161.62 100370 9866.30 100277 6448.32 100077 9790.70 100946 6783.74 100261 4629.92 100915 3904.54 100477 6790.79 100508 5284.26 100285 7022.96 100621 3295.55 100485 4404.50 100250 4452.26 100468 8202.87 100254 2676.00 100607 7798.25 100306 6576.91 100203 8649.38 100657 4193.00 100973 9933.25 100763 4112.04 100590 7636.71 100113 3693.06 100960 855.31 100058 3539.47 100209 7558.96 100880 1539.79 100812 7927.37 100527 2423.97 100955 622.18 100411 2524.80 100821 3927.19 100304 1598.37 100833 2317.29 100593 6466.20 100929 2667.91 100800 924.54 100500 4347.30 100779 5431.41 100283 1104.70 100708 2799.43 100801 9789.04 100523 6760.37 100577 2361.18 100611 1098.90 100239 3074.70 100180 7866.72 100791 6051.08 100562 4883.50 100012 8224.27 100151 9213.64 100409 560.56
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.may;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
/**
*
* @author Sam
*/
public class AccountList {
private final int MAX_SIZE = 1000;
int count;
BankAccount[] accounts;
void readFromFile(File file) throws FileNotFoundException{
Scanner sc = new Scanner(file);
while (sc.hasNext())
accounts[count++] = new BankAccount(sc.nextLong(), sc.nextDouble());
}
void sort() { //bubble sort
for (int i = 0; i < count -1 ; i++)
for (int j = 0; j < count - 1 - i; j++)
if (accounts[j].compareTo(accounts[j+1].getAccountNumber()) < 0) {
BankAccount tmp = accounts[j];
accounts[j] = accounts[j + 1];
accounts[j + 1 ] = tmp;
}
}
void writeToFile(File file) throws FileNotFoundException { //write to file simply
PrintWriter pw = new PrintWriter(file);
for (int i = 0; i< count; i++)
pw.print(accounts[i].getAccountNumber() + " " + accounts[i].getBalance() + " ");
}
BankAccount search(long accNum) { //binary search
int high = count - 1;
int low = 0;
while(low <= high) {
int mid = (low + high)/2;
if (accounts[mid].compareTo(accNum) == 0)
return accounts[mid];
else if (accounts[mid].compareTo(accNum) < 0)
high = mid - 1;
else
low = mid + 1;
}
return null;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
AccountList ac = new AccountList();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ac.readFromFile(new File("INPUT FILE")); //read from input file
ac.sort(); // sort the array
ac.writeToFile(new File("YOUR NAME")); //change file name as required
System.out.println("Enter account number to search:");
long accNum = Long.parseLong(br.readLine()); //accept acc num
BankAccount tmp = ac.search(accNum); //search
if (tmp == null) //if not found
System.err.println("Accout not found"); //print error msg
else //else show balance
System.out.println("Balacne in account number " + accNum + " is $" + tmp.getBalance());
}
}
class BankAccount implements Comparable<Long>{
private long accountNumber;
private double balance;
public BankAccount(long accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
public long getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
@Override
public int compareTo(Long o) {
return (int) (accountNumber - o);
}
}
I kept the code as simple as possible fot you. If you need any help on this code, please let me know, I shall try my very best to answer all your queries. You can also find the code commented for your help.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.